From 93c68afc0e19e16a6d472f37aef16041d6930883 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 11 Jun 2021 18:13:10 +0200 Subject: [PATCH] Wrap alloc extension in its own module --- lvgl/src/display.rs | 2 +- lvgl/src/widgets/label.rs | 36 +++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lvgl/src/display.rs b/lvgl/src/display.rs index e3fd81c..c140e3f 100644 --- a/lvgl/src/display.rs +++ b/lvgl/src/display.rs @@ -79,7 +79,7 @@ impl DrawBuffer { if self.initialized.swap_and_check() { // TODO: needs to be 'static somehow // Cannot be in the DrawBuffer struct because the type `lv_disp_buf_t` contains a raw - // pointer and raw pointers are not Sync and consequently cannot be in `static` variables. + // pointer and raw pointers are not Send and consequently cannot be in `static` variables. let mut inner: MaybeUninit = MaybeUninit::uninit(); let primary_buffer_guard = self.refresh_buffer.lock(); let draw_buf = unsafe { diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs index 4e99bd3..68cdabd 100644 --- a/lvgl/src/widgets/label.rs +++ b/lvgl/src/widgets/label.rs @@ -1,9 +1,6 @@ use crate::widgets::Label; use crate::{LvResult, NativeObject}; -#[cfg(feature = "alloc")] -use cstr_core::CString; - impl Label { pub fn set_label_align(&mut self, align: LabelAlign) -> LvResult<()> { unsafe { @@ -14,15 +11,36 @@ impl Label { } #[cfg(feature = "alloc")] -impl> From for Label { - fn from(text: S) -> Self { - let text_cstr = CString::new(text.as_ref()).unwrap(); - let mut label = Label::new().unwrap(); - label.set_text(text_cstr.as_c_str()).unwrap(); - label +mod alloc_imp { + use crate::widgets::Label; + use crate::LvError; + use cstr_core::CString; + use core::convert::TryFrom; + + impl> From for Label { + fn from(text: S) -> Self { + // text.try_into().unwrap() + let text_cstr = CString::new(text.as_ref()).unwrap(); + let mut label = Label::new().unwrap(); + label.set_text(text_cstr.as_c_str()).unwrap(); + label + } } + + // Issue link: https://github.com/rust-lang/rust/issues/50133 + // + // impl> TryFrom for Label { + // type Error = LvError; + // fn try_from(text: S) -> Result { + // let text_cstr = CString::new(text.as_ref())?; + // let mut label = Label::new()?; + // label.set_text(text_cstr.as_c_str())?; + // Ok(label) + // } + // } } + #[derive(Debug, Copy, Clone, PartialEq)] #[repr(u8)] pub enum LabelAlign {