Use macros to reduce boilerplate code

This commit is contained in:
Rafael Caricio 2020-04-13 16:42:05 +02:00
parent 5b82ae6266
commit c5282439d9

View file

@ -97,6 +97,22 @@ pub trait Object: Container {
} }
} }
macro_rules! define_object {
($item:ident) => {
pub struct $item {
core: ObjectX,
}
impl Container for $item {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
self.core.raw()
}
}
impl Object for $item {}
}
}
pub enum ObjectAlign { pub enum ObjectAlign {
Center, Center,
InTopLeft, InTopLeft,
@ -121,9 +137,7 @@ pub enum ObjectAlign {
OutRightBottom, OutRightBottom,
} }
pub struct Button { define_object!(Button);
core: ObjectX,
}
impl Button { impl Button {
pub fn new<C>(parent: &mut C) -> Self where C: Container { pub fn new<C>(parent: &mut C) -> Self where C: Container {
@ -136,14 +150,6 @@ impl Button {
} }
} }
impl Container for Button {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
self.core.raw()
}
}
impl Object for Button {}
pub enum LabelAlign { pub enum LabelAlign {
Left, Left,
Center, Center,
@ -151,9 +157,7 @@ pub enum LabelAlign {
Auto, Auto,
} }
pub struct Label { define_object!(Label);
core: ObjectX,
}
impl Label { impl Label {
pub fn new<C>(parent: &mut C) -> Self where C: Container { pub fn new<C>(parent: &mut C) -> Self where C: Container {
@ -186,11 +190,3 @@ impl Label {
} }
} }
} }
impl Container for Label {
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
self.core.raw()
}
}
impl Object for Label {}