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

Can't have single use renderables #24

Closed
Nemo157 opened this issue Jan 20, 2016 · 4 comments
Closed

Can't have single use renderables #24

Nemo157 opened this issue Jan 20, 2016 · 4 comments

Comments

@Nemo157
Copy link
Contributor

Nemo157 commented Jan 20, 2016

Since Render takes itself by reference anything that implements it needs to support re-rendering multiple times. While working on supporting modified event streams in (second example at maud-pulldown-cmark) I found this limiting as pulldown_cmark::Parser isn't reusable, requiring me to wrap stuff up in closures to allow re-creating the parser when necessary. In most cases it's very unlikely that these structs would be kept around, so I was wondering if it were possible to have fn render(self, w: &mut Write) -> fmt::Result and provide implementations for references where needed.

@Nemo157
Copy link
Contributor Author

Nemo157 commented Jan 20, 2016

To see whether this would work I attempted to implement it (https://git.nemo157.com/forks/maud/commit/f1ecd07). Took a while to work out how to get it working, but finally realised that emulating the Fn, FnOnce hierarchy would work, so in my code I could just impl RenderOnce and consume the Parser while rendering. If you want this I could open a pull request (+ RenderMut maybe, might be useful for some other advanced renderable?).

@Nemo157
Copy link
Contributor Author

Nemo157 commented Jan 20, 2016

I threw together an implementation of RenderMut and some better tests for both (64565e8).

@lambda-fairy
Copy link
Owner

Cheers for the report. I agree that having a way to render a value by-move would be useful. But I'm hesitant about making that the default. After all, println!("{}", x) borrows x rather than moving it.

As for RenderMut: do you have a use case for this trait? I don't want to add it in unless there's a real use for it.

@Nemo157
Copy link
Contributor Author

Nemo157 commented Feb 2, 2016

I can't think of any real use case for RenderMut. I just remembered that it will still default to borrowing a value if it implements Render because of the existence of impl<'a, T: Render> RenderOnce for &'a T and calling the method via use ::maud::RenderOnce; $expr.render(&mut *$w). The compiler will auto-ref $expr till it finds the implementation of RenderOnce for &$expr then call that. I'll add a test that shows that in my changes for #26.

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

2 participants