mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Expose Scope::new to allow finer control over objects lifetimes

Open buffet opened this issue 2 years ago • 0 comments

I have a struct with a few members I am also exposing in my Lua API.

At the moment I wrap the struct member in an Rc, though this is technically incorrect since wgpu safety requires my window to outlive surfaces.

struct Example {
    foo: Foo,
    bar: Bar, // must outlive foo
}

I think something like this would be quite nice:

struct Example {
    scope: mlua::Scope,
    foo: Foo,
    bar: Bar, // must outlive foo and scope
}

lua.register_userdata_type::<EventLoopWindowTarget<()>>(|reg| {
    reg.add_field_method_get("foo", |_, this| {
        this.scope.create_any_userdata_ref(&this.foo)
    });
})?;

buffet avatar Nov 15 '23 16:11 buffet