Add BoxedSencSync which is Send + Sync

This commit is contained in:
Joscha 2023-04-17 20:26:26 +02:00
parent 968dbe501f
commit f414db40d5
2 changed files with 77 additions and 44 deletions

View file

@ -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<E> + Send + Sync + 'a,
{
BoxedSendSync::new(self)
}
fn boxed_async<'a, E>(self) -> BoxedAsync<'a, E>
where
Self: AsyncWidget<E> + Send + Sync + 'a,

View file

@ -13,6 +13,74 @@ impl<'a, E> Boxed<'a, E> {
}
}
impl<E> Widget<E> for Boxed<'_, E> {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<dyn WidgetWrapper<E> + Send + Sync + 'a>);
impl<'a, E> BoxedSendSync<'a, E> {
pub fn new<I>(inner: I) -> Self
where
I: Widget<E> + Send + Sync + 'a,
{
Self(Box::new(inner))
}
}
impl<E> Widget<E> for BoxedSendSync<'_, E> {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<dyn AsyncWidgetWrapper<E> + Send + Sync + 'a>);
impl<'a, E> BoxedAsync<'a, E> {
pub fn new<I>(inner: I) -> Self
where
I: AsyncWidget<E> + Send + Sync + 'a,
{
Self(Box::new(inner))
}
}
#[async_trait]
impl<E> AsyncWidget<E> for BoxedAsync<'_, E> {
async fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<E> {
fn wrap_size(
&self,
@ -42,32 +110,6 @@ where
}
}
impl<E> Widget<E> for Boxed<'_, E> {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<dyn AsyncWidgetWrapper<E> + Send + Sync + 'a>);
impl<'a, E> BoxedAsync<'a, E> {
pub fn new<I>(inner: I) -> Self
where
I: AsyncWidget<E> + Send + Sync + 'a,
{
Self(Box::new(inner))
}
}
#[async_trait]
trait AsyncWidgetWrapper<E> {
async fn wrap_size(
@ -98,19 +140,3 @@ where
(*self).draw(frame).await
}
}
#[async_trait]
impl<E> AsyncWidget<E> for BoxedAsync<'_, E> {
async fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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
}
}