mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Difficulty creating a wrapper type

Open nickeb96 opened this issue 1 year ago • 1 comments

This is more of a question than a bug, but I'm trying to get the following simplified example to work:

struct LuaVec<'lua>(pub Vec<Value<'lua>>);

impl<'lua> UserData for LuaVec<'lua> {
  fn add_methods<'lua2, M: UserDataMethods<'lua2, Self>>(methods: &mut M) {
    methods.add_method_mut("push", |_, this, value: Value<'lua2>| {
      this.0.push(value);
      Ok(())
    });
  }
}

I've tried a few different variations of having one lifetime depend on the other, but everything I've tried gives me error[E0276]: impl has stricter requirements than trait.

Is it possible with the current userdata api to implement UserData on a type that already has lua values in it? I realize that the two lifetimes need to be the same, but I can't figure out how to represent that and still implement the trait properly.

nickeb96 avatar May 09 '24 22:05 nickeb96

There are few options how to solve this:

  1. Store Lua values in userdata using set_nth_user_value api (recommended)

  2. Store Lua values in Registry using crate_registry_value api and then store generated RegistryKey in userdata.

khvzak avatar May 12 '24 22:05 khvzak