-
Notifications
You must be signed in to change notification settings - Fork 117
Contextify #98
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
Contextify #98
Conversation
As a side effect, would fix #97, which is the actual driving motivation behind this change. Adds compiletest_rs tests for context lifetime invariance, and all the tests and compiletest_rs tests pass, but the documentation and doctests are *all* screwed up. There are some outstanding API design questions, but the strategy SEEMS to actually work.
There's one thing that was possible before this change that will no longer be possible, and that's mixing outer handles with inner handles from a let lua = Lua::new();
let table = lua.create_table().unwrap();
lua.scope(|scope| {
scope
.create_function_mut(|lua, ()| {
// this will fail, `lua` and `table` unique lifetimes are different
lua.globals().set("t", table.clone()).unwrap();
Ok(())
}).unwrap()
.call::<_, ()>(())
.unwrap();
});
assert_eq!(
lua.globals()
.get::<_, Table>("t")
.unwrap()
.get::<_, String>("a")
.unwrap(),
"b"
); Somewhat confusingly, you CAN still do this on Lua::new().context(|lua| {
let table = lua.create_table().unwrap();
lua.scope(|scope| {
scope
.create_function_mut(|_, ()| {
// Fine, because only one lifetime is involved
table.set("a", "b").unwrap();
Ok(())
}).unwrap()
.call::<_, ()>(())
.unwrap();
});
assert_eq!(table.get::<_, String>("a").unwrap(), "b");
}); which is to say, that you can still ofc access outer variables inside a scope callback like you would expect, there's just no way to say that the two You COULD imagine a The reason this is not safe has to do with coroutines. However, if we used the outer This situation would actually be basically the same if I had fixed the unsoundness in #97 in any of the other ways that I mentioned, this is not unique to this particular PR. The root unsoundness problem is how loose the The only alternative that I can come up, which is also potentially conceptually simpler, is to have a single Right now, I don't have a huge motivation to do this. I think this is an extreme corner case, and it is almost always a better idea to use If anybody disagrees with this and thinks that the pattern above is super useful to them, please let me know. If you think that the alternative lifetime definition makes |
I'm going to merge this, I haven't been able to come up with any better ideas and haven't thought of any terrible problems with it. This still needs more work w.r.t. documentation etc, but it can at least sit in |
Fixes #97 and the only current API panic condition
HUGE API change, the entire previous API has been moved to a context callback to take advantage of generative lifetimes. This both fixed a soundness but and removes an API wart, but requires callbacks to use.