use crate::*; /// Because we can't implement [Draw] for `F: FnOnce...` without conflicts. pub struct Thunk(pub F, std::marker::PhantomData); implPerhaps>> Draw for Thunk { fn draw (self, to: &mut T) -> Perhaps> { (self.0)(to) } } /// Basic [Draw]able closure. /// /// ``` /// # use tengri::*; /// # fn test () -> impl Draw { /// thunk(|to: &mut Tui|Ok(Some(to.1))) /// # } /// ``` pub const fn thunk Perhaps>> ( draw: F ) -> Thunk { Thunk(draw, std::marker::PhantomData) } /// Only render when condition is true. /// /// ``` /// # use tengri::*; /// # fn test () -> impl Draw { /// when(true, "Yes") /// # } /// ``` pub const fn when (condition: bool, draw: impl Draw) -> impl Draw { thunk(move|to: &mut T|if condition { draw.draw(to) } else { Ok(Default::default()) }) } /// Render one thing if a condition is true and another false. /// /// ``` /// # use tengri::*; /// # fn test () -> impl Draw { /// either(true, "Yes", "No") /// # } /// ``` pub const fn either (condition: bool, a: impl Draw, b: impl Draw) -> impl Draw { thunk(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) }) }