Box::deref makes the EDN rendering examples really work!

This commit is contained in:
🪞👃🪞 2025-01-05 16:37:06 +01:00
parent ea8ba031c3
commit 62a0e8c17c
9 changed files with 103 additions and 92 deletions

View file

@ -1,4 +1,5 @@
use crate::*;
use std::ops::Deref;
/// Custom layout and rendering.
pub trait Render<E: Engine>: Send + Sync {
@ -8,11 +9,57 @@ pub trait Render<E: Engine>: Send + Sync {
Box::new(self) as RenderBox<'a, E>
}
}
pub type RenderDyn<'a, Engine> = dyn Render<Engine> + 'a;
impl<'a, E: Engine> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn content (&self) -> impl Render<E> { self }
fn content (&self) -> impl Render<E> { *self }
fn layout (&self, area: E::Area) -> E::Area { Render::layout(self.deref(), area) }
fn render (&self, output: &mut E::Output) { Render::render(self.deref(), output) }
}
pub type RenderBox<'a, E: Engine> = Box<RenderDyn<'a, E>>;
impl<'a, E: Engine> Content<E> for RenderBox<'a, E> {
fn content (&self) -> impl Render<E> { self }
fn content (&self) -> impl Render<E> { self.deref() }
}
impl<E: Engine, C: Content<E>> Render<E> for C {
fn layout (&self, area: E::Area) -> E::Area { Content::layout(self, area) }
fn render (&self, output: &mut E::Output) { Content::render(self, output) }
}
#[macro_export] macro_rules! render {
(($self:ident:$Struct:ty) => $content:expr) => {
impl <E: Engine> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> { Some($content) }
}
};
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $render:expr) => {
impl <$($($L),*)? E: Engine, $($T$(:$Trait)?),*> Content<E>
for $Struct $(<$($L),* $($T),*>>)? {
fn render (&$self, $to: &mut E::Output) { $render }
}
};
($Engine:ty:
($self:ident:$Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?) => $content:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Render<$Engine> { $content }
}
};
($Engine:ty:
|$self:ident : $Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?, $to:ident| $render:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut <$Engine as Engine>::Output) { $render }
}
};
}