/// Main `Template` trait; implementations are generally derived /// /// If you need an object-safe template, use [`DynTemplate`]. /// /// ## Rendering performance /// /// When rendering a askama template, you should prefer the methods /// /// * [`.render()`][Template::render] (to render the content into a new string), /// * [`.render_into()`][Template::render_into] (to render the content into an [`fmt::Write`] /// object, e.g. [`String`]) or /// * [`.write_into()`][Template::write_into] (to render the content into an [`io::Write`] object, /// e.g. [`Vec<u8>`][alloc::vec::Vec]) /// /// over [`.to_string()`][std::string::ToString::to_string] or [`format!()`][alloc::format]. /// While `.to_string()` and `format!()` give you the same result, they generally perform much worse /// than askama's own methods, because [`fmt::Write`] uses [dynamic methods calls] instead of /// monomorphised code. On average, expect `.to_string()` to be 100% to 200% slower than /// `.render()`. /// /// [dynamic methods calls]: <https://doc.rust-lang.org/stable/std/keyword.dyn.html> pubtrait Template: fmt::Display + FastWritable { /// Helper method which allocates a new `String` and renders into it. #[inline] #[cfg(feature = "alloc")]
fn render(&self) -> Result<String> { self.render_with_values(NO_VALUES)
}
/// Helper method which allocates a new `String` and renders into it with provided [`Values`]. #[inline] #[cfg(feature = "alloc")]
fn render_with_values(&self, values: &dyn Values) -> Result<String> { letmut buf = String::new(); let _ = buf.try_reserve(Self::SIZE_HINT); self.render_into_with_values(&mut buf, values)?;
Ok(buf)
}
/// Renders the template to the given `writer` fmt buffer. #[inline]
fn render_into<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> Result<()> { self.render_into_with_values(writer, NO_VALUES)
}
/// Renders the template to the given `writer` fmt buffer with provided [`Values`].
fn render_into_with_values<W: fmt::Write + ?Sized>(
&self,
writer: &mut W,
values: &dyn Values,
) -> Result<()>;
/// Renders the template to the given `writer` io buffer. #[inline] #[cfg(feature = "std")]
fn write_into<W: io::Write + ?Sized>(&self, writer: &mut W) -> io::Result<()> { self.write_into_with_values(writer, NO_VALUES)
}
/// Renders the template to the given `writer` io buffer with provided [`Values`]. #[cfg(feature = "std")]
fn write_into_with_values<W: io::Write + ?Sized>(
&self,
writer: &mut W,
values: &dyn Values,
) -> io::Result<()> { struct Wrapped<W: io::Write> {
writer: W,
err: Option<io::Error>,
}
/// Provides a rough estimate of the expanded length of the rendered template. Larger /// values result in higher memory usage but fewer reallocations. Smaller values result in the /// opposite. This value only affects [`render`]. It does not take effect when calling /// [`render_into`], [`write_into`], the [`fmt::Display`] implementation, or the blanket /// [`ToString::to_string`] implementation. /// /// [`render`]: Template::render /// [`render_into`]: Template::render_into /// [`write_into`]: Template::write_into /// [`ToString::to_string`]: alloc::string::ToString::to_string const SIZE_HINT: usize;
}
/// [`dyn`-compatible] wrapper trait around [`Template`] implementers /// /// This trades reduced performance (mostly due to writing into `dyn Write`) for dyn-compatibility. /// /// [`dyn`-compatible]: https://doc.rust-lang.org/stable/reference/items/traits.html#dyn-compatibility pubtrait DynTemplate { /// Helper method which allocates a new `String` and renders into it. #[cfg(feature = "alloc")]
fn dyn_render(&self) -> Result<String>;
/// Helper method which allocates a new `String` and renders into it with provided [`Values`]. #[cfg(feature = "alloc")]
fn dyn_render_with_values(&self, values: &dyn Values) -> Result<String>;
/// Renders the template to the given `writer` fmt buffer.
fn dyn_render_into(&self, writer: &mutdyn fmt::Write) -> Result<()>;
/// Renders the template to the given `writer` fmt buffer with provided [`Values`].
fn dyn_render_into_with_values(
&self,
writer: &mutdyn fmt::Write,
values: &dyn Values,
) -> Result<()>;
/// Renders the template to the given `writer` io buffer. #[cfg(feature = "std")]
fn dyn_write_into(&self, writer: &mutdyn io::Write) -> io::Result<()>;
/// Renders the template to the given `writer` io buffer with provided [`Values`]. #[cfg(feature = "std")]
fn dyn_write_into_with_values(
&self,
writer: &mutdyn io::Write,
values: &dyn Values,
) -> io::Result<()>;
/// Provides a conservative estimate of the expanded length of the rendered template.
fn size_hint(&self) -> usize;
}
impl<T: Template> DynTemplate for T { #[inline] #[cfg(feature = "alloc")]
fn dyn_render(&self) -> Result<String> {
<Selfas Template>::render(self)
}
/// Implement the trait `$Trait` for a list of reference (wrapper) types to `$T: $Trait + ?Sized`
macro_rules! impl_for_ref {
(impl $Trait:ident for $T:ident $body:tt) => { const _: () = { crate::impl_for_ref! { impl<$T> $Trait for [
&T
&mut T
core::cell::Ref<'_, T>
core::cell::RefMut<'_, T>
] $body
}
}; #[cfg(feature = "alloc")] const _: () = { crate::impl_for_ref! { impl<$T> $Trait for [
alloc::boxed::Box<T>
alloc::rc::Rc<T>
alloc::sync::Arc<T>
] $body
}
}; #[cfg(feature = "std")] const _: () = { crate::impl_for_ref! { impl<$T> $Trait for [
std::sync::MutexGuard<'_, T>
std::sync::RwLockReadGuard<'_, T>
std::sync::RwLockWriteGuard<'_, T>
] $body
}
};
};
(impl<$T:ident> $Trait:ident for [$($ty:ty)*] $body:tt) => {
$(impl<$T: $Trait + ?Sized> $Trait for $ty $body)*
}
}
/// Types implementing this trait can be written without needing to employ an [`fmt::Formatter`]. pubtrait FastWritable { /// Used internally by askama to speed up writing some types.
fn write_into<W: fmt::Write + ?Sized>(
&self,
dest: &mut W,
values: &dyn Values,
) -> crate::Result<()>;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.