Skip to content
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

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 12 additions & 5 deletions rinja/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ impl<T: Template + ?Sized> Template for &T {
const MIME_TYPE: &'static str = T::MIME_TYPE;
}

/// Object-safe wrapper trait around [`Template`] implementers
/// [`dyn`-compatible] wrapper trait around [`Template`] implementers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// [`dyn`-compatible] wrapper trait around [`Template`] implementers
/// [`dyn`-compatible] wrapper trait around [`Template`] implementors.

In rust docs it's "implementors" so I suppose it's the correct word. ^^'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thank you for proofreading!

(I'm like 99% sure that in English you are supposed to role a dice when to write -ter and when to write -tor. :D)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and let's not even try to speak english, the pronunciation is even more random. 😆

///
/// 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/nightly/reference/items/traits.html#dyn-compatibility>
pub trait DynTemplate {
/// Helper method which allocates a new `String` and renders into it
fn dyn_render(&self) -> Result<String>;
Expand All @@ -191,29 +193,34 @@ pub trait DynTemplate {
}

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

#[inline]
fn dyn_render_into(&self, writer: &mut dyn fmt::Write) -> Result<()> {
<Self as Template>::render_into(self, writer)
Self::render_into(self, writer)
}

#[inline]
fn dyn_write_into(&self, writer: &mut dyn io::Write) -> io::Result<()> {
<Self as Template>::write_into(self, writer)
Self::write_into(self, writer)
}

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

#[inline]
fn mime_type(&self) -> &'static str {
Self::MIME_TYPE
}
}

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