-
Notifications
You must be signed in to change notification settings - Fork 113
Accessing Scope from within a scoped callback #158
Description
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:
- Is this a supported use-case, and if so, how does it work?
- If not, can it be supported, for example by providing a reference to
Scopewithin callbacks created fromScopejust like there is a reference toContextalready? I believe there should be no soundness issues arising from this, as the callbacks created byScopedon't live longer thanScopeitself 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!