mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Lifetime Issues with Scope

Open BenjaminSchaaf opened this issue 9 months ago • 3 comments

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.

BenjaminSchaaf avatar May 11 '24 14:05 BenjaminSchaaf

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

khvzak avatar May 12 '24 22:05 khvzak

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

BenjaminSchaaf avatar May 13 '24 03:05 BenjaminSchaaf

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

khvzak avatar May 13 '24 11:05 khvzak