From 99becdb590db4d5c7c0f50cb1a91cc099313de2d Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sun, 29 May 2022 20:17:09 +0100 Subject: More inline attributes --- src/conversion.rs | 31 ++++++++++++++++++++++++++++++- src/multi.rs | 11 +++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/conversion.rs b/src/conversion.rs index b9939c1..6682959 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -250,18 +250,21 @@ impl<'lua> ToLua<'lua> for &str { } impl<'lua> ToLua<'lua> for Cow<'_, str> { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(self.as_bytes())?)) } } impl<'lua> ToLua<'lua> for Box { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(&*self)?)) } } impl<'lua> FromLua<'lua> for Box { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { let ty = value.type_name(); Ok(lua @@ -278,12 +281,14 @@ impl<'lua> FromLua<'lua> for Box { } impl<'lua> ToLua<'lua> for CString { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(self.as_bytes())?)) } } impl<'lua> FromLua<'lua> for CString { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { let ty = value.type_name(); let string = lua @@ -306,24 +311,28 @@ impl<'lua> FromLua<'lua> for CString { } impl<'lua> ToLua<'lua> for &CStr { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(self.to_bytes())?)) } } impl<'lua> ToLua<'lua> for Cow<'_, CStr> { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(self.to_bytes())?)) } } impl<'lua> ToLua<'lua> for BString { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(&self)?)) } } impl<'lua> FromLua<'lua> for BString { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { let ty = value.type_name(); Ok(BString::from( @@ -340,14 +349,16 @@ impl<'lua> FromLua<'lua> for BString { } impl<'lua> ToLua<'lua> for &BStr { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { - Ok(Value::String(lua.create_string(&self)?)) + Ok(Value::String(lua.create_string(self)?)) } } macro_rules! lua_convert_int { ($x:ty) => { impl<'lua> ToLua<'lua> for $x { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { cast(self) .map(Value::Integer) @@ -362,6 +373,7 @@ macro_rules! lua_convert_int { } impl<'lua> FromLua<'lua> for $x { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { let ty = value.type_name(); (if let Value::Integer(i) = value { @@ -405,6 +417,7 @@ lua_convert_int!(usize); macro_rules! lua_convert_float { ($x:ty) => { impl<'lua> ToLua<'lua> for $x { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { cast(self) .ok_or_else(|| Error::ToLuaConversionError { @@ -417,6 +430,7 @@ macro_rules! lua_convert_float { } impl<'lua> FromLua<'lua> for $x { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { let ty = value.type_name(); lua.coerce_number(value)? @@ -444,6 +458,7 @@ impl<'lua, T> ToLua<'lua> for &[T] where T: Clone + ToLua<'lua>, { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table( lua.create_sequence_from(self.iter().cloned())?, @@ -455,6 +470,7 @@ impl<'lua, T, const N: usize> ToLua<'lua> for [T; N] where T: ToLua<'lua>, { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_sequence_from(self)?)) } @@ -464,6 +480,7 @@ impl<'lua, T, const N: usize> FromLua<'lua> for [T; N] where T: FromLua<'lua>, { + #[inline] fn from_lua(value: Value<'lua>, _lua: &'lua Lua) -> Result { match value { #[cfg(feature = "luau")] @@ -496,24 +513,28 @@ where } impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Box<[T]> { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_sequence_from(self.into_vec())?)) } } impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Box<[T]> { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { Ok(Vec::::from_lua(value, lua)?.into_boxed_slice()) } } impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Vec { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_sequence_from(self)?)) } } impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Vec { + #[inline] fn from_lua(value: Value<'lua>, _lua: &'lua Lua) -> Result { match value { #[cfg(feature = "luau")] @@ -535,6 +556,7 @@ impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Vec { impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>, S: BuildHasher> ToLua<'lua> for HashMap { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_table_from(self)?)) } @@ -543,6 +565,7 @@ impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>, S: BuildHasher> ToLua<'lu impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>, S: BuildHasher + Default> FromLua<'lua> for HashMap { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { if let Value::Table(table) = value { table.pairs().collect() @@ -557,12 +580,14 @@ impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>, S: BuildHasher + Defa } impl<'lua, K: Ord + ToLua<'lua>, V: ToLua<'lua>> ToLua<'lua> for BTreeMap { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_table_from(self)?)) } } impl<'lua, K: Ord + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for BTreeMap { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { if let Value::Table(table) = value { table.pairs().collect() @@ -577,6 +602,7 @@ impl<'lua, K: Ord + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for BTreeMap< } impl<'lua, T: Eq + Hash + ToLua<'lua>, S: BuildHasher> ToLua<'lua> for HashSet { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_table_from( self.into_iter().map(|val| (val, true)), @@ -585,6 +611,7 @@ impl<'lua, T: Eq + Hash + ToLua<'lua>, S: BuildHasher> ToLua<'lua> for HashSet, S: BuildHasher + Default> FromLua<'lua> for HashSet { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { match value { Value::Table(table) if table.len()? > 0 => table.sequence_values().collect(), @@ -602,6 +629,7 @@ impl<'lua, T: Eq + Hash + FromLua<'lua>, S: BuildHasher + Default> FromLua<'lua> } impl<'lua, T: Ord + ToLua<'lua>> ToLua<'lua> for BTreeSet { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_table_from( self.into_iter().map(|val| (val, true)), @@ -610,6 +638,7 @@ impl<'lua, T: Ord + ToLua<'lua>> ToLua<'lua> for BTreeSet { } impl<'lua, T: Ord + FromLua<'lua>> FromLua<'lua> for BTreeSet { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { match value { Value::Table(table) if table.len()? > 0 => table.sequence_values().collect(), diff --git a/src/multi.rs b/src/multi.rs index bed3d23..92b3d4f 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -11,6 +11,7 @@ use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti}; /// Result is convertible to `MultiValue` following the common Lua idiom of returning the result /// on success, or in the case of an error, returning `nil` and an error message. impl<'lua, T: ToLua<'lua>, E: ToLua<'lua>> ToLuaMulti<'lua> for StdResult { + #[inline] fn to_lua_multi(self, lua: &'lua Lua) -> Result> { let mut result = MultiValue::new_or_cached(lua); match self { @@ -25,6 +26,7 @@ impl<'lua, T: ToLua<'lua>, E: ToLua<'lua>> ToLuaMulti<'lua> for StdResult } impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for T { + #[inline] fn to_lua_multi(self, lua: &'lua Lua) -> Result> { let mut v = MultiValue::new_or_cached(lua); v.push_front(self.to_lua(lua)?); @@ -33,6 +35,7 @@ impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for T { } impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T { + #[inline] fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result { let res = T::from_lua(values.pop_front().unwrap_or(Nil), lua); lua.cache_multivalue(values); @@ -41,12 +44,14 @@ impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T { } impl<'lua> ToLuaMulti<'lua> for MultiValue<'lua> { + #[inline] fn to_lua_multi(self, _: &'lua Lua) -> Result> { Ok(self) } } impl<'lua> FromLuaMulti<'lua> for MultiValue<'lua> { + #[inline] fn from_lua_multi(values: MultiValue<'lua>, _: &'lua Lua) -> Result { Ok(values) } @@ -124,6 +129,7 @@ impl DerefMut for Variadic { } impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for Variadic { + #[inline] fn to_lua_multi(self, lua: &'lua Lua) -> Result> { let mut values = MultiValue::new_or_cached(lua); values.refill(self.0.into_iter().map(|e| e.to_lua(lua)))?; @@ -132,6 +138,7 @@ impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for Variadic { } impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for Variadic { + #[inline] fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result { let res = values .drain_all() @@ -146,12 +153,14 @@ impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for Variadic { macro_rules! impl_tuple { () => ( impl<'lua> ToLuaMulti<'lua> for () { + #[inline] fn to_lua_multi(self, lua: &'lua Lua) -> Result> { Ok(MultiValue::new_or_cached(lua)) } } impl<'lua> FromLuaMulti<'lua> for () { + #[inline] fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result { lua.cache_multivalue(values); Ok(()) @@ -166,6 +175,7 @@ macro_rules! impl_tuple { { #[allow(unused_mut)] #[allow(non_snake_case)] + #[inline] fn to_lua_multi(self, lua: &'lua Lua) -> Result> { let ($($name,)* $last,) = self; @@ -181,6 +191,7 @@ macro_rules! impl_tuple { { #[allow(unused_mut)] #[allow(non_snake_case)] + #[inline] fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result { $(let $name = values.pop_front().unwrap_or(Nil);)* let $last = FromLuaMulti::from_lua_multi(values, lua)?; -- cgit v1.2.3