Skip to content

Commit

Permalink
chore: add some helper fns
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhanio committed Jan 12, 2024
1 parent 60d5ed5 commit f26de5c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion hypertext/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl<T: Display> Renderable for Displayed<T> {
///
/// 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<F: FnOnce(&mut String)>(pub F);

impl<F: FnOnce(&mut String)> Render<F> {
Expand Down Expand Up @@ -262,11 +263,22 @@ impl<I: IntoIterator> RenderIterator for I where Self::Item: Renderable {}
/// r#"<main><div><h1>Alice</h1><p>Age: 20</p></div></main>"#,
/// );
/// ```
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<impl FnOnce(&mut String)> {
Render(move |output| {
self.render_to(output);
})
}
}

impl Renderable for char {
Expand Down

0 comments on commit f26de5c

Please sign in to comment.