-
Hi, I am using fn register_variant(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<Variant>(|reg| {
reg.add_method("info", |_, this: &Variant, key: String| {
let info = this.0.info(key.as_bytes());
info.integer()
.map(|v| {
//let mut t = lua.create_table().expect("error creating table");
match v {
Some(v) => {
// how to return lua table here rather than just the first value?
for val in v.iter() {
return Ok::<LuaValue<'_>, mlua::Error>(Value::Integer(*val));
}
}
None => return Ok(Value::Nil),
}
Ok(Value::Nil)
})
.map_err(|e| mlua::Error::ExternalError(Arc::new(e)))
});
}
} thanks, |
Beta Was this translation helpful? Give feedback.
Answered by
khvzak
Apr 11, 2024
Replies: 2 comments 2 replies
-
bah, I see the first argument to the add_method function is |
Beta Was this translation helpful? Give feedback.
0 replies
-
is this an efficient (in terms of execution speed) way to provide read-only access to a vector of rust values (int this case, reg.add_method("info", |lua: &Lua, this: &Variant, key: String| {
let info = this.0.info(key.as_bytes());
info.integer()
.map(|v| {
let t = lua.create_table().expect("error creating table");
match v {
Some(v) => {
// how to return lua table here rather than just the first value?
for (i, val) in v.iter().enumerate() {
t.raw_set(i + 1, *val).expect("error setting value");
}
Ok::<LuaValue<'_>, mlua::Error>(Value::Table(t))
}
None => Ok(Value::Nil),
}
})
.map_err(|e| mlua::Error::ExternalError(Arc::new(e)))
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would recommend createing a table with preallocated capacity to store N elements in array.
Also more efficient would be just using
lua.create_sequence_from(v)