Implement FromLuaVec for bool, add AnyUserdata::is for checking userdata type
ci/woodpecker/push/debug Pipeline was successful Details

This commit is contained in:
SeanOMik 2024-02-19 23:12:02 -05:00
parent d32c138e99
commit 22b6d218bd
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
2 changed files with 47 additions and 1 deletions

View File

@ -90,6 +90,25 @@ impl<'a> AnyUserdata<'a> {
}
}
/// Returns a boolean indiciating if this Userdata is of type `T`.
pub fn is<T: Userdata + 'static>(&self) -> crate::Result<bool> {
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::<T>()
.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<String> {
unsafe {

View File

@ -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<T: FromLuaVec<'a>>(self, lua: &'a State) -> crate::Result<Option<T>> {
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<Value<'a>> 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<Self> {
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<Self> {