use crate::*; use std::marker::PhantomData; /// Lazily-evaluated [Render]able. pub struct Thunk, F: Fn()->T + Send + Sync>( PhantomData, F ); impl, F: Fn()->T + Send + Sync> Thunk { pub const fn new (thunk: F) -> Self { Self(PhantomData, thunk) } } impl, F: Fn()->T + Send + Sync> Content for Thunk { fn content (&self) -> impl Render { (self.1)() } } pub struct ThunkBox<'a, E: Output>( PhantomData, BoxRenderBox<'a, E> + Send + Sync + 'a> ); impl<'a, E: Output> ThunkBox<'a, E> { pub const fn new (thunk: BoxRenderBox<'a, E> + Send + Sync + 'a>) -> Self { Self(PhantomData, thunk) } } impl<'a, E: Output> Content for ThunkBox<'a, E> { fn content (&self) -> impl Render { (self.1)() } } impl<'a, E, F, T> From for ThunkBox<'a, E> where E: Output, F: Fn()->T + Send + Sync + 'a, T: Render + Send + Sync + 'a { fn from (f: F) -> Self { Self(PhantomData, Box::new(move||f().boxed())) } } //impl<'a, E: Output, F: Fn()->Box + 'a> + Send + Sync + 'a> From for ThunkBox<'a, E> { //fn from (f: F) -> Self { //Self(Default::default(), Box::new(f)) //} //} pub struct ThunkRender(PhantomData, F); impl ThunkRender { pub fn new (render: F) -> Self { Self(PhantomData, render) } } impl Content for ThunkRender { fn render (&self, to: &mut E) { (self.1)(to) } } pub struct ThunkLayout< E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync >( PhantomData, F1, F2 ); implE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> ThunkLayout { pub fn new (layout: F1, render: F2) -> Self { Self(PhantomData, layout, render) } } impl Content for ThunkLayout where E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync { fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) } fn render (&self, to: &mut E) { (self.2)(to) } }