mlua-rs / mlua

High level Lua 5.4/5.3/5.2/5.1 (including LuaJIT) and Roblox Luau bindings to Rust with async/await support
Other
1.68k stars 135 forks source link

Lifetime Issues with Scope #405

Open BenjaminSchaaf opened 5 months ago

BenjaminSchaaf commented 5 months ago

Some trivially valid examples are currently impossible due to lifetime annotations:

// This works fine
let mut i = 0;
lua.scope(|scope| {
    scope.create_any_userdata_ref_mut(&mut i);
});

// This fails because the type must be 'static
struct Wrapper<'a>(&'a mut i32);
let mut w = Wrapper(&mut i);
lua.scope(|scope| {
    scope.create_any_userdata_ref_mut(&mut w)
});

scope.create_any_userdata_ref_mut should not require T: 'static. The only workaround I've found is to transmute the parameter.

khvzak commented 5 months ago

Try Scope::create_nonstatic_userdata. It's slower alternative that does not require T: 'static.

BenjaminSchaaf commented 5 months ago

That does work, but creating a new metaclass for almost every single function call is not a tenable situation.

khvzak commented 5 months ago

Unfortunately non-'static types don't implement Any trait which is the main blocker.