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

Rendering a template requires it to live for as long as the registry #529

Closed
lovasoa opened this issue Sep 12, 2022 · 7 comments · Fixed by #532
Closed

Rendering a template requires it to live for as long as the registry #529

lovasoa opened this issue Sep 12, 2022 · 7 comments · Fixed by #532

Comments

@lovasoa
Copy link
Contributor

lovasoa commented Sep 12, 2022

Currently the types in this crate allow Template objects to live outside of the registry, but the lifetime annotations of the render method make it very hard to actually implement a system like that.

Is there a reason why a template that lives shorter than the registry where partials are stored couldn't be rendered?

I would need that for sqlpage

@sunng87
Copy link
Owner

sunng87 commented Sep 12, 2022

IIRC there is no particular reason for that just because it was assumed that all templates are managed by the registry. Do you have an example that I can work on to verify?

For now you can use render_template to render it directly from string with a little cost of parsing. Actually in render_template's implementation, template lives short than the registry.

@lovasoa
Copy link
Contributor Author

lovasoa commented Sep 12, 2022

I need this because I am handling my own customized template cache, so re-parsing the templates every time would make the whole thing useless. Would it be hard to remove the 'reg lifetime on self in Renderable::render ?

@sunng87
Copy link
Owner

sunng87 commented Sep 13, 2022

It should not be hard. But it can be a break change by introducing additional lifetime specifier. I'm going to do some experiment to see if it works for your case.

@lovasoa
Copy link
Contributor Author

lovasoa commented Sep 13, 2022

Thank you very much ! Don't hesitate to share your progress so that I can test it in SQLPage.
And do we need to add a new lifetime ? Couldn't we have a simpler signature like

    fn render<'reg: 'rc, 'rc>(
-        &'reg self,
+        &self,
        registry: &'reg Registry<'reg>,
        ctx: &'rc Context,
        rc: &mut RenderContext<'reg, 'rc>,
        out: &mut dyn Output,
    ) -> Result<(), RenderError>

@sunng87
Copy link
Owner

sunng87 commented Sep 15, 2022

Removing lifetime won't work because some constants from template are passed within rendering call chain, which requires explicit lifetime. The currently design assumes all templates are stored within registry so data in templates shares same lifetime of registry.

An alternative approach for your problem is to create multiple registries and manage them with your strategy.

@lovasoa
Copy link
Contributor Author

lovasoa commented Sep 15, 2022

I do need a single registry, with all the associated helpers, but need to store templates in my own data structure. Creating a new registry with all the associated helpers on every render would be a huge overhead !
Unsafely transmuting the lifetimes just works and doesn't cause any memory issue, which seems to indicate that it is indeed possible to relax the lifetime requirements, at least in my case. Does the registry actually store borrowed information from the template being rendered ? If not, the lifetime requirement is too strict.

@lovasoa
Copy link
Contributor Author

lovasoa commented Sep 15, 2022

Would it be possible to at least have the requirement that the template lives longer than the context 'rc, but not the registry 'reg ?

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

Successfully merging a pull request may close this issue.

2 participants