From f26de5cf5a298048c41f0c2c36683fa1c60cbeed Mon Sep 17 00:00:00 2001 From: Vidhan Bhatt Date: Fri, 12 Jan 2024 08:18:47 -0500 Subject: [PATCH] chore: add some helper fns --- hypertext/src/alloc.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/hypertext/src/alloc.rs b/hypertext/src/alloc.rs index c4316e9..1e726d1 100644 --- a/hypertext/src/alloc.rs +++ b/hypertext/src/alloc.rs @@ -119,6 +119,7 @@ impl Renderable for Displayed { /// /// The renderer function must handle escaping any special characters. #[derive(Debug, Clone, Copy)] +#[must_use = "this `Render` must be rendered to a string"] pub struct Render(pub F); impl Render { @@ -262,11 +263,22 @@ impl RenderIterator for I where Self::Item: Renderable {} /// r#"

Alice

Age: 20

"#, /// ); /// ``` -pub trait Renderable { +pub trait Renderable +where + Self: Sized, +{ /// Renders this type to the given string. /// /// The implementation must handle escaping any special characters. fn render_to(self, output: &mut String); + + /// Converts this value into a [`Render`]. + #[inline] + fn into_render(self) -> Render { + Render(move |output| { + self.render_to(output); + }) + } } impl Renderable for char {