use crate::*; /// Composable renderable with static dispatch. pub trait Content: Sized { /// Return opaque [Render]able. fn content (&self) -> Option + '_> { Option::<()>::None } } /// The platonic ideal unit of [Content]: total emptiness at dead center (e=1vg^sqrt(-1)) impl Content for () {} impl Content for fn(&mut E) { fn content (&self) -> Option + '_> { Some(self) } } impl> Content for fn()->T { fn content (&self) -> Option + '_> { Some(self()) } } impl> Content for Option { fn content (&self) -> Option + '_> { if let Some(content) = self { content.content() } else { None } } } /// You can render from an opaque pointer. impl Content for &dyn Render where Self: Sized { fn content (&self) -> Option + '_> { #[allow(suspicious_double_ref_op)] Some(self.deref()) } } /// Implement composable content for a struct. #[macro_export] macro_rules! content { // Implement for all [Output]s. (|$self:ident:$Struct:ty| $content:expr) => { impl Content for $Struct { fn content (&$self) -> impl Render + '_ { Some($content) } } }; // Implement for specific [Output]. ($Output:ty:| $self:ident: $Struct:ident$(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)? |$content:expr) => { impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output> for $Struct $(<$($($L)? $($T)?),+>)? { fn content (&$self) -> impl Render<$Output> + '_ { $content } } }; }