Skip to content

Commit

Permalink
style lifetime tracking works :D
Browse files Browse the repository at this point in the history
  • Loading branch information
nia-e committed Jun 20, 2023
1 parent a426472 commit 29642c0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lvgl/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn task_handler() {

/// Directly send an event to a specific widget.
#[inline]
pub fn event_send<W: Widget>(obj: &mut W, event: Event<W::SpecialEvent>) -> LvResult<()> {
pub fn event_send<W: for<'a> Widget<'a>>(obj: &mut W, event: Event<<W as Widget<'_>>::SpecialEvent>) -> LvResult<()> {
unsafe {
lvgl_sys::lv_event_send(obj.raw()?.as_mut(), event.into(), ptr::null_mut());
};
Expand Down
14 changes: 7 additions & 7 deletions lvgl/src/lv_core/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl NativeObject for Obj<'_> {
}

/// A wrapper for all LVGL common operations on generic objects.
pub trait Widget: NativeObject + Sized {
pub trait Widget<'a>: NativeObject + Sized + 'a {
type SpecialEvent;
type Part: Into<lvgl_sys::lv_part_t>;

Expand All @@ -82,7 +82,7 @@ pub trait Widget: NativeObject + Sized {
unsafe fn from_raw(raw_pointer: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Option<Self>;

/// Adds a `Style` to a given widget.
fn add_style(&mut self, part: Self::Part, style: &mut Style) -> LvResult<()> {
fn add_style(&mut self, part: Self::Part, style: &'a mut Style) -> LvResult<()> {
unsafe {
lvgl_sys::lv_obj_add_style(
self.raw()?.as_mut(),
Expand Down Expand Up @@ -147,7 +147,7 @@ pub trait Widget: NativeObject + Sized {
}
}

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

Expand Down Expand Up @@ -187,15 +187,15 @@ macro_rules! define_object {
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>),
F: FnMut(Self, $crate::support::Event<<Self as $crate::Widget<'a>>::SpecialEvent>),
{
use $crate::NativeObject;
unsafe {
let obj = self.raw()?.as_mut();
obj.user_data = $crate::Box::into_raw($crate::Box::new(f)) as *mut _;
lvgl_sys::lv_obj_add_event_cb(
obj,
lvgl_sys::lv_event_cb_t::Some($crate::support::event_callback::<Self, F>),
lvgl_sys::lv_event_cb_t::Some($crate::support::event_callback::<'a, Self, F>),
lvgl_sys::lv_event_code_t_LV_EVENT_ALL,
obj.user_data,
);
Expand All @@ -204,13 +204,13 @@ macro_rules! define_object {
}
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion lvgl/src/lv_core/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl NativeObject for Screen<'_> {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions lvgl/src/misc/anim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Animation {
animator: F,
) -> LvResult<Self>
where
T: Widget,
T: for<'b> Widget<'b>,
F: FnMut(&mut Obj, i32) + 'a,
{
unsafe {
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Animation {

unsafe extern "C" fn animator_trampoline<'a, T, F>(obj: *mut c_void, val: i32)
where
T: Widget,
T: for<'b> Widget<'b>,
F: FnMut(&mut Obj, i32) + 'a,
{
unsafe {
Expand Down
6 changes: 3 additions & 3 deletions lvgl/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ pub enum PointerEvent {
DragThrowBegin,
}

pub(crate) unsafe extern "C" fn event_callback<T, F>(event: *mut lvgl_sys::lv_event_t)
pub(crate) unsafe extern "C" fn event_callback<'a, T, F>(event: *mut lvgl_sys::lv_event_t)
where
T: Widget + Sized,
F: FnMut(T, Event<T::SpecialEvent>),
T: Widget<'a> + Sized,
F: FnMut(T, Event<<T as Widget<'a>>::SpecialEvent>),
{
let code = (*event).code;
let obj = (*event).target;
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 @@ -8,7 +8,7 @@ mod alloc_imp {
use cstr_core::CString;
//use core::convert::TryFrom;

impl<S: AsRef<str>> From<S> for Label {
impl<S: AsRef<str>> From<S> for Label<'_> {
fn from(text: S) -> Self {
// text.try_into().unwrap()
let text_cstr = CString::new(text.as_ref()).unwrap();
Expand Down

0 comments on commit 29642c0

Please sign in to comment.