diff --git a/src/conversion.rs b/src/conversion.rs index 1e3fdf2..5b907c0 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -20,24 +20,28 @@ use crate::userdata::{AnyUserData, UserData}; use crate::value::{FromLua, Nil, ToLua, Value}; impl<'lua> ToLua<'lua> for Value<'lua> { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(self) } } impl<'lua> FromLua<'lua> for Value<'lua> { + #[inline] fn from_lua(lua_value: Value<'lua>, _: &'lua Lua) -> Result { Ok(lua_value) } } impl<'lua> ToLua<'lua> for String<'lua> { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::String(self)) } } impl<'lua> FromLua<'lua> for String<'lua> { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result> { let ty = value.type_name(); lua.coerce_string(value)? @@ -50,12 +54,14 @@ impl<'lua> FromLua<'lua> for String<'lua> { } impl<'lua> ToLua<'lua> for Table<'lua> { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::Table(self)) } } impl<'lua> FromLua<'lua> for Table<'lua> { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result> { match value { Value::Table(table) => Ok(table), @@ -69,12 +75,14 @@ impl<'lua> FromLua<'lua> for Table<'lua> { } impl<'lua> ToLua<'lua> for Function<'lua> { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::Function(self)) } } impl<'lua> FromLua<'lua> for Function<'lua> { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result> { match value { Value::Function(table) => Ok(table), @@ -88,12 +96,14 @@ impl<'lua> FromLua<'lua> for Function<'lua> { } impl<'lua> ToLua<'lua> for Thread<'lua> { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::Thread(self)) } } impl<'lua> FromLua<'lua> for Thread<'lua> { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result> { match value { Value::Thread(t) => Ok(t), @@ -107,12 +117,14 @@ impl<'lua> FromLua<'lua> for Thread<'lua> { } impl<'lua> ToLua<'lua> for AnyUserData<'lua> { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::UserData(self)) } } impl<'lua> FromLua<'lua> for AnyUserData<'lua> { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result> { match value { Value::UserData(ud) => Ok(ud), @@ -126,12 +138,14 @@ impl<'lua> FromLua<'lua> for AnyUserData<'lua> { } impl<'lua, T: 'static + MaybeSend + UserData> ToLua<'lua> for T { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::UserData(lua.create_userdata(self)?)) } } impl<'lua, T: 'static + UserData + Clone> FromLua<'lua> for T { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { match value { Value::UserData(ud) => Ok(ud.borrow::()?.clone()), @@ -145,12 +159,14 @@ impl<'lua, T: 'static + UserData + Clone> FromLua<'lua> for T { } impl<'lua> ToLua<'lua> for Error { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::Error(self)) } } impl<'lua> FromLua<'lua> for Error { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { match value { Value::Error(err) => Ok(err), @@ -164,12 +180,14 @@ impl<'lua> FromLua<'lua> for Error { } impl<'lua> ToLua<'lua> for bool { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::Boolean(self)) } } impl<'lua> FromLua<'lua> for bool { + #[inline] fn from_lua(v: Value<'lua>, _: &'lua Lua) -> Result { match v { Value::Nil => Ok(false), @@ -180,12 +198,14 @@ impl<'lua> FromLua<'lua> for bool { } impl<'lua> ToLua<'lua> for LightUserData { + #[inline] fn to_lua(self, _: &'lua Lua) -> Result> { Ok(Value::LightUserData(self)) } } impl<'lua> FromLua<'lua> for LightUserData { + #[inline] fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { match value { Value::LightUserData(ud) => Ok(ud), @@ -199,12 +219,14 @@ impl<'lua> FromLua<'lua> for LightUserData { } impl<'lua> ToLua<'lua> for StdString { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(&self)?)) } } impl<'lua> FromLua<'lua> for StdString { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { let ty = value.type_name(); Ok(lua @@ -220,6 +242,7 @@ impl<'lua> FromLua<'lua> for StdString { } impl<'lua> ToLua<'lua> for &str { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::String(lua.create_string(self)?)) } @@ -595,6 +618,7 @@ impl<'lua, T: Ord + FromLua<'lua>> FromLua<'lua> for BTreeSet { } impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Option { + #[inline] fn to_lua(self, lua: &'lua Lua) -> Result> { match self { Some(val) => val.to_lua(lua), @@ -604,6 +628,7 @@ impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Option { } impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Option { + #[inline] fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { match value { Nil => Ok(None), diff --git a/src/thread.rs b/src/thread.rs index 632c341..20e9abd 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -341,6 +341,7 @@ where } #[cfg(feature = "async")] +#[inline(always)] fn is_poll_pending(val: &MultiValue) -> bool { match val.iter().enumerate().last() { Some((1, Value::LightUserData(ud))) => { diff --git a/src/userdata.rs b/src/userdata.rs index 113725f..ead279c 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -597,11 +597,13 @@ pub trait UserData: Sized { pub(crate) struct UserDataCell(RefCell>); impl UserDataCell { + #[inline] pub(crate) fn new(data: T) -> Self { UserDataCell(RefCell::new(UserDataWrapped::new(data))) } #[cfg(feature = "serialize")] + #[inline] pub(crate) fn new_ser(data: T) -> Self where T: 'static + Serialize, @@ -610,6 +612,7 @@ impl UserDataCell { } // Immutably borrows the wrapped value. + #[inline] fn try_borrow(&self) -> Result> { self.0 .try_borrow() @@ -618,6 +621,7 @@ impl UserDataCell { } // Mutably borrows the wrapped value. + #[inline] fn try_borrow_mut(&self) -> Result> { self.0 .try_borrow_mut() @@ -633,11 +637,13 @@ pub(crate) enum UserDataWrapped { } impl UserDataWrapped { + #[inline] fn new(data: T) -> Self { UserDataWrapped::Default(data) } #[cfg(feature = "serialize")] + #[inline] fn new_ser(data: T) -> Self where T: 'static + Serialize, @@ -659,6 +665,7 @@ impl Drop for UserDataWrapped { impl Deref for UserDataWrapped { type Target = T; + #[inline] fn deref(&self) -> &Self::Target { match self { Self::Default(data) => data, @@ -669,6 +676,7 @@ impl Deref for UserDataWrapped { } impl DerefMut for UserDataWrapped { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { match self { Self::Default(data) => data, @@ -726,6 +734,7 @@ impl<'lua> AnyUserData<'lua> { /// /// Returns a `UserDataBorrowError` if the userdata is already mutably borrowed. Returns a /// `UserDataTypeMismatch` if the userdata is not of type `T`. + #[inline] pub fn borrow(&self) -> Result> { self.inspect(|cell| cell.try_borrow()) } @@ -736,6 +745,7 @@ impl<'lua> AnyUserData<'lua> { /// /// Returns a `UserDataBorrowMutError` if the userdata cannot be mutably borrowed. /// Returns a `UserDataTypeMismatch` if the userdata is not of type `T`. + #[inline] pub fn borrow_mut(&self) -> Result> { self.inspect(|cell| cell.try_borrow_mut()) } diff --git a/src/util.rs b/src/util.rs index fa11981..41de848 100644 --- a/src/util.rs +++ b/src/util.rs @@ -19,6 +19,7 @@ static METATABLE_CACHE: Lazy>> = Lazy::new(|| { // Checks that Lua has enough free stack space for future stack operations. On failure, this will // panic with an internal error message. +#[inline] pub unsafe fn assert_stack(state: *mut ffi::lua_State, amount: c_int) { // TODO: This should only be triggered when there is a logic error in `mlua`. In the future, // when there is a way to be confident about stack safety and test it, this could be enabled @@ -30,6 +31,7 @@ pub unsafe fn assert_stack(state: *mut ffi::lua_State, amount: c_int) { } // Checks that Lua has enough free stack space and returns `Error::StackError` on failure. +#[inline] pub unsafe fn check_stack(state: *mut ffi::lua_State, amount: c_int) -> Result<()> { if ffi::lua_checkstack(state, amount) == 0 { Err(Error::StackError) @@ -48,6 +50,7 @@ impl StackGuard { // Creates a StackGuard instance with record of the stack size, and on Drop will check the // stack size and drop any extra elements. If the stack size at the end is *smaller* than at // the beginning, this is considered a fatal logic error and will result in a panic. + #[inline] pub unsafe fn new(state: *mut ffi::lua_State) -> StackGuard { StackGuard { state, @@ -57,6 +60,7 @@ impl StackGuard { } // Similar to `new`, but checks and keeps `extra` elements from top of the stack on Drop. + #[inline] pub unsafe fn new_extra(state: *mut ffi::lua_State, extra: c_int) -> StackGuard { StackGuard { state, @@ -211,6 +215,7 @@ pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error { } // Uses 3 stack spaces +#[inline] pub unsafe fn push_string + ?Sized>( state: *mut ffi::lua_State, s: &S, @@ -222,6 +227,7 @@ pub unsafe fn push_string + ?Sized>( } // Uses 3 stack spaces +#[inline] pub unsafe fn push_table(state: *mut ffi::lua_State, narr: c_int, nrec: c_int) -> Result<()> { protect_lua(state, 0, 1, |state| ffi::lua_createtable(state, narr, nrec)) } @@ -241,6 +247,7 @@ where } // Internally uses 3 stack spaces, does not call checkstack. +#[inline] pub unsafe fn push_userdata(state: *mut ffi::lua_State, t: T) -> Result<()> { let ud = protect_lua(state, 0, 1, |state| { ffi::lua_newuserdata(state, mem::size_of::()) as *mut T @@ -249,6 +256,7 @@ pub unsafe fn push_userdata(state: *mut ffi::lua_State, t: T) -> Result<()> { Ok(()) } +#[inline] pub unsafe fn get_userdata(state: *mut ffi::lua_State, index: c_int) -> *mut T { let ud = ffi::lua_touserdata(state, index) as *mut T; mlua_debug_assert!(!ud.is_null(), "userdata pointer is null"); diff --git a/src/value.rs b/src/value.rs index 7399844..2196c6d 100644 --- a/src/value.rs +++ b/src/value.rs @@ -157,18 +157,21 @@ pub struct MultiValue<'lua>(Vec>); impl<'lua> MultiValue<'lua> { /// Creates an empty `MultiValue` containing no values. + #[inline] pub fn new() -> MultiValue<'lua> { MultiValue(Vec::new()) } } impl<'lua> Default for MultiValue<'lua> { + #[inline] fn default() -> MultiValue<'lua> { MultiValue::new() } } impl<'lua> FromIterator> for MultiValue<'lua> { + #[inline] fn from_iter>>(iter: I) -> Self { MultiValue::from_vec(Vec::from_iter(iter)) } @@ -178,6 +181,7 @@ impl<'lua> IntoIterator for MultiValue<'lua> { type Item = Value<'lua>; type IntoIter = iter::Rev>>; + #[inline] fn into_iter(self) -> Self::IntoIter { self.0.into_iter().rev() } @@ -187,43 +191,52 @@ impl<'a, 'lua> IntoIterator for &'a MultiValue<'lua> { type Item = &'a Value<'lua>; type IntoIter = iter::Rev>>; + #[inline] fn into_iter(self) -> Self::IntoIter { (&self.0).iter().rev() } } impl<'lua> MultiValue<'lua> { + #[inline] pub fn from_vec(mut v: Vec>) -> MultiValue<'lua> { v.reverse(); MultiValue(v) } + #[inline] pub fn into_vec(self) -> Vec> { let mut v = self.0; v.reverse(); v } + #[inline] pub(crate) fn reserve(&mut self, size: usize) { self.0.reserve(size); } + #[inline] pub(crate) fn push_front(&mut self, value: Value<'lua>) { self.0.push(value); } + #[inline] pub(crate) fn pop_front(&mut self) -> Option> { self.0.pop() } + #[inline] pub fn len(&self) -> usize { self.0.len() } + #[inline] pub fn is_empty(&self) -> bool { self.0.len() == 0 } + #[inline] pub fn iter(&self) -> iter::Rev>> { self.0.iter().rev() }