-
Notifications
You must be signed in to change notification settings - Fork 116
Issues with 'static bounds on globals etc. #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The 'lua lifetime you're referring to is an invariant lifetime produced by the What you're asking for already exists as part of the scope system, it's just arguably less ergonomic in the situation where the parent We tried to make this easier in the era before Since then, the situation has actually gotten more complicated. The lifetimes in The lua globals table doesn't factor into it, once a value is inside Lua everything has an indefinite lifetime, you can take values you create from a
It might be safe (as in, not exhibit UB) for you if you make sure that the t lives longer than lua, but it would be unsound for rlua to provide the ability to do that safely, since you could write safe code that uses it that exhibits UB. |
Thanks for the reply!
Ahh that's a misinterpretation on my part, I was aiming for for the lifetime of the Lua object rather than for the lifetime of the context object, I hadn't considered the strictly outlive difficulty, that's the interesting one.
This was enough of a clue that I worked out that you have to feed the scope method output into the globals methods, so the following totally works: fn test(a: &Test) {
// Create lua env
let lua = Lua::new();
lua.context(|ctx| {
ctx.scope(|scope| {
let globals = ctx.globals();
scope.create_nonstatic_userdata(a);
});
});
} Which is pretty much exactly what I wanted, but I hadn't worked this out from the docs / guided tour. I'd be happy to open a PR with some doctest examples on the scope methods to make their use more obvious if you're interested?
Oh yes this was a horrendous fix to a problem that I couldn't see a way around, I'm glad there is an actual mechanism that I had just not understood, and thanks for your help! |
Yeah sure, that would be helpful! There's a very small bit about it in the docs for (Edit: That documentation is also confusing, because there are too many definitions of the word "lifetime" there) |
And yeah, you figured it out obviously, but I probably should have started by actually just showing you that you could do that rather than talking about it abstractly, sorry for not being more clear! |
Hey hi, thanks for making a neat thing!
I've been playing with trying to pass references in to the lua environment and running up against the
'static
bound limitations, and wondering whether there's a reason they are required, whether there's a convenient workaround i am missing, if they could be relaxed to'lua
to let the compiler manage them appropriately (which seems to me to be viable?).As a super simple example, you can't really use references because
&a
is not'static
As far as I can see, so long as the lifetime of a exceeds
'lua
this should be safe, aslua
is known to drop beforea
? And if not (as it is currently), it'd be neat to have some documentation as to why and how to safely get around this limit with dynamic objects (i guess being able to add globals using scopes might be viable?).This becomes even more complex when you have types with internal references, the only solution I have found is to transmute from local to
'static
lifetimes (let t = std::mem::transmute::<Test<'a>, Test<'static>>(t)
) on the basis that the object is known to live longer than the lua lifetime, however, if relaxing the bound to'lua
is unsound then this approach would also be.The text was updated successfully, but these errors were encountered: