Closed SnirkImmington closed 7 years ago
If you're talking about https://github.com/tomaka/hlua/blob/master/hlua/src/any.rs#L30 Then it's because of a bug in rustc. Maybe it has been fixed now.
Would there also be runtime ways (i.e. a for loop, etc.) of doing it? I haven't looked at the code a whole lot but I'd be willing to give it a go, even if this bug still exists there should be a way around it.
Well, it is the runtime way, as the actual content of the array is unknown. The compiler bug prevented it from working.
Is this compiler bug known and/or fixed?
I'm not sure this is a compiler bug? Since one variant of AnyLuaValue
contains a Vec<(AnyLuaValue, AnyLuaValue)>
, calling push_to_lua
on that vector triggers an error related to polymorphic recursion. I think. I'm tinkering with it now...
That code alone works: https://is.gd/rFFNLf
So I guess the problem has been fixed in rustc.
However, un-commenting this line and running cargo test
still gives the following error:
error: reached the recursion limit while instantiating `<hlua::any::AnyLuaValue as hlua::Push<L>><&mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut hlua::Lua>::push_to_lua`
😞
So the problem is that in order to push a Vec
on a L
, each element of the Vec needs to be pushed individually on the L
.
But we can't just push elements on the L
because pushing consumes the L
. You could only push a single element and then the borrow checker would complain.
Instead the where clause for Vec
pushing requires that elements of the Vec
must be pushable on &mut L
, so that pushing each element consumes a &mut L
and not a L
.
So far so good.
Unfortunately the consequence is in order to push a Vec<(AnyLuaValue, AnyLuaValue)>
on a L
we need to call the function that pushes a AnyLuaValue
on a &mut L
. But if that AnyLuaValue
contains a Vec<(AnyLuaValue, AnyLuaValue)>
, then we need to call the function that pushes a AnyLuaValue
on a &mut &mut L
. But if that AnyLuaValue
contains a Vec, then we need to call the function that pushes a AnyLuaValue
on a &mut &mut &mut L
. And so on.
This is why we're reaching the recursion limit.
Unfortunately I don't see any obvious fix for now.
Fixed in #106
I have a program that uses
AnyLuaValue
s a lot. We have a data store that the Rust and Lua parts share back and forth across, and the easiest way to share data back and forth was throughAnyLuaValue
s.I'd written traits
ToTable
andFromTable
for the conversions (using move semantics to avoid copies):This is also a great way to store structured data (like a slimmed-down JSON) and it made it easy to provide compatibility without using userdata,
&mut Lua
orPushGuard<L>
orAsMutLua
. I've even got a macro to generate a struct with these implementations automatically.However, pushing and reading
AnyLuaData::LuaArray
is unsupported right now, with a little comment that mentions hitting a stack limit. Is there a way to implement it?