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

Change Render and RenderOnce to return Markup instead #48

Closed
lambda-fairy opened this issue Oct 7, 2016 · 0 comments
Closed

Change Render and RenderOnce to return Markup instead #48

lambda-fairy opened this issue Oct 7, 2016 · 0 comments

Comments

@lambda-fairy
Copy link
Owner

lambda-fairy commented Oct 7, 2016

In theory the existing signatures of Render/RenderOnce allow for more efficient code. But in practice we often end up just invoking html! and pushing that to the buffer. If this is the more common use case, then we should have these trait methods return Markup directly instead.

Something like this:

trait Render {
    fn render(&self) -> Markup;

    // what .render() used to be
    fn render_to(&self, buffer: &mut String) {
        buffer.push_str(&self.render());
    }
}

// similarly for RenderOnce

Note that we still need .render_to() to optimize the blanket impl for T: Display, because write!(s, "{}", x) is much faster than x.to_string().

Example:

// shorthand for linking to a stylesheet
struct Stylesheet<S>(pub S);
impl<S: AsRef<str>> Render for Stylesheet<S> {
    fn render(&self) -> Markup {
        html! {
            link rel="stylesheet" type="text/css" href=(self.0.as_ref()) /
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant