use crate::*; pub struct Group(T); impl Group { pub const fn new () -> Group<()> { Group(()) } pub const fn add (self, value: U) -> Group<(T, U)> { Group((self.0, value)) } } /// Lazily-evaluated [Draw]able. pub struct Thunk, F: Fn()->T>( PhantomData, F ); impl, F: Fn()->T> Thunk { pub const fn new (thunk: F) -> Self { Self(PhantomData, thunk) } } impl + Layout, F: Fn()->T> Content for Thunk { fn content (&self) -> impl Draw + Layout + '_ { (self.1)() } } pub struct ThunkDraw(PhantomData, F); impl ThunkDraw { pub fn new (render: F) -> Self { Self(PhantomData, render) } } impl Draw for ThunkDraw { fn draw (&self, to: &mut E) { (self.1)(to) } } #[derive(Debug, Default)] pub struct Memo { pub value: T, pub view: Arc> } impl Memo { pub fn new (value: T, view: U) -> Self { Self { value, view: Arc::new(view.into()) } } pub fn update ( &mut self, newval: T, render: impl Fn(&mut U, &T, &T)->R ) -> Option { if newval != self.value { let result = render(&mut*self.view.write().unwrap(), &newval, &self.value); self.value = newval; return Some(result); } None } } /// Clear a pre-allocated buffer, then write into it. #[macro_export] macro_rules! rewrite { ($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } } }