sunng87 / handlebars-rust

Rust templating with Handlebars
MIT License
1.26k stars 137 forks source link

Async Io / Tokio Support #436

Open DrSloth opened 3 years ago

DrSloth commented 3 years ago

Hi, I am currently working on an async IoT web app and use this crate. I often have changes in templates / user defined templates and often have to reload them. Currently, all io, such as reading and writing to disk, can only be implemented blocking from what I have seen. Is async IO / support for Tokio planned, would you accept a PR for simple async IO if it is behind a feature flag? Or is there a way to implement async rendering / writing that I am missing?

Thanks in advance and keep up the good work!

sunng87 commented 3 years ago

Yes, a PR for async is welcomed. Please create it against our latest master branch because 4.0 has a new concept called TemplateSource. Let me know if you need help with the PR.

DrSloth commented 3 years ago

Sorry for the late answer. I am going to work on this as soon as i have time. I am going to comment here again if any questions arise.

DrSloth commented 2 years ago

Hello. I am sorry i forgot about this for some while and revisited it last weekend. I couldn't find a way where async is really benficial here, as far as i can see the compiler is not really made for "streaming" parsing and compiling. Compiling a template is obviously cpu bound and for some benefit the compiler would need to handle "steams" of data rather than directly parsing a string. The read can be made async by directly using the functions which parse from string and using something like tokio::fs::read_to_string. Maybe i am missing something but i don't know how this could be generically added while also yielding real benefit without rewriting large parts of the parser/compiler logic.

Maybe someone else has an idea where async is beneficial and they, me or we can take a look at it again.

Diegovsky commented 2 years ago

You would have to rewrite the parser to work with streams. Maybe using a buffer so it can go back a few bytes or forwards while parsing. I unfortunately don't have much time (and expertise) otherwise I would open a PR :(