-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 Template::custom() take an FnOnce? #1064
Comments
What is the nature of EDIT: Hit the button too fast. |
In my specific case, it's a list of localisation bundles. I would like to handle loading them and then handle any errors outside of the closure, then move the vector inside the closure when appropriate. That vector gets moved to another function, which creates a Tera function for producing localised messages, referencing that vector for all queries. The localisation bundles aren't |
That sounds... messy. I would suggest sprinkling a few If loading the bundle is relatively cheap or you're willing to sacrifice that time for each template reload in debug mode, you might load the bundle in the |
It's really not messy. fn main() {
let bundles = init_bundles();
rocket::ignite()
.attach(Template::custom(move |engines| {
engines.tera.register_function("tr", create_tera_function(bundles));
})
.launch();
}
fn create_tera_function(bundles: Bundles) -> Box<tera::GlobalFn> {
Box::new(move |args| {
// ...
})
} That's essentially how it is. I don't think that's messy. However, that code is impossible because of Loading the bundles is cheap enough to do in the |
I think that might actually be the key issue here. Would this issue also be addressed if The only real alternative I can think of on |
Yeah, I agree it's a tough spot for Rocket. Could it be possible to propagate the error every time the templates reload? Or do you mean it would be like when the templates fail to reload currently, where it just uses the last successfully-loaded templates and displays an error. That would be fine.
|
If we made this change, I imagine we would do the following:
|
Yeah, so it would be similar to these lines. If so, that all sounds good to me and gets the desired behaviour I was looking for. :) |
@jebrosen Will you be taking a second look at this? |
Whoops! Didn't mean to close. |
Rocket version: 0.4.2
Steps I've taken to answer the question myself: looked at the source and mentally examined the needs of such a closure.
Documentation I believe should include an answer: none.
Basically, see title. Is there a need for
Template::custom()
to takeFn
instead ofFnOnce
?I would like to initialise some values and do error handling outside of the closure, then move a value inside the closure and use it there. Unfortunately, this is not possible, since the type is not
Copy
norClone
.This is currently impossible due to the constraints of
Fn
.If
Template::custom()
can take anFnOnce
, I'd be happy to PR such a change. :)The text was updated successfully, but these errors were encountered: