Skip to content

Accessing Scope from within a scoped callback #158

Open
@ElusiveMori

Description

@ElusiveMori

I'm trying to have some userdata with Rc in it. Obviously, the userdata type becomes non-Send, and so cannot be passed to rlua without going through a Scope.

However, I need this data to be returned from within a callback. My flow at the moment is set up sorta like this:

    lua.context(|ctx| {
        ctx.scope(|scp| {
            // all application code basically goes here

            // some_luafn is supposed to be called from within the scope,
            // and returns a callback that yields non-Send userdata
            // this doesn't work at the moment
            fn some_luafn<'lua, 'scope>(scope: &'scope LuaScope<'lua, 'scope>) -> LuaFunction<'lua> {
                scope.create_function_mut(move |ctx: LuaContext, _: ()| {
                    let result = some_method_returns_userdata(data, ext).unwrap();
                    let userdata: AnyUserData<'lua>  = scope.create_static_userdata(result).unwrap();

                    Ok(userdata)
                }).unwrap()
            }


        })
    });

I can't for the life of me figure out the proper lifetimes of some_luafn. I'm not even sure if this use-case is supported.

So, in this issue, I'd like to ask the following two questions:

  1. Is this a supported use-case, and if so, how does it work?
  2. If not, can it be supported, for example by providing a reference to Scope within callbacks created from Scope just like there is a reference to Context already? I believe there should be no soundness issues arising from this, as the callbacks created by Scope don't live longer than Scope itself anyway.

On a slightly related tangent:
Would it be reasonable to provide a version of rlua without the Send requirements all over the place? It seems like a rather niche feature to be able to Send the Lua state, yet it enforces a whole myriad of inconveniences all over the place, despite the fact that Lua itself is single-threaded. At the very least, it feels like the Scope API is somewhat limited at the moment.

I could potentially look into making a PR/fork without the Send requirements if there are no other caveats to this other than wanting to make the Lua struct Sendable.

Thanks for reading this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Future workAn issue which would be nice to do, but not one of the current priorities.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions