Question: Is MetaMethod::IPairs missing for lua54? #636
-
I think I'm missing something. I'm using the I can do that for Here is an example I would like to create: methods.add_meta_method(MetaMethod::IPairs, |_, _, ()| -> Result<()> {
Err(Error::RuntimeError("attempt to iterate over ...".into()))
}); What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Lua 5.4 does not support You can use struct MyUserData(i32);
impl LuaUserData for MyUserData {
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::Index, |_lua, this, k: i32| {
if k < this.0 {
return Ok(Some(k * k));
}
Ok(None)
});
}
}
lua.load(
r#"
my_ud = ...
for i, x in ipairs(my_ud) do
print(i, x)
end
"#,
)
.call::<()>(MyUserData(5))
.unwrap(); or simply provide your own iterator function. If you want to remove |
Beta Was this translation helpful? Give feedback.
-
@khvzak Perfect. This is what I was missing. Thank you so much for this precise answer. I have everything I need. Also, thanks for this robust and complete Lua integration. |
Beta Was this translation helpful? Give feedback.
Lua 5.4 does not support
__ipairs
metamethods (it was removed since 5.3).You can use
__index
instead:or simply provide your own iterator function.
If you want to remove
ipairs
support, then your__index
should return a error for …