diff --git a/src/widget.rs b/src/widget.rs index 9062411..58ce562 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1,8 +1,8 @@ use async_trait::async_trait; use crate::widgets::{ - Background, Border, Boxed, BoxedAsync, Desync, Either2, Either3, Float, JoinSegment, Layer2, - Padding, Resize, + Background, Border, Boxed, BoxedAsync, BoxedSendSync, Desync, Either2, Either3, Float, + JoinSegment, Layer2, Padding, Resize, }; use crate::{Frame, Size, WidthDb}; @@ -47,6 +47,13 @@ pub trait WidgetExt: Sized { Boxed::new(self) } + fn boxed_send_sync<'a, E>(self) -> BoxedSendSync<'a, E> + where + Self: Widget + Send + Sync + 'a, + { + BoxedSendSync::new(self) + } + fn boxed_async<'a, E>(self) -> BoxedAsync<'a, E> where Self: AsyncWidget + Send + Sync + 'a, diff --git a/src/widgets/boxed.rs b/src/widgets/boxed.rs index 050514c..3d9713f 100644 --- a/src/widgets/boxed.rs +++ b/src/widgets/boxed.rs @@ -13,6 +13,74 @@ impl<'a, E> Boxed<'a, E> { } } +impl Widget for Boxed<'_, E> { + fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.wrap_size(widthdb, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.wrap_draw(frame) + } +} + +pub struct BoxedSendSync<'a, E>(Box + Send + Sync + 'a>); + +impl<'a, E> BoxedSendSync<'a, E> { + pub fn new(inner: I) -> Self + where + I: Widget + Send + Sync + 'a, + { + Self(Box::new(inner)) + } +} + +impl Widget for BoxedSendSync<'_, E> { + fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.wrap_size(widthdb, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.wrap_draw(frame) + } +} + +pub struct BoxedAsync<'a, E>(Box + Send + Sync + 'a>); + +impl<'a, E> BoxedAsync<'a, E> { + pub fn new(inner: I) -> Self + where + I: AsyncWidget + Send + Sync + 'a, + { + Self(Box::new(inner)) + } +} + +#[async_trait] +impl AsyncWidget for BoxedAsync<'_, E> { + async fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.wrap_size(widthdb, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.wrap_draw(frame).await + } +} + trait WidgetWrapper { fn wrap_size( &self, @@ -42,32 +110,6 @@ where } } -impl Widget for Boxed<'_, E> { - fn size( - &self, - widthdb: &mut WidthDb, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.wrap_size(widthdb, max_width, max_height) - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.wrap_draw(frame) - } -} - -pub struct BoxedAsync<'a, E>(Box + Send + Sync + 'a>); - -impl<'a, E> BoxedAsync<'a, E> { - pub fn new(inner: I) -> Self - where - I: AsyncWidget + Send + Sync + 'a, - { - Self(Box::new(inner)) - } -} - #[async_trait] trait AsyncWidgetWrapper { async fn wrap_size( @@ -98,19 +140,3 @@ where (*self).draw(frame).await } } - -#[async_trait] -impl AsyncWidget for BoxedAsync<'_, E> { - async fn size( - &self, - widthdb: &mut WidthDb, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.wrap_size(widthdb, max_width, max_height).await - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.wrap_draw(frame).await - } -}