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 fn new (thunk: F) -> Self { Self(Default::default(), thunk) } } impl, F: Fn()->T + Send + Sync> Content for Thunk { fn content (&self) -> impl Render { (self.1)() } } pub struct BoxThunk<'a, E: Output>(PhantomData, BoxBox + 'a> + Send + Sync + 'a>); impl<'a, E: Output> BoxThunk<'a, E> { pub fn new (thunk: BoxBox + 'a> + Send + Sync + 'a>) -> Self { Self(Default::default(), thunk) } } impl<'a, E: Output> Content for BoxThunk<'a, E> { fn content (&self) -> impl Render { (self.1)() } } impl<'a, E: Output, F: Fn()->T + Send + Sync + 'a, T: Render + Send + Sync + 'a> From for BoxThunk<'a, E> { fn from (f: F) -> Self { Self(Default::default(), Box::new(move||f().boxed())) } } //impl<'a, E: Output, F: Fn()->Box + 'a> + Send + Sync + 'a> From for BoxThunk<'a, E> { //fn from (f: F) -> Self { //Self(Default::default(), Box::new(f)) //} //} pub struct RenderThunk(PhantomData, F); impl RenderThunk { pub fn new (render: F) -> Self { Self(Default::default(), render) } } impl Content for RenderThunk { fn render (&self, to: &mut E) { (self.1)(to) } } pub struct LayoutThunkE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync>(PhantomData, F1, F2); implE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> LayoutThunk { pub fn new (layout: F1, render: F2) -> Self { Self(Default::default(), layout, render) } } implE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> Content for LayoutThunk { fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) } fn render (&self, to: &mut E) { (self.2)(to) } }