pub trait IntoHtml {
// Required method
fn into_html(self) -> impl IntoHtml;
// Provided methods
fn escape_and_write(self, buf: &mut Buffer)
where Self: Sized { ... }
fn size_hint(&self) -> usize { ... }
fn into_string(self) -> String
where Self: Sized { ... }
}
Expand description
A type that can be represented as HTML.
Required Methods§
Sourcefn into_html(self) -> impl IntoHtml
fn into_html(self) -> impl IntoHtml
Converts this value into HTML by producing a type that implements
IntoHtml
.
This method enables composition of HTML structures by delegating
rendering to the returned value. Use it to build nested HTML
elements, combine components, or leverage existing IntoHtml
implementations.
§Examples
Compose nested HTML elements using macros:
struct Article {
title: String,
content: String,
author: String,
}
impl IntoHtml for Article {
fn into_html(self) -> impl IntoHtml {
article!(
h1!(self.title),
p!(class = "content", self.content),
footer!("Written by ", self.author)
)
}
}
Chain multiple implementations through delegation:
struct ArticlePage {
title: String,
articles: Vec<Article>,
}
impl IntoHtml for ArticlePage {
fn into_html(self) -> impl IntoHtml {
html!(head!(title!(self.title)), body!(self.articles))
}
}
For “leaf” types (elements that render directly without children, like
primitive values), always return self
to avoid infinite recursion:
struct TextNode(String);
impl IntoHtml for TextNode {
fn into_html(self) -> impl IntoHtml {
// Leaf type returns itself to terminate the rendering chain
self
}
fn escape_and_write(self, buf: &mut Buffer) {
escape_into(buf, &self.0);
}
fn size_hint(&self) -> usize {
self.0.len()
}
}
Provided Methods§
Sourcefn escape_and_write(self, buf: &mut Buffer)where
Self: Sized,
fn escape_and_write(self, buf: &mut Buffer)where
Self: Sized,
Writes the HTML into the provided String
.
fn size_hint(&self) -> usize
Sourcefn into_string(self) -> Stringwhere
Self: Sized,
fn into_string(self) -> Stringwhere
Self: Sized,
Allocates a new String
containing the HTML.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.