From 22b6d218bdb6a5aafc0efd4fb80a67d2c796d0e7 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Mon, 19 Feb 2024 23:12:02 -0500 Subject: [PATCH] Implement FromLuaVec for bool, add AnyUserdata::is for checking userdata type --- src/userdata/any.rs | 19 +++++++++++++++++++ src/value.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/userdata/any.rs b/src/userdata/any.rs index 52efeec..ce37102 100755 --- a/src/userdata/any.rs +++ b/src/userdata/any.rs @@ -90,6 +90,25 @@ impl<'a> AnyUserdata<'a> { } } + /// Returns a boolean indiciating if this Userdata is of type `T`. + pub fn is(&self) -> crate::Result { + unsafe { + let _g = StackGuard::new(self.state); + let s = self.state.state_ptr(); + + self.lref.push_to_lua_stack(self.state)?; + + if lua::lua_getmetatable(s, -1) == 0 { + return Err(crate::Error::MissingMetatable); + } + + self.state.get_userdata_metatable::() + .push_to_lua_stack(self.state)?; + + Ok(lua::lua_rawequal(s, -2, -1) == 1) + } + } + /// Returns the name of the userdata by accessing the metatable pub fn name(&self) -> crate::Result { unsafe { diff --git a/src/value.rs b/src/value.rs index 0b87801..8d943ff 100755 --- a/src/value.rs +++ b/src/value.rs @@ -378,6 +378,18 @@ impl<'a> ValueVec<'a> { Ok(ValueVec(vec)) } } + + /// Returns none if there aren't enough values in self to fulfil T + pub fn try_into_vals>(self, lua: &'a State) -> crate::Result> { + if let Some(v) = T::value_num() { + if v > self.len() { + return Ok(None); + } + } + + T::from_lua_value_vec(lua, self) + .map(|t| Some(t)) + } } impl<'a> From> for ValueVec<'a> { @@ -481,6 +493,7 @@ impl_from_lua_vec_for_from_lua!(u32); impl_from_lua_vec_for_from_lua!(f64); impl_from_lua_vec_for_from_lua!(f32); impl_from_lua_vec_for_from_lua!(String); +impl_from_lua_vec_for_from_lua!(bool); impl_from_lua_vec_for_from_lua!(Table, 'a); impl_from_lua_vec_for_from_lua!(Value, 'a); @@ -630,7 +643,7 @@ macro_rules! impl_from_lua_vec_tuple { if values.len() != 1 { return Err(crate::Error::IncorrectArgCount { arg_expected: 1, - arg_count: $count, + arg_count: values.len() as i32, }); } @@ -677,6 +690,20 @@ impl<'a, T: AsLua<'a>, const N: usize> AsLua<'a> for [T; N] { } } +impl<'a, T: FromLua<'a>, const N: usize> FromLua<'a> for [T; N] { + fn from_lua(lua: &'a State, val: Value<'a>) -> crate::Result { + let tyname = val.type_name(); + + if let Some(table) = val.as_table() { + //table.get(key) + } else { + // Error::type_mismatch("array/table", &tyname) + } + + todo!() // TODO + } +} + // TODO: Find out a way to implement this /* impl<'a, T: FromLua> FromLuaVec<'a> for T { fn from_lua_value_vec(state: &'a State, values: ValueVec<'a>) -> crate::Result {