Add Desync widget to turn Widgets into AsyncWidgets

This commit is contained in:
Joscha 2023-04-17 19:36:44 +02:00
parent 57788a9dd9
commit 4179e7f56c
3 changed files with 49 additions and 2 deletions

View file

@ -1,8 +1,8 @@
use async_trait::async_trait;
use crate::widgets::{
Background, Border, Boxed, BoxedAsync, Either2, Either3, Float, JoinSegment, Layer2, Padding,
Resize,
Background, Border, Boxed, BoxedAsync, Desync, Either2, Either3, Float, JoinSegment, Layer2,
Padding, Resize,
};
use crate::{Frame, Size, WidthDb};
@ -53,6 +53,9 @@ pub trait WidgetExt: Sized {
{
BoxedAsync::new(self)
}
fn desync(self) -> Desync<Self> {
Desync(self)
}
fn first2<W2>(self) -> Either2<Self, W2> {
Either2::First(self)

View file

@ -2,6 +2,7 @@ pub mod background;
pub mod border;
pub mod boxed;
pub mod cursor;
pub mod desync;
pub mod editor;
pub mod either;
pub mod empty;
@ -17,6 +18,7 @@ pub use background::*;
pub use border::*;
pub use boxed::*;
pub use cursor::*;
pub use desync::*;
pub use editor::*;
pub use either::*;
pub use empty::*;

42
src/widgets/desync.rs Normal file
View file

@ -0,0 +1,42 @@
use async_trait::async_trait;
use crate::{AsyncWidget, Widget};
pub struct Desync<I>(pub I);
impl<E, I> Widget<E> for Desync<I>
where
I: Widget<E>,
{
fn size(
&self,
widthdb: &mut crate::WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<crate::Size, E> {
self.0.size(widthdb, max_width, max_height)
}
fn draw(self, frame: &mut crate::Frame) -> Result<(), E> {
self.0.draw(frame)
}
}
#[async_trait]
impl<E, I> AsyncWidget<E> for Desync<I>
where
I: Widget<E> + Send + Sync,
{
async fn size(
&self,
widthdb: &mut crate::WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<crate::Size, E> {
self.0.size(widthdb, max_width, max_height)
}
async fn draw(self, frame: &mut crate::Frame) -> Result<(), E> {
self.0.draw(frame)
}
}