diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2022-10-18 10:59:13 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2022-12-18 00:35:41 +0000 |
commit | c44463f030f592fa4bc0feaf66c76d467ae01919 (patch) | |
tree | e6ba3f1f36739c9864da22ae886aefc2f7819d4e | |
parent | de38e299fc974090a02f2f90b70221810ef6d7e5 (diff) | |
download | mlua-c44463f030f592fa4bc0feaf66c76d467ae01919.zip |
Replace `impl AsRef<str>` with simple `&str`
-rw-r--r-- | src/lua.rs | 26 | ||||
-rw-r--r-- | src/scope.rs | 36 | ||||
-rw-r--r-- | src/userdata.rs | 26 | ||||
-rw-r--r-- | src/userdata_impl.rs | 45 |
4 files changed, 62 insertions, 71 deletions
@@ -711,11 +711,7 @@ impl Lua { /// Behavior is similar to Lua's [`require`] function. /// /// [`require`]: https://www.lua.org/manual/5.4/manual.html#pdf-require - pub fn load_from_function<'lua, T>( - &'lua self, - modname: impl AsRef<str>, - func: Function<'lua>, - ) -> Result<T> + pub fn load_from_function<'lua, T>(&'lua self, modname: &str, func: Function<'lua>) -> Result<T> where T: FromLua<'lua>, { @@ -728,7 +724,7 @@ impl Lua { Table(self.pop_ref()) }; - let modname = self.create_string(modname.as_ref())?; + let modname = self.create_string(modname)?; let value = match loaded.raw_get(modname.clone())? { Value::Nil => { let result = match func.call(modname.clone())? { @@ -750,7 +746,7 @@ impl Lua { /// unloaded only by closing Lua state. /// /// [`package.loaded`]: https://www.lua.org/manual/5.4/manual.html#pdf-package.loaded - pub fn unload(&self, modname: impl AsRef<str>) -> Result<()> { + pub fn unload(&self, modname: &str) -> Result<()> { let loaded = unsafe { let _sg = StackGuard::new(self.state); check_stack(self.state, 2)?; @@ -760,7 +756,7 @@ impl Lua { Table(self.pop_ref()) }; - let modname = self.create_string(modname.as_ref())?; + let modname = self.create_string(modname)?; loaded.raw_remove(modname)?; Ok(()) } @@ -1992,7 +1988,7 @@ impl Lua { /// /// This value will be available to rust from all `Lua` instances which share the same main /// state. - pub fn set_named_registry_value<'lua, T>(&'lua self, name: impl AsRef<str>, t: T) -> Result<()> + pub fn set_named_registry_value<'lua, T>(&'lua self, name: &str, t: T) -> Result<()> where T: ToLua<'lua>, { @@ -2002,7 +1998,7 @@ impl Lua { check_stack(self.state, 5)?; self.push_value(t)?; - rawset_field(self.state, ffi::LUA_REGISTRYINDEX, name.as_ref()) + rawset_field(self.state, ffi::LUA_REGISTRYINDEX, name) } } @@ -2012,7 +2008,7 @@ impl Lua { /// get a value previously set by [`set_named_registry_value`]. /// /// [`set_named_registry_value`]: #method.set_named_registry_value - pub fn named_registry_value<'lua, T>(&'lua self, name: impl AsRef<str>) -> Result<T> + pub fn named_registry_value<'lua, T>(&'lua self, name: &str) -> Result<T> where T: FromLua<'lua>, { @@ -2021,7 +2017,7 @@ impl Lua { check_stack(self.state, 3)?; let protect = !self.unlikely_memory_error(); - push_string(self.state, name.as_ref().as_bytes(), protect)?; + push_string(self.state, name.as_bytes(), protect)?; ffi::lua_rawget(self.state, ffi::LUA_REGISTRYINDEX); self.pop_value() @@ -2034,7 +2030,7 @@ impl Lua { /// Equivalent to calling [`set_named_registry_value`] with a value of Nil. /// /// [`set_named_registry_value`]: #method.set_named_registry_value - pub fn unset_named_registry_value(&self, name: impl AsRef<str>) -> Result<()> { + pub fn unset_named_registry_value(&self, name: &str) -> Result<()> { self.set_named_registry_value(name, Nil) } @@ -3183,11 +3179,11 @@ unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) -> Result< #[inline(always)] pub unsafe fn requiref( state: *mut ffi::lua_State, - modname: impl AsRef<str>, + modname: &str, openf: ffi::lua_CFunction, glb: c_int, ) -> Result<()> { - let modname = mlua_expect!(CString::new(modname.as_ref()), "modname contains nil byte"); + let modname = mlua_expect!(CString::new(modname), "modname contains nil byte"); protect_lua!(state, 0, 1, |state| { ffi::luaL_requiref(state, modname.as_ptr() as *const c_char, openf, glb) }) diff --git a/src/scope.rs b/src/scope.rs index 2273cfa..69fc0a4 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -620,28 +620,28 @@ impl<'lua, T: UserData> Default for NonStaticUserDataMethods<'lua, T> { } impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'lua, T> { - fn add_method<A, R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_method<A, R, M>(&mut self, name: &str, method: M) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result<R>, { self.methods.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::Method(Box::new(move |lua, ud, args| { method(lua, ud, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), )); } - fn add_method_mut<A, R, M>(&mut self, name: impl AsRef<str>, mut method: M) + fn add_method_mut<A, R, M>(&mut self, name: &str, mut method: M) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<R>, { self.methods.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::MethodMut(Box::new(move |lua, ud, args| { method(lua, ud, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), @@ -649,7 +649,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(feature = "async")] - fn add_async_method<A, R, M, MR>(&mut self, _name: impl AsRef<str>, _method: M) + fn add_async_method<A, R, M, MR>(&mut self, _name: &str, _method: M) where T: Clone, A: FromLuaMulti<'lua>, @@ -662,28 +662,28 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l mlua_panic!("asynchronous methods are not supported for non-static userdata") } - fn add_function<A, R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_function<A, R, F>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result<R>, { self.methods.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::Function(Box::new(move |lua, args| { function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), )); } - fn add_function_mut<A, R, F>(&mut self, name: impl AsRef<str>, mut function: F) + fn add_function_mut<A, R, F>(&mut self, name: &str, mut function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result<R>, { self.methods.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::FunctionMut(Box::new(move |lua, args| { function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), @@ -691,7 +691,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(feature = "async")] - fn add_async_function<A, R, F, FR>(&mut self, _name: impl AsRef<str>, _function: F) + fn add_async_function<A, R, F, FR>(&mut self, _name: &str, _function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -805,52 +805,52 @@ impl<'lua, T: UserData> Default for NonStaticUserDataFields<'lua, T> { } impl<'lua, T: UserData> UserDataFields<'lua, T> for NonStaticUserDataFields<'lua, T> { - fn add_field_method_get<R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_field_method_get<R, M>(&mut self, name: &str, method: M) where R: ToLua<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> Result<R>, { self.field_getters.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::Method(Box::new(move |lua, ud, _| { method(lua, ud)?.to_lua_multi(lua) })), )); } - fn add_field_method_set<A, M>(&mut self, name: impl AsRef<str>, mut method: M) + fn add_field_method_set<A, M>(&mut self, name: &str, mut method: M) where A: FromLua<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::MethodMut(Box::new(move |lua, ud, args| { method(lua, ud, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), )); } - fn add_field_function_get<R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_field_function_get<R, F>(&mut self, name: &str, function: F) where R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> Result<R>, { self.field_getters.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::Function(Box::new(move |lua, args| { function(lua, AnyUserData::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), )); } - fn add_field_function_set<A, F>(&mut self, name: impl AsRef<str>, mut function: F) + fn add_field_function_set<A, F>(&mut self, name: &str, mut function: F) where A: FromLua<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().into(), + name.into(), NonStaticMethod::FunctionMut(Box::new(move |lua, args| { let (ud, val) = <_>::from_lua_multi(args, lua)?; function(lua, ud, val)?.to_lua_multi(lua) diff --git a/src/userdata.rs b/src/userdata.rs index baec57b..eaf3800 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -310,7 +310,7 @@ pub trait UserDataMethods<'lua, T: UserData> { /// /// If `add_meta_method` is used to set the `__index` metamethod, the `__index` metamethod will /// be used as a fall-back if no regular method is found. - fn add_method<A, R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_method<A, R, M>(&mut self, name: &str, method: M) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -321,7 +321,7 @@ pub trait UserDataMethods<'lua, T: UserData> { /// Refer to [`add_method`] for more information about the implementation. /// /// [`add_method`]: #method.add_method - fn add_method_mut<A, R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_method_mut<A, R, M>(&mut self, name: &str, method: M) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -337,7 +337,7 @@ pub trait UserDataMethods<'lua, T: UserData> { /// [`add_method`]: #method.add_method #[cfg(feature = "async")] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn add_async_method<A, R, M, MR>(&mut self, name: impl AsRef<str>, method: M) + fn add_async_method<A, R, M, MR>(&mut self, name: &str, method: M) where T: Clone, A: FromLuaMulti<'lua>, @@ -355,7 +355,7 @@ pub trait UserDataMethods<'lua, T: UserData> { /// [`AnyUserData`]: crate::AnyUserData /// [`add_method`]: #method.add_method /// [`add_method_mut`]: #method.add_method_mut - fn add_function<A, R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_function<A, R, F>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -366,7 +366,7 @@ pub trait UserDataMethods<'lua, T: UserData> { /// This is a version of [`add_function`] that accepts a FnMut argument. /// /// [`add_function`]: #method.add_function - fn add_function_mut<A, R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_function_mut<A, R, F>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -382,7 +382,7 @@ pub trait UserDataMethods<'lua, T: UserData> { /// [`add_function`]: #method.add_function #[cfg(feature = "async")] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn add_async_function<A, R, F, FR>(&mut self, name: impl AsRef<str>, function: F) + fn add_async_function<A, R, F, FR>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -508,7 +508,7 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// If `add_meta_method` is used to set the `__index` metamethod, the `__index` metamethod will /// be used as a fall-back if no regular field or method are found. - fn add_field_method_get<R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_field_method_get<R, M>(&mut self, name: &str, method: M) where R: ToLua<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> Result<R>; @@ -520,7 +520,7 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// If `add_meta_method` is used to set the `__newindex` metamethod, the `__newindex` metamethod will /// be used as a fall-back if no regular field is found. - fn add_field_method_set<A, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_field_method_set<A, M>(&mut self, name: &str, method: M) where A: FromLua<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<()>; @@ -532,7 +532,7 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// [`AnyUserData`]: crate::AnyUserData /// [`add_field_method_get`]: #method.add_field_method_get - fn add_field_function_get<R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_field_function_get<R, F>(&mut self, name: &str, function: F) where R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> Result<R>; @@ -544,7 +544,7 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// [`AnyUserData`]: crate::AnyUserData /// [`add_field_method_set`]: #method.add_field_method_set - fn add_field_function_set<A, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_field_function_set<A, F>(&mut self, name: &str, function: F) where A: FromLua<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()>; @@ -974,7 +974,7 @@ impl<'lua> AnyUserData<'lua> { /// The value can be retrieved with [`get_named_user_value`]. /// /// [`get_named_user_value`]: #method.get_named_user_value - pub fn set_named_user_value<V>(&self, name: impl AsRef<str>, v: V) -> Result<()> + pub fn set_named_user_value<V>(&self, name: &str, v: V) -> Result<()> where V: ToLua<'lua>, { @@ -987,7 +987,6 @@ impl<'lua> AnyUserData<'lua> { lua.push_value(v.to_lua(lua)?)?; // Multiple (extra) user values are emulated by storing them in a table - let name = name.as_ref(); protect_lua!(lua.state, 2, 0, |state| { if getuservalue_table(state, -2) != ffi::LUA_TTABLE { // Create a new table to use as uservalue @@ -1012,7 +1011,7 @@ impl<'lua> AnyUserData<'lua> { /// Returns an associated value by name set by [`set_named_user_value`]. /// /// [`set_named_user_value`]: #method.set_named_user_value - pub fn get_named_user_value<V>(&self, name: impl AsRef<str>) -> Result<V> + pub fn get_named_user_value<V>(&self, name: &str) -> Result<V> where V: FromLua<'lua>, { @@ -1024,7 +1023,6 @@ impl<'lua> AnyUserData<'lua> { lua.push_userdata_ref(&self.0)?; // Multiple (extra) user values are emulated by storing them in a table - let name = name.as_ref(); protect_lua!(lua.state, 1, 1, |state| { if getuservalue_table(state, -1) != ffi::LUA_TTABLE { ffi::lua_pushnil(state); diff --git a/src/userdata_impl.rs b/src/userdata_impl.rs index 0c792be..3126b09 100644 --- a/src/userdata_impl.rs +++ b/src/userdata_impl.rs @@ -48,28 +48,27 @@ impl<'lua, T: 'static + UserData> Default for StaticUserDataMethods<'lua, T> { } impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMethods<'lua, T> { - fn add_method<A, R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_method<A, R, M>(&mut self, name: &str, method: M) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result<R>, { - self.methods - .push((name.as_ref().into(), Self::box_method(method))); + self.methods.push((name.into(), Self::box_method(method))); } - fn add_method_mut<A, R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_method_mut<A, R, M>(&mut self, name: &str, method: M) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<R>, { self.methods - .push((name.as_ref().into(), Self::box_method_mut(method))); + .push((name.into(), Self::box_method_mut(method))); } #[cfg(feature = "async")] - fn add_async_method<A, R, M, MR>(&mut self, name: impl AsRef<str>, method: M) + fn add_async_method<A, R, M, MR>(&mut self, name: &str, method: M) where T: Clone, A: FromLuaMulti<'lua>, @@ -78,31 +77,31 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet MR: 'lua + Future<Output = Result<R>>, { self.async_methods - .push((name.as_ref().into(), Self::box_async_method(method))); + .push((name.into(), Self::box_async_method(method))); } - fn add_function<A, R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_function<A, R, F>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result<R>, { self.methods - .push((name.as_ref().into(), Self::box_function(function))); + .push((name.into(), Self::box_function(function))); } - fn add_function_mut<A, R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_function_mut<A, R, F>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result<R>, { self.methods - .push((name.as_ref().into(), Self::box_function_mut(function))); + .push((name.into(), Self::box_function_mut(function))); } #[cfg(feature = "async")] - fn add_async_function<A, R, F, FR>(&mut self, name: impl AsRef<str>, function: F) + fn add_async_function<A, R, F, FR>(&mut self, name: &str, function: F) where A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, @@ -110,7 +109,7 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet FR: 'lua + Future<Output = Result<R>>, { self.async_methods - .push((name.as_ref().into(), Self::box_async_function(function))); + .push((name.into(), Self::box_async_function(function))); } fn add_meta_method<A, R, M>(&mut self, meta: impl Into<MetaMethod>, method: M) @@ -467,46 +466,44 @@ impl<'lua, T: 'static + UserData> Default for StaticUserDataFields<'lua, T> { } impl<'lua, T: 'static + UserData> UserDataFields<'lua, T> for StaticUserDataFields<'lua, T> { - fn add_field_method_get<R, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_field_method_get<R, M>(&mut self, name: &str, method: M) where R: ToLua<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> Result<R>, { self.field_getters.push(( - name.as_ref().into(), + name.into(), StaticUserDataMethods::box_method(move |lua, data, ()| method(lua, data)), )); } - fn add_field_method_set<A, M>(&mut self, name: impl AsRef<str>, method: M) + fn add_field_method_set<A, M>(&mut self, name: &str, method: M) where A: FromLua<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<()>, { - self.field_setters.push(( - name.as_ref().into(), - StaticUserDataMethods::box_method_mut(method), - )); + self.field_setters + .push((name.into(), StaticUserDataMethods::box_method_mut(method))); } - fn add_field_function_get<R, F>(&mut self, name: impl AsRef<str>, function: F) + fn add_field_function_get<R, F>(&mut self, name: &str, function: F) where R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> Result<R>, { self.field_getters.push(( - name.as_ref().into(), + name.into(), StaticUserDataMethods::<T>::box_function(function), )); } - fn add_field_function_set<A, F>(&mut self, name: impl AsRef<str>, mut function: F) + fn add_field_function_set<A, F>(&mut self, name: &str, mut function: F) where A: FromLua<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().into(), + name.into(), StaticUserDataMethods::<T>::box_function_mut(move |lua, (data, val)| { function(lua, data, val) }), |