Skip to content

Commit

Permalink
Merge pull request #199 from Kijewski/pr-dyn-compat
Browse files Browse the repository at this point in the history
"object safe" is now "dyn-compatible"
  • Loading branch information
GuillaumeGomez authored Jan 16, 2025
2 parents 206231b + a4527b8 commit d5c65ab
Showing 1 changed file with 9 additions and 3 deletions.
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

0 comments on commit d5c65ab

Please sign in to comment.