diff --git a/lvgl-sys/src/lib.rs b/lvgl-sys/src/lib.rs index ca2fadf9..fd35f222 100644 --- a/lvgl-sys/src/lib.rs +++ b/lvgl-sys/src/lib.rs @@ -23,10 +23,10 @@ mod tests { lv_init(); let horizontal_resolution = lv_disp_get_hor_res(core::ptr::null_mut()); - assert_eq!(horizontal_resolution, 0 as i16); + assert_eq!(horizontal_resolution, 0); let vertical_resolution = lv_disp_get_ver_res(core::ptr::null_mut()); - assert_eq!(vertical_resolution, 0 as i16); + assert_eq!(vertical_resolution, 0); } } } diff --git a/lvgl/src/display.rs b/lvgl/src/display.rs index 5ba7aee5..a574702e 100644 --- a/lvgl/src/display.rs +++ b/lvgl/src/display.rs @@ -67,9 +67,8 @@ impl<'a> Display { let disp_p = &mut display_diver.disp_drv; disp_p.hor_res = hor_res.try_into().unwrap_or(240); disp_p.ver_res = ver_res.try_into().unwrap_or(240); - let ret = Ok(disp_drv_register(&mut display_diver, None)?); + Ok(disp_drv_register(&mut display_diver, None)?) //display_diver.disp_drv.leak(); - ret } /// Returns the current active screen. diff --git a/lvgl/src/functions.rs b/lvgl/src/functions.rs index b99c7734..feb6250f 100644 --- a/lvgl/src/functions.rs +++ b/lvgl/src/functions.rs @@ -38,7 +38,7 @@ pub(crate) fn disp_get_default() -> Result { )) } -pub(crate) fn get_str_act<'a>(disp: Option<&'a Display>) -> Result> { +pub(crate) fn get_str_act(disp: Option<&Display>) -> Result { let scr_ptr = unsafe { lvgl_sys::lv_disp_get_scr_act( disp.map(|d| d.disp.as_ptr()) diff --git a/lvgl/src/lib.rs b/lvgl/src/lib.rs index ce8db6f7..5a4e5b30 100644 --- a/lvgl/src/lib.rs +++ b/lvgl/src/lib.rs @@ -109,8 +109,12 @@ fn once_init() { } } -/// Initializes LVGL. Unless `unsafe_no_autoinit` is enabled, do not call -/// without first calling `deinit()` and dropping all old values. +/// Initializes LVGL. +/// +/// # Safety +/// +/// Unless `unsafe_no_autoinit` is enabled, do not call this function without +/// first calling `deinit()` and dropping all old values. #[cfg(not(feature = "unsafe_no_autoinit"))] pub unsafe fn init() { unsafe { @@ -119,6 +123,10 @@ pub unsafe fn init() { } /// Uninitializes LVGL. Make sure to reinitialize it before reusing it. +/// +/// # Safety +/// +/// This function should not be called if LVGL is already uninitialized. pub unsafe fn deinit() { unsafe { lvgl_sys::lv_deinit() } } diff --git a/lvgl/src/lv_core/screen.rs b/lvgl/src/lv_core/screen.rs index 40fd5670..55b2005a 100644 --- a/lvgl/src/lv_core/screen.rs +++ b/lvgl/src/lv_core/screen.rs @@ -1,19 +1,11 @@ use crate::{LvError, NativeObject, Obj, Part, Widget}; /// An LVGL screen. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Screen<'a> { raw: Obj<'a>, } -impl Default for Screen<'_> { - fn default() -> Self { - Self { - raw: Obj::default(), - } - } -} - impl NativeObject for Screen<'_> { fn raw(&self) -> crate::LvResult> { self.raw.raw() @@ -36,7 +28,7 @@ impl<'a> TryFrom> for Screen<'a> { type Error = LvError; fn try_from(value: Obj<'a>) -> Result { - match unsafe { (*value.raw()?.as_mut()).parent } as usize { + match unsafe { value.raw()?.as_mut().parent } as usize { 0 => Ok(Self { raw: value }), _ => Err(LvError::InvalidReference), } diff --git a/lvgl/src/lv_core/style.rs b/lvgl/src/lv_core/style.rs index 480976a7..f37a34a4 100644 --- a/lvgl/src/lv_core/style.rs +++ b/lvgl/src/lv_core/style.rs @@ -217,10 +217,7 @@ pub enum StyleValues { impl StyleValues { pub fn is_some(&self) -> bool { - match self { - StyleValues::None => false, - _ => true, - } + !matches!(self, StyleValues::None) } } diff --git a/lvgl/src/misc/anim.rs b/lvgl/src/misc/anim.rs index 1ca00da5..b8a2263a 100644 --- a/lvgl/src/misc/anim.rs +++ b/lvgl/src/misc/anim.rs @@ -43,13 +43,13 @@ impl Animation { }, }; - (*anim.raw).time = duration.as_millis().try_into().unwrap_or(0); - (*anim.raw).start_value = start; - (*anim.raw).current_value = start; - (*anim.raw).end_value = end; - (*anim.raw).user_data = Box::::into_raw(Box::new(animator)) as *mut _; - (*anim.raw).var = target as *mut _ as *mut _; - (*anim.raw).exec_cb = Some(animator_trampoline::<'a, 'b, T, F>); + anim.raw.time = duration.as_millis().try_into().unwrap_or(0); + anim.raw.start_value = start; + anim.raw.current_value = start; + anim.raw.end_value = end; + anim.raw.user_data = Box::::into_raw(Box::new(animator)) as *mut _; + anim.raw.var = target as *mut _ as *mut _; + anim.raw.exec_cb = Some(animator_trampoline::<'a, 'b, T, F>); Ok(anim) } @@ -64,32 +64,32 @@ impl Animation { /// Sets the delay before starting the animation. pub fn set_delay(&mut self, delay: Duration) -> Result<(), TryFromIntError> { - (*self.raw).act_time = -(delay.as_millis().try_into()?); + self.raw.act_time = -(delay.as_millis().try_into()?); Ok(()) } /// Sets the delay before playback. pub fn set_playback_delay(&mut self, delay: Duration) -> Result<(), TryFromIntError> { - (*self.raw).playback_delay = delay.as_millis().try_into()?; + self.raw.playback_delay = delay.as_millis().try_into()?; Ok(()) } /// Sets the total playback time. pub fn set_playback_time(&mut self, time: Duration) -> Result<(), TryFromIntError> { - (*self.raw).playback_time = time.as_millis().try_into()?; + self.raw.playback_time = time.as_millis().try_into()?; Ok(()) } /// Sets the delay before repeating the animation. pub fn set_repeat_delay(&mut self, delay: Duration) -> Result<(), TryFromIntError> { - (*self.raw).repeat_delay = delay.as_millis().try_into()?; + self.raw.repeat_delay = delay.as_millis().try_into()?; Ok(()) } /// Sets how many times the animation repeats. pub fn set_repeat_count(&mut self, count: AnimRepeatCount) { unsafe { - (*self.raw).repeat_cnt = match count { + self.raw.repeat_cnt = match count { AnimRepeatCount::Finite(c) => c, AnimRepeatCount::Infinite => lvgl_sys::LV_ANIM_REPEAT_INFINITE .try_into() diff --git a/lvgl/src/support.rs b/lvgl/src/support.rs index 358f58e7..a85cba6a 100644 --- a/lvgl/src/support.rs +++ b/lvgl/src/support.rs @@ -393,9 +393,9 @@ pub enum LabelLongMode { Wrap = lvgl_sys::LV_LABEL_LONG_WRAP, } -impl Into for LabelLongMode { - fn into(self) -> u8 { - unsafe { (self as u32).try_into().unwrap_unchecked() } +impl From for u8 { + fn from(value: LabelLongMode) -> Self { + unsafe { (value as u32).try_into().unwrap_unchecked() } } } diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs index 44f6243d..69306fb1 100644 --- a/lvgl/src/widgets/label.rs +++ b/lvgl/src/widgets/label.rs @@ -34,10 +34,11 @@ mod alloc_imp { impl Label<'_> { pub fn set_long_mode(&mut self, long_mode: LabelLongMode) -> LvResult<()> { unsafe { - Ok(lvgl_sys::lv_label_set_long_mode( + lvgl_sys::lv_label_set_long_mode( self.raw()?.as_mut(), long_mode.into(), - )) + ); + Ok(()) } }