Skip to content

"object safe" is now "dyn-compatible" #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions rinja/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ impl<T: Template + ?Sized> Template for &T {
const SIZE_HINT: usize = T::SIZE_HINT;
}

/// Object-safe wrapper trait around [`Template`] implementers
/// [`dyn`-compatible] wrapper trait around [`Template`] implementers
///
/// This trades reduced performance (mostly due to writing into `dyn Write`) for object safety.
/// 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
pub trait DynTemplate {
/// Helper method which allocates a new `String` and renders into it
#[cfg(feature = "alloc")]
Expand All @@ -200,11 +202,13 @@ pub trait DynTemplate {
}

impl<T: Template> DynTemplate for T {
#[inline]
#[cfg(feature = "alloc")]
fn dyn_render(&self) -> Result<String> {
<Self as Template>::render(self)
}

#[inline]
fn dyn_render_into(&self, writer: &mut dyn fmt::Write) -> Result<()> {
<Self as Template>::render_into(self, writer)
}
Expand All @@ -215,12 +219,14 @@ impl<T: Template> DynTemplate for T {
<Self as Template>::write_into(self, writer)
}

#[inline]
fn size_hint(&self) -> usize {
Self::SIZE_HINT
<Self as Template>::SIZE_HINT
}
}

impl fmt::Display for dyn DynTemplate {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.dyn_render_into(f).map_err(|_| fmt::Error {})
}
Expand Down
Loading