From a42647285c2dccf21b6eb1b21d2c3f10d6cae2d8 Mon Sep 17 00:00:00 2001 From: Nia Espera Date: Tue, 20 Jun 2023 18:05:42 +0200 Subject: [PATCH] track lifetimes, still need style lifetime --- lvgl-codegen/src/lib.rs | 10 +++++----- lvgl/src/display.rs | 17 ++++++----------- lvgl/src/functions.rs | 2 +- lvgl/src/lv_core/obj.rs | 34 +++++++++++++++++++--------------- lvgl/src/lv_core/screen.rs | 30 +++++++++++++++--------------- lvgl/src/widgets/arc.rs | 2 +- lvgl/src/widgets/bar.rs | 2 +- lvgl/src/widgets/keyboard.rs | 2 +- lvgl/src/widgets/label.rs | 2 +- lvgl/src/widgets/slider.rs | 2 +- lvgl/src/widgets/table.rs | 2 +- 11 files changed, 52 insertions(+), 53 deletions(-) diff --git a/lvgl-codegen/src/lib.rs b/lvgl-codegen/src/lib.rs index 1998b8bc..75a02432 100644 --- a/lvgl-codegen/src/lib.rs +++ b/lvgl-codegen/src/lib.rs @@ -66,7 +66,7 @@ impl Rusty for LvWidget { Ok(quote! { define_object!(#widget_name); - impl #widget_name { + impl<'a> #widget_name<'a> { #(#methods)* } }) @@ -122,7 +122,7 @@ impl Rusty for LvFunc { } pub fn new() -> crate::LvResult { - let mut parent = crate::display::DefaultDisplay::get_scr_act()?; + let mut parent = crate::display::get_scr_act()?; Self::create(&mut parent) } @@ -613,7 +613,7 @@ mod test { let expected_code = quote! { define_object!(Arc); - impl Arc { + impl<'a> Arc<'a> { } }; @@ -645,7 +645,7 @@ mod test { let expected_code = quote! { define_object!(Arc); - impl Arc { + impl<'a> Arc<'a> { pub fn create(parent: &mut impl crate::NativeObject) -> crate::LvResult { unsafe { let ptr = lvgl_sys::lv_arc_create( @@ -661,7 +661,7 @@ mod test { } pub fn new() -> crate::LvResult { - let mut parent = crate::display::DefaultDisplay::get_scr_act()?; + let mut parent = crate::display::get_scr_act()?; Self::create(&mut parent) } } diff --git a/lvgl/src/display.rs b/lvgl/src/display.rs index d3318c42..c5f3d19f 100644 --- a/lvgl/src/display.rs +++ b/lvgl/src/display.rs @@ -69,12 +69,12 @@ impl<'a> Display { } /// Returns the current active screen. - pub fn get_scr_act(&self) -> Result { + pub fn get_scr_act(&'a self) -> Result> { Ok(get_str_act(Some(self))?.try_into()?) } /// Sets a `Screen` as currently active. - pub fn set_scr_act(&mut self, screen: &mut Screen) -> LvResult<()> { + pub fn set_scr_act(&'a self, screen: &'a mut Screen) -> LvResult<()> { let scr_ptr = unsafe { screen.raw()?.as_mut() }; unsafe { lvgl_sys::lv_disp_load_scr(scr_ptr) } Ok(()) @@ -152,14 +152,9 @@ impl Drop for Display { } } -#[derive(Copy, Clone)] -pub(crate) struct DefaultDisplay {} - -impl DefaultDisplay { - /// Gets the active screen of the default display. - pub(crate) fn get_scr_act() -> Result { - Ok(get_str_act(None)?.try_into()?) - } +/// Gets the active screen of the default display. +pub(crate) fn get_scr_act() -> Result> { + Ok(get_str_act(None)?.try_into()?) } /// A buffer of size `N` representing `N` pixels. `N` can be smaller than the @@ -402,7 +397,7 @@ mod tests { .get_scr_act() .expect("Return screen directly from the display instance"); let _screen_default = - DefaultDisplay::get_scr_act().expect("Return screen from the default display"); + get_scr_act().expect("Return screen from the default display"); } #[test] diff --git a/lvgl/src/functions.rs b/lvgl/src/functions.rs index 26b40d9e..972ed3c2 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(disp: Option<&Display>) -> Result { +pub(crate) fn get_str_act<'a>(disp: Option<&'a 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/lv_core/obj.rs b/lvgl/src/lv_core/obj.rs index 77194cc2..6d53d807 100644 --- a/lvgl/src/lv_core/obj.rs +++ b/lvgl/src/lv_core/obj.rs @@ -8,6 +8,7 @@ use crate::lv_core::style::Style; use crate::{Align, LvError, LvResult}; use core::fmt::{self, Debug}; +use core::marker::PhantomData; use core::ptr; /// Represents a native LVGL object. @@ -19,13 +20,15 @@ pub trait NativeObject { /// Generic LVGL object. /// /// This is the parent object of all widget types. It stores the native LVGL raw pointer. -pub struct Obj { +pub struct Obj<'a> { // We use a raw pointer here because we do not control this memory address, it is controlled // by LVGL's global state. raw: *mut lvgl_sys::lv_obj_t, + // This is to ensure safety for style memory; it has no runtime impact + styles_used: PhantomData<&'a lvgl_sys::lv_style_t>, } -impl Debug for Obj { +impl Debug for Obj<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("NativeObject") .field("raw", &"!! LVGL lv_obj_t ptr !!") @@ -35,13 +38,13 @@ impl Debug for Obj { // We need to manually impl methods on Obj since widget codegen is defined in // terms of Obj -impl Obj { +impl Obj<'_> { pub fn create(parent: &mut impl NativeObject) -> LvResult { unsafe { let ptr = lvgl_sys::lv_obj_create(parent.raw()?.as_mut()); if ptr::NonNull::new(ptr).is_some() { //(*ptr).user_data = Box::new(UserDataObj::empty()).into_raw() as *mut _; - Ok(Self { raw: ptr }) + Ok(Self { raw: ptr, styles_used: PhantomData }) } else { Err(LvError::InvalidReference) } @@ -49,12 +52,12 @@ impl Obj { } pub fn new() -> crate::LvResult { - let mut parent = crate::display::DefaultDisplay::get_scr_act()?; + let mut parent = crate::display::get_scr_act()?; Self::create(&mut parent) } } -impl NativeObject for Obj { +impl NativeObject for Obj<'_> { fn raw(&self) -> LvResult> { if let Some(non_null_ptr) = ptr::NonNull::new(self.raw) { Ok(non_null_ptr) @@ -144,19 +147,20 @@ pub trait Widget: NativeObject + Sized { } } -impl Widget for Obj { +impl Widget for Obj<'_> { type SpecialEvent = u32; type Part = Part; unsafe fn from_raw(raw: ptr::NonNull) -> Option { - Some(Self { raw: raw.as_ptr() }) + Some(Self { raw: raw.as_ptr(), styles_used: PhantomData }) } } -impl Default for Obj { +impl Default for Obj<'_> { fn default() -> Self { Self { raw: unsafe { lvgl_sys::lv_obj_create(ptr::null_mut()) }, + styles_used: PhantomData, } } } @@ -176,11 +180,11 @@ macro_rules! define_object { }; ($item:ident, event = $event_type:ty, part = $part_type:ty) => { #[derive(Debug)] - pub struct $item { - core: $crate::Obj, + pub struct $item<'a> { + core: $crate::Obj<'a>, } - impl $item { + impl<'a> $item<'a> { pub fn on_event(&mut self, f: F) -> $crate::LvResult<()> where F: FnMut(Self, $crate::support::Event<::SpecialEvent>), @@ -200,13 +204,13 @@ macro_rules! define_object { } } - impl $crate::NativeObject for $item { + impl<'a> $crate::NativeObject for $item<'a> { fn raw(&self) -> $crate::LvResult> { self.core.raw() } } - impl $crate::Widget for $item { + impl<'a> $crate::Widget for $item<'a> { type SpecialEvent = $event_type; type Part = $part_type; @@ -248,7 +252,7 @@ macro_rules! define_object { // } // // pub fn new() -> crate::LvResult { -// let mut parent = crate::display::DefaultDisplay::get_scr_act()?; +// let mut parent = crate::display::get_scr_act()?; // Ok(Self::create_at(&mut parent)?) // } // } diff --git a/lvgl/src/lv_core/screen.rs b/lvgl/src/lv_core/screen.rs index e6ced46b..b2dee565 100644 --- a/lvgl/src/lv_core/screen.rs +++ b/lvgl/src/lv_core/screen.rs @@ -2,11 +2,11 @@ use crate::{LvError, NativeObject, Obj, Part, Widget}; /// An LVGL screen. #[derive(Debug)] -pub struct Screen { - raw: Obj, +pub struct Screen<'a> { + raw: Obj<'a>, } -impl Default for Screen { +impl Default for Screen<'_> { fn default() -> Self { Self { raw: Obj::default(), @@ -14,13 +14,13 @@ impl Default for Screen { } } -impl NativeObject for Screen { +impl NativeObject for Screen<'_> { fn raw(&self) -> crate::LvResult> { self.raw.raw() } } -impl Widget for Screen { +impl Widget for Screen<'_> { type SpecialEvent = u32; type Part = Part; @@ -32,31 +32,31 @@ impl Widget for Screen { } } -impl TryFrom for Screen { +impl<'a> TryFrom> for Screen<'a> { type Error = LvError; - fn try_from(value: Obj) -> Result { + fn try_from(value: Obj<'a>) -> Result { match unsafe { (*value.raw()?.as_mut()).parent } as usize { - 0 => Ok(Self { raw: value }), + 0 => Ok(Self { raw: value, }), _ => Err(LvError::InvalidReference), } } } -impl Into for Screen { - fn into(self) -> Obj { +impl<'a> Into> for Screen<'a> { + fn into(self) -> Obj<'a> { self.raw } } -impl AsRef for Screen { - fn as_ref(&self) -> &Obj { +impl<'a> AsRef> for Screen<'a> { + fn as_ref(&self) -> &Obj<'a> { &self.raw } } -impl AsMut for Screen { - fn as_mut(&mut self) -> &mut Obj { +impl<'a> AsMut> for Screen<'a> { + fn as_mut(&mut self) -> &mut Obj<'a> { &mut self.raw } } @@ -72,7 +72,7 @@ mod test { const VER_RES: u32 = 240; crate::tests::initialize_test(false); let buffer = DrawBuffer::<{ (HOR_RES * VER_RES) as usize }>::default(); - let mut display = Display::register(buffer, HOR_RES, VER_RES, |_| {}).unwrap(); + let display = Display::register(buffer, HOR_RES, VER_RES, |_| {}).unwrap(); let mut screen_old = display.get_scr_act().unwrap(); let mut screen_new = Screen::default(); display.set_scr_act(&mut screen_new).unwrap(); diff --git a/lvgl/src/widgets/arc.rs b/lvgl/src/widgets/arc.rs index 2825985e..17bcdb6a 100644 --- a/lvgl/src/widgets/arc.rs +++ b/lvgl/src/widgets/arc.rs @@ -2,7 +2,7 @@ use crate::lv_core::obj::NativeObject; use crate::widgets::Arc; use crate::LvResult; -impl Arc { +impl Arc<'_> { // /// Set the start angle, for the given arc part. // /// 0 degrees for the right, 90 degrees for the bottom, etc. // pub fn set_start_angle(&mut self, angle: u16, part: ArcPart) -> LvResult<()> { diff --git a/lvgl/src/widgets/bar.rs b/lvgl/src/widgets/bar.rs index a34f5947..c6aaeb87 100644 --- a/lvgl/src/widgets/bar.rs +++ b/lvgl/src/widgets/bar.rs @@ -2,7 +2,7 @@ use crate::support::AnimationState; use crate::widgets::Bar; use crate::{LvResult, NativeObject}; -impl Bar { +impl Bar<'_> { /// Set minimum and the maximum values of the bar //pub fn set_range(&mut self, min: i16, max: i16) -> LvResult<()> { // unsafe { diff --git a/lvgl/src/widgets/keyboard.rs b/lvgl/src/widgets/keyboard.rs index 76a40b9b..da1f5669 100644 --- a/lvgl/src/widgets/keyboard.rs +++ b/lvgl/src/widgets/keyboard.rs @@ -2,7 +2,7 @@ use crate::widgets::{Keyboard, Textarea}; use crate::LvResult; use crate::NativeObject; -impl Keyboard { +impl Keyboard<'_> { /// Associates a given `Textarea` to the keyboard. pub fn set_textarea(&mut self, textarea: &mut Textarea) -> LvResult<()> { unsafe { diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs index b392050f..a659e8bb 100644 --- a/lvgl/src/widgets/label.rs +++ b/lvgl/src/widgets/label.rs @@ -31,7 +31,7 @@ mod alloc_imp { // } } -impl Label { +impl Label<'_> { pub fn set_long_mode(&mut self, long_mode: LabelLongMode) -> LvResult<()> { unsafe { Ok(lvgl_sys::lv_label_set_long_mode( diff --git a/lvgl/src/widgets/slider.rs b/lvgl/src/widgets/slider.rs index 3d18ec88..bb8d0dda 100644 --- a/lvgl/src/widgets/slider.rs +++ b/lvgl/src/widgets/slider.rs @@ -2,7 +2,7 @@ use crate::lv_core::obj::NativeObject; use crate::widgets::Slider; use crate::{AnimationState, LvResult}; -impl Slider { +impl Slider<'_> { /// Set a new value on the slider pub fn set_value(&self, value: i32, anim: AnimationState) -> LvResult<()> { unsafe { lvgl_sys::lv_bar_set_value(self.core.raw()?.as_ptr(), value, anim.into()) } diff --git a/lvgl/src/widgets/table.rs b/lvgl/src/widgets/table.rs index a5a859f5..28947888 100644 --- a/lvgl/src/widgets/table.rs +++ b/lvgl/src/widgets/table.rs @@ -3,7 +3,7 @@ use crate::widgets::Table; use crate::LvResult; use core::mem::MaybeUninit; -impl Table { +impl Table<'_> { /// Sets the column width. Row height cannot be set manually and is /// calculated by LVGL based on styling parameters. pub fn set_col_width(&mut self, column: u16, width: i16) -> LvResult<()> {