from_lua macro

This commit is contained in:
Quaternions 2024-10-03 18:53:17 -07:00
parent 9d219004f4
commit 5a1df16bd9
7 changed files with 22 additions and 56 deletions

View File

@ -76,11 +76,5 @@ impl mlua::UserData for CFrame{
); );
} }
} }
impl<'lua> mlua::FromLua<'lua> for CFrame{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{ type_from_lua_userdata!(CFrame);
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected CFrame got {:?}",other))),
}
}
}

View File

@ -40,11 +40,4 @@ impl mlua::UserData for Color3{
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
} }
} }
impl<'lua> mlua::FromLua<'lua> for Color3{ type_from_lua_userdata!(Color3);
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Color3 got {:?}",other))),
}
}
}

View File

@ -39,14 +39,7 @@ impl mlua::UserData for EnumItem{
}); });
} }
} }
impl<'lua> mlua::FromLua<'lua> for EnumItem{ type_from_lua_userdata!(EnumItem);
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}
impl mlua::UserData for EnumItems{ impl mlua::UserData for EnumItems{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
@ -61,14 +54,7 @@ impl mlua::UserData for EnumItems{
}); });
} }
} }
impl<'lua> mlua::FromLua<'lua> for EnumItems{ type_from_lua_userdata!(EnumItems);
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}
impl mlua::UserData for Enum{ impl mlua::UserData for Enum{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
@ -76,11 +62,4 @@ impl mlua::UserData for Enum{
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(_methods:&mut M){ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(_methods:&mut M){
} }
} }
impl<'lua> mlua::FromLua<'lua> for Enum{ type_from_lua_userdata!(Enum);
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}

View File

@ -54,14 +54,7 @@ macro_rules! class{
self.referent self.referent
} }
} }
impl<'lua> mlua::FromLua<'lua> for $class{ type_from_lua_userdata!($class);
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->mlua::Result<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($class),other))),
}
}
}
}; };
} }
macro_rules! class_composition{ macro_rules! class_composition{

12
src/runner/macros.rs Normal file
View File

@ -0,0 +1,12 @@
macro_rules! type_from_lua_userdata{
($asd:ident)=>{
impl<'lua> mlua::FromLua<'lua> for $asd{
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->Result<Self,mlua::Error>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
}
}
}
};
}

View File

@ -1,3 +1,5 @@
#[macro_use]
mod macros;
mod runner; mod runner;
mod r#enum; mod r#enum;

View File

@ -73,11 +73,4 @@ impl mlua::UserData for Vector3{
} }
} }
impl<'lua> mlua::FromLua<'lua> for Vector3{ type_from_lua_userdata!(Vector3);
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected Vector3 got {:?}",other))),
}
}
}