Skip to content

Commit

Permalink
track lifetimes, still need style lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
nia-e committed Jun 20, 2023
1 parent 216d7ab commit a426472
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 53 deletions.
10 changes: 5 additions & 5 deletions lvgl-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Rusty for LvWidget {
Ok(quote! {
define_object!(#widget_name);

impl #widget_name {
impl<'a> #widget_name<'a> {
#(#methods)*
}
})
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Rusty for LvFunc {
}

pub fn new() -> crate::LvResult<Self> {
let mut parent = crate::display::DefaultDisplay::get_scr_act()?;
let mut parent = crate::display::get_scr_act()?;
Self::create(&mut parent)
}

Expand Down Expand Up @@ -613,7 +613,7 @@ mod test {
let expected_code = quote! {
define_object!(Arc);

impl Arc {
impl<'a> Arc<'a> {

}
};
Expand Down Expand Up @@ -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<Self> {
unsafe {
let ptr = lvgl_sys::lv_arc_create(
Expand All @@ -661,7 +661,7 @@ mod test {
}

pub fn new() -> crate::LvResult<Self> {
let mut parent = crate::display::DefaultDisplay::get_scr_act()?;
let mut parent = crate::display::get_scr_act()?;
Self::create(&mut parent)
}
}
Expand Down
17 changes: 6 additions & 11 deletions lvgl/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ impl<'a> Display {
}

/// Returns the current active screen.
pub fn get_scr_act(&self) -> Result<Screen> {
pub fn get_scr_act(&'a self) -> Result<Screen<'a>> {
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(())
Expand Down Expand Up @@ -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<Screen> {
Ok(get_str_act(None)?.try_into()?)
}
/// Gets the active screen of the default display.
pub(crate) fn get_scr_act() -> Result<Screen<'static>> {
Ok(get_str_act(None)?.try_into()?)
}

/// A buffer of size `N` representing `N` pixels. `N` can be smaller than the
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn disp_get_default() -> Result<Display> {
))
}

pub(crate) fn get_str_act(disp: Option<&Display>) -> Result<Obj> {
pub(crate) fn get_str_act<'a>(disp: Option<&'a Display>) -> Result<Obj<'a>> {
let scr_ptr = unsafe {
lvgl_sys::lv_disp_get_scr_act(
disp.map(|d| d.disp.as_ptr())
Expand Down
34 changes: 19 additions & 15 deletions lvgl/src/lv_core/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 !!")
Expand All @@ -35,26 +38,26 @@ 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<Self> {
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)
}
}
}

pub fn new() -> crate::LvResult<Self> {
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<ptr::NonNull<lvgl_sys::lv_obj_t>> {
if let Some(non_null_ptr) = ptr::NonNull::new(self.raw) {
Ok(non_null_ptr)
Expand Down Expand Up @@ -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<lvgl_sys::lv_obj_t>) -> Option<Self> {
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,
}
}
}
Expand All @@ -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<F>(&mut self, f: F) -> $crate::LvResult<()>
where
F: FnMut(Self, $crate::support::Event<<Self as $crate::Widget>::SpecialEvent>),
Expand All @@ -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<core::ptr::NonNull<lvgl_sys::lv_obj_t>> {
self.core.raw()
}
}

impl $crate::Widget for $item {
impl<'a> $crate::Widget for $item<'a> {
type SpecialEvent = $event_type;
type Part = $part_type;

Expand Down Expand Up @@ -248,7 +252,7 @@ macro_rules! define_object {
// }
//
// pub fn new() -> crate::LvResult<Self> {
// let mut parent = crate::display::DefaultDisplay::get_scr_act()?;
// let mut parent = crate::display::get_scr_act()?;
// Ok(Self::create_at(&mut parent)?)
// }
// }
Expand Down
30 changes: 15 additions & 15 deletions lvgl/src/lv_core/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ 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(),
}
}
}

impl NativeObject for Screen {
impl NativeObject for Screen<'_> {
fn raw(&self) -> crate::LvResult<core::ptr::NonNull<lvgl_sys::lv_obj_t>> {
self.raw.raw()
}
}

impl Widget for Screen {
impl Widget for Screen<'_> {
type SpecialEvent = u32;
type Part = Part;

Expand All @@ -32,31 +32,31 @@ impl Widget for Screen {
}
}

impl TryFrom<Obj> for Screen {
impl<'a> TryFrom<Obj<'a>> for Screen<'a> {
type Error = LvError;

fn try_from(value: Obj) -> Result<Self, Self::Error> {
fn try_from(value: Obj<'a>) -> Result<Self, Self::Error> {
match unsafe { (*value.raw()?.as_mut()).parent } as usize {
0 => Ok(Self { raw: value }),
0 => Ok(Self { raw: value, }),
_ => Err(LvError::InvalidReference),
}
}
}

impl Into<Obj> for Screen {
fn into(self) -> Obj {
impl<'a> Into<Obj<'a>> for Screen<'a> {
fn into(self) -> Obj<'a> {
self.raw
}
}

impl AsRef<Obj> for Screen {
fn as_ref(&self) -> &Obj {
impl<'a> AsRef<Obj<'a>> for Screen<'a> {
fn as_ref(&self) -> &Obj<'a> {
&self.raw
}
}

impl AsMut<Obj> for Screen {
fn as_mut(&mut self) -> &mut Obj {
impl<'a> AsMut<Obj<'a>> for Screen<'a> {
fn as_mut(&mut self) -> &mut Obj<'a> {
&mut self.raw
}
}
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/widgets/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/widgets/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/widgets/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/widgets/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down

0 comments on commit a426472

Please sign in to comment.