Comments (5)
So I ended up being able to do this, despite initially being convinced I was going to run into problems. This is actually how LuaCallback was defined before, and I have since changed it, but I don't believe I was thinking about it clearly.
This was the relevant discussion I had about this before, and I now am starting to think I was thinking about it incorrectly. I actually do STILL have some boxed functions with approximately this signature:
Box<for<'lua, 'a> Fn(&'lua Lua, &'a C, LuaMultiValue<'lua>) -> LuaResult<LuaMultiValue<'lua>>>
I think the part that I was missing before is that calling these functions is obviously compatible with the new looser lifetime bounds on callbacks, and I don't HAVE to make these the same signature as the ones stored inside Lua.
This is actually pretty significant, because it allows us possibly to go back to having automatic type deduction on callbacks rather than having to call lua.pack / lua.unpack all the time. I might make a pull request about this soon.
from rlua.
You can use an Rc<RefCell<House>>
. Note that the userdata API provides a nicer wrapper around this so you might want to consider using that instead.
Now, I wonder whether it would be okay to accept closures with a different lifetime than 'static
, provided they still outlive the Lua
instance.
For reference, create_function
looks like this:
fn create_function<F>(&self, func: F) -> LuaFunction
where
F: 'static + for<'a> FnMut(&'a Lua, LuaMultiValue<'a>) -> LuaResult<LuaMultiValue<'a>>
Could we change it so that the closure only has to outlive the Lua
instance? Like this:
fn create_function<'lua, F>(&'lua self, func: F) -> LuaFunction<'lua>
where
F: 'lua + for<'a> FnMut(&'a Lua, LuaMultiValue<'a>) -> LuaResult<LuaMultiValue<'a>>
On that note, we could also lift the for<'a> FnMut
bound to be FnMut(&'lua Lua ...
. That way, the closure doesn't have to work with any lifetime 'a
, but only with the specific (known) lifetime of the Lua
object.
from rlua.
Moving a lot of the 'static lifetimes to 'lua is a good suggestion, and I'll take a look at maybe doing that this weekend sometime. There's a lot of hairiness around the type of callbacks, and I'm not positive what you're saying about giving the parameters the lifetime 'lua is possible, but I don't remember the specifics right now. I'll have better answers once I have a chance to take a look at it.
from rlua.
I'm sorry if I closed this issue prematurely, I realized later that it might not have been polite to do that without asking. 5b723d5 solves this partially but arguably not completely, your original example would not necessarily compile, since the function you create may outlive the variable it closes over (any closed over variables actually need a lifetime longer than Lua).
Feel free to reopen if you still find this to be an issue. I have some poorly thought out ideas about creating scope limited wrapper types that may help someday.
from rlua.
This is actually unfixed by 721ffc4, feel free to reopen this issue, though I'm not sure there's a fix for it anymore, or at least not an easy one.
from rlua.
Related Issues (20)
- Possible bug with __newindex not called on numeric indexing HOT 4
- Question: How to exec multiple things at once? HOT 4
- Segfault on clone of #[repr(C)] userdata subfield HOT 9
- ios build error HOT 7
- examples for error handling HOT 2
- Example for eval return a struct? HOT 1
- Cannot load DLLs in v0.19 HOT 6
- Missing mutable borrows for Table<'lua> HOT 2
- Context.create_sequence_from should support references HOT 1
- Undefined symbol lua_checkstack HOT 8
- Crash on empty source HOT 2
- custom runtime path HOT 3
- creating a `require`-able module HOT 8
- [Request] UserData::from_lua HOT 1
- Suggestion: proc_macro for LuaUserDataMethods::add_method HOT 3
- No documentation on how to use LuaRocks with rlua HOT 3
- Rlua + Luarocks Modules -> Fail to load HOT 3
- Memory leak when using system-luajit HOT 2
- Merge with mlua HOT 32
- [Typo] Example says laoded(instead of loaded) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rlua.