From 194f2f9874a134039f49856d832e710d31cc55b2 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 6 Sep 2025 11:18:39 +0300 Subject: [PATCH] output: remove RenderBox --- output/src/layout_align.rs | 2 +- output/src/output_content.rs | 7 ------- output/src/output_render.rs | 12 +++--------- output/src/test.rs | 4 ++-- proc/src/lib.rs | 4 ++-- tui/examples/tui.rs | 14 +++++++------- tui/src/tui_content/_tui_focus.rs | 2 +- 7 files changed, 16 insertions(+), 29 deletions(-) diff --git a/output/src/layout_align.rs b/output/src/layout_align.rs index 9991d1d..76cc8fd 100644 --- a/output/src/layout_align.rs +++ b/output/src/layout_align.rs @@ -1,7 +1,7 @@ //! ``` //! use ::tengri::{output::*, tui::*}; //! let area: [u16;4] = [10, 10, 20, 20]; -//! fn test (area: [u16;4], item: &impl Content, expected: [u16;4]) { +//! fn test (area: [u16;4], item: &impl Render, expected: [u16;4]) { //! assert_eq!(Content::layout(item, area), expected); //! assert_eq!(Render::layout(item, area), expected); //! }; diff --git a/output/src/output_content.rs b/output/src/output_content.rs index 392f49c..cf09897 100644 --- a/output/src/output_content.rs +++ b/output/src/output_content.rs @@ -31,13 +31,6 @@ impl> Content for Option { } } -/// You can render from a box. -impl Content for RenderBox { - fn content (&self) -> Option + '_> { - Some(self.deref()) - } -} - /// You can render from an opaque pointer. impl Content for &dyn Render where Self: Sized { fn content (&self) -> Option + '_> { diff --git a/output/src/output_render.rs b/output/src/output_render.rs index d1f97a9..d757597 100644 --- a/output/src/output_render.rs +++ b/output/src/output_render.rs @@ -6,11 +6,10 @@ pub trait Render { 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 + 'a> where Self: Sized + 'a { Box::new(self) as Box + 'a> } - /// Perform type erasure, turning `self` into an opaque [Rc]. fn rc <'a> (self) -> Rc + 'a> where Self: Sized + 'a { Rc::new(self) as Rc + 'a> } @@ -66,17 +65,12 @@ impl> Render for [R] { } } -/// Opaque pointer to a renderable that lives on the heap. -/// -/// Return this from [Content::content] to use dynamic dispatch. -pub type RenderBox = Box>; - /// 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 + impl <$($($L),*)? E: Output, $($T$(:$Trait)?),*> Render for $Struct $(<$($L),* $($T),*>>)? { fn render (&$self, $to: &mut E) { $render } } @@ -85,7 +79,7 @@ pub type RenderBox = Box>; $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 } } diff --git a/output/src/test.rs b/output/src/test.rs index d324664..6bd653b 100644 --- a/output/src/test.rs +++ b/output/src/test.rs @@ -146,7 +146,7 @@ proptest! { () } } - impl Content for String { + impl Render 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 Content for Foo {} - fn _make_map + Send + Sync> (data: &Vec) -> impl Content { + fn _make_map + Send + Sync> (data: &Vec) -> impl Render { Map::new(||data.iter(), |_foo, _index|{}) } let _data = vec![Foo, Foo, Foo]; diff --git a/proc/src/lib.rs b/proc/src/lib.rs index bc39ee5..9deff9e 100644 --- a/proc/src/lib.rs +++ b/proc/src/lib.rs @@ -106,7 +106,7 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) { //#[tengri_proc::view(SomeOutput)] //impl SomeView { //#[tengri::view(":view-1")] - //fn view_1 (&self) -> impl Content + use<'_> { + //fn view_1 (&self) -> impl Render + use<'_> { //"view-1" //} //} @@ -114,7 +114,7 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) { //let written = quote! { #parsed }; //assert_eq!(format!("{written}"), format!("{}", quote! { //impl SomeView { - //fn view_1 (&self) -> impl Content + use<'_> { + //fn view_1 (&self) -> impl Render + use<'_> { //"view-1" //} //} diff --git a/tui/examples/tui.rs b/tui/examples/tui.rs index 1d0124f..677f0da 100644 --- a/tui/examples/tui.rs +++ b/tui/examples/tui.rs @@ -76,25 +76,25 @@ content!(TuiOut: |self: Example|{ //#[tengri_proc::view(TuiOut)] //impl Example { - //pub fn title (&self) -> impl Content + use<'_> { + //pub fn title (&self) -> impl Render + use<'_> { //Tui::bg(Color::Rgb(60, 10, 10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EXAMPLES.len())))).boxed() //} - //pub fn code (&self) -> impl Content + use<'_> { + //pub fn code (&self) -> impl Render + use<'_> { //Tui::bg(Color::Rgb(10, 60, 10), Push::y(2, Align::n(format!("{}", EXAMPLES[self.0])))).boxed() //} - //pub fn hello (&self) -> impl Content + use<'_> { + //pub fn hello (&self) -> impl Render + use<'_> { //Tui::bg(Color::Rgb(10, 100, 10), "Hello").boxed() //} - //pub fn world (&self) -> impl Content + use<'_> { + //pub fn world (&self) -> impl Render + use<'_> { //Tui::bg(Color::Rgb(100, 10, 10), "world").boxed() //} - //pub fn hello_world (&self) -> impl Content + use<'_> { + //pub fn hello_world (&self) -> impl Render + use<'_> { //"Hello world!".boxed() //} - //pub fn map_e (&self) -> impl Content + use<'_> { + //pub fn map_e (&self) -> impl Render + use<'_> { //Map::east(5u16, ||0..5u16, |n, _i|format!("{n}")).boxed() //} - //pub fn map_s (&self) -> impl Content + use<'_> { + //pub fn map_s (&self) -> impl Render + use<'_> { //Map::south(5u16, ||0..5u16, |n, _i|format!("{n}")).boxed() //} //} diff --git a/tui/src/tui_content/_tui_focus.rs b/tui/src/tui_content/_tui_focus.rs index cb1be40..f3eba0e 100644 --- a/tui/src/tui_content/_tui_focus.rs +++ b/tui/src/tui_content/_tui_focus.rs @@ -243,7 +243,7 @@ impl FocusOrder for T { } pub trait FocusWrap { - fn wrap > (self, focus: T, content: &'_ W) -> impl Content + '_; + fn wrap > (self, focus: T, content: &'_ W) -> impl Render + '_; } pub fn to_focus_command (input: &TuiIn) -> Option> {