mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Allow chunk to take variables implementing `AsChunk`

Open ModProg opened this issue 3 years ago • 6 comments

This would allow composition of chunks:

let a = chunk!{...}
let b = chunk!{... $a ...}

ModProg avatar Dec 27 '22 19:12 ModProg

This would mirror the quote crate for rust syntax

ModProg avatar Dec 27 '22 19:12 ModProg

PR is welcome :) It's quite untrivial change.

khvzak avatar Jan 02 '23 15:01 khvzak

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

ModProg avatar Jan 03 '23 00:01 ModProg

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:

  1. Owned and constructed by either using format or concatination: format!("... {a} ...", a=a.to_source()).into_bytes()
  2. 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<()>;
}

ModProg avatar Jan 04 '23 14:01 ModProg

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.

ModProg avatar Jan 04 '23 14:01 ModProg

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....

lenscas avatar Mar 01 '23 11:03 lenscas