Allow chunk to take variables implementing `AsChunk`
This would allow composition of chunks:
let a = chunk!{...}
let b = chunk!{... $a ...}
This would mirror the quote crate for rust syntax
PR is welcome :) It's quite untrivial change.
I would imagine, I think the only way I see is to implement AsChunk for everything that implements ToLua. But not sure if that might conflict anywhere
Not sure if the AsChunk trait is the right one to use.
Currently, what the chunk macro is doing, is taking all captures and putting them in the environment, but to allow for supporting compositing chunks, for every capture we need to do the following:
insert it in the source, this means the source needs to be:
- Owned and constructed by either using format or concatination:
format!("... {a} ...", a=a.to_source()).into_bytes() - put it's values in the env:
a.insert_variables_into_env(env)?
So the new trait would need
trait InnerChunk {
fn to_source(&self, variable_name: String) -> String {
variable_name
}
fn insert_variables_into_env(&self, variable_name: String, &mut LuaTable) -> Result<()>;
}
Though, I'm unsure about the ownership/lifetimes of everything.
Chunks created by chunk! would implement both traits.
And IntoChunk would have to be implemented for T: ToLua and just insert itself into the env.
fn insert_variables_into_env(&self, variable_name: String, &mut LuaTable) -> Result<()>;
What if the 2 chunks capture variables with the same name but with different values(or even types?). Might want to think of some way to make sure that the 2 chunks will not conflict when they are combined together....