output: remove RenderBox
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-09-06 11:18:39 +03:00
parent 1c21a85f27
commit 194f2f9874
7 changed files with 16 additions and 29 deletions

View file

@ -1,7 +1,7 @@
//! ```
//! use ::tengri::{output::*, tui::*};
//! let area: [u16;4] = [10, 10, 20, 20];
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
//! fn test (area: [u16;4], item: &impl Render<TuiOut>, expected: [u16;4]) {
//! assert_eq!(Content::layout(item, area), expected);
//! assert_eq!(Render::layout(item, area), expected);
//! };

View file

@ -31,13 +31,6 @@ impl<E: Output, T: Content<E>> Content<E> for Option<T> {
}
}
/// You can render from a box.
impl<E: Output> Content<E> for RenderBox<E> {
fn content (&self) -> Option<impl Render<E> + '_> {
Some(self.deref())
}
}
/// You can render from an opaque pointer.
impl<E: Output> Content<E> for &dyn Render<E> where Self: Sized {
fn content (&self) -> Option<impl Render<E> + '_> {

View file

@ -6,11 +6,10 @@ pub trait Render<E: Output> {
fn render (&self, output: &mut E);
/// Compute layout.
fn layout (&self, area: E::Area) -> E::Area { area }
/// Perform type erasure, turning `self` into an opaque [RenderBox].
fn boxed <'a> (self) -> Box<dyn Render<E> + 'a> where Self: Sized + 'a {
Box::new(self) as Box<dyn Render<E> + 'a>
}
/// Perform type erasure, turning `self` into an opaque [Rc].
fn rc <'a> (self) -> Rc<dyn Render<E> + 'a> where Self: Sized + 'a {
Rc::new(self) as Rc<dyn Render<E> + 'a>
}
@ -66,17 +65,12 @@ impl<E: Output, R: Render<E>> Render<E> for [R] {
}
}
/// Opaque pointer to a renderable that lives on the heap.
///
/// Return this from [Content::content] to use dynamic dispatch.
pub type RenderBox<E> = Box<dyn Render<E>>;
/// Implement custom rendering for a struct.
#[macro_export] macro_rules! render {
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $render:expr) => {
impl <$($($L),*)? E: Output, $($T$(:$Trait)?),*> Content<E>
impl <$($($L),*)? E: Output, $($T$(:$Trait)?),*> Render<E>
for $Struct $(<$($L),* $($T),*>>)? {
fn render (&$self, $to: &mut E) { $render }
}
@ -85,7 +79,7 @@ pub type RenderBox<E> = Box<dyn Render<E>>;
$self:ident:
$Struct:ident $(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?, $to:ident
|$render:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Render<$Output>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut $Output) { $render }
}

View file

@ -146,7 +146,7 @@ proptest! {
()
}
}
impl Content<TestOutput> for String {
impl Render<TestOutput> for String {
fn render (&self, to: &mut TestOutput) {
to.area_mut().set_w(self.len() as u16);
}
@ -162,7 +162,7 @@ proptest! {
#[test] fn test_iter_map () {
struct Foo;
impl<T: Output> Content<T> for Foo {}
fn _make_map <T: Output, U: Content<T> + Send + Sync> (data: &Vec<U>) -> impl Content<T> {
fn _make_map <T: Output, U: Content<T> + Send + Sync> (data: &Vec<U>) -> impl Render<T> {
Map::new(||data.iter(), |_foo, _index|{})
}
let _data = vec![Foo, Foo, Foo];