From 79eb64d85514e4c908684a40f6a09aeb9dbfbe44 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Tue, 18 Oct 2022 01:53:17 +0100 Subject: [PATCH] Use `impl AsRef` for userdata methods/fields instead of generic param. Use `impl AsRef` for module names and named registry values. --- src/lua.rs | 33 ++++++----------- src/scope.rs | 73 ++++++++++++++----------------------- src/userdata.rs | 69 +++++++++++++---------------------- src/userdata_impl.rs | 87 ++++++++++++++++++-------------------------- src/util.rs | 6 +-- tests/tests.rs | 4 +- tests/userdata.rs | 6 +-- 7 files changed, 106 insertions(+), 172 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index de39df4..fe99b24 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -711,13 +711,12 @@ 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, S, T>( + pub fn load_from_function<'lua, T>( &'lua self, - modname: &S, + modname: impl AsRef, func: Function<'lua>, ) -> Result where - S: AsRef<[u8]> + ?Sized, T: FromLua<'lua>, { let loaded = unsafe { @@ -729,7 +728,7 @@ impl Lua { Table(self.pop_ref()) }; - let modname = self.create_string(modname)?; + let modname = self.create_string(modname.as_ref())?; let value = match loaded.raw_get(modname.clone())? { Value::Nil => { let result = match func.call(modname.clone())? { @@ -751,10 +750,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: &S) -> Result<()> - where - S: AsRef<[u8]> + ?Sized, - { + pub fn unload(&self, modname: impl AsRef) -> Result<()> { let loaded = unsafe { let _sg = StackGuard::new(self.state); check_stack(self.state, 2)?; @@ -764,7 +760,7 @@ impl Lua { Table(self.pop_ref()) }; - let modname = self.create_string(modname)?; + let modname = self.create_string(modname.as_ref())?; loaded.raw_remove(modname)?; Ok(()) } @@ -1996,9 +1992,8 @@ 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, S, T>(&'lua self, name: &S, t: T) -> Result<()> + pub fn set_named_registry_value<'lua, T>(&'lua self, name: impl AsRef, t: T) -> Result<()> where - S: AsRef<[u8]> + ?Sized, T: ToLua<'lua>, { let t = t.to_lua(self)?; @@ -2007,7 +2002,7 @@ impl Lua { check_stack(self.state, 5)?; self.push_value(t)?; - rawset_field(self.state, ffi::LUA_REGISTRYINDEX, name) + rawset_field(self.state, ffi::LUA_REGISTRYINDEX, name.as_ref()) } } @@ -2017,9 +2012,8 @@ 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, S, T>(&'lua self, name: &S) -> Result + pub fn named_registry_value<'lua, T>(&'lua self, name: impl AsRef) -> Result where - S: AsRef<[u8]> + ?Sized, T: FromLua<'lua>, { let value = unsafe { @@ -2027,7 +2021,7 @@ impl Lua { check_stack(self.state, 3)?; let protect = !self.unlikely_memory_error(); - push_string(self.state, name.as_ref(), protect)?; + push_string(self.state, name.as_ref().as_bytes(), protect)?; ffi::lua_rawget(self.state, ffi::LUA_REGISTRYINDEX); self.pop_value() @@ -2040,10 +2034,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: &S) -> Result<()> - where - S: AsRef<[u8]> + ?Sized, - { + pub fn unset_named_registry_value(&self, name: impl AsRef) -> Result<()> { self.set_named_registry_value(name, Nil) } @@ -3190,9 +3181,9 @@ where // Uses 3 stack spaces unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> { #[inline(always)] - pub unsafe fn requiref + ?Sized>( + pub unsafe fn requiref( state: *mut ffi::lua_State, - modname: &S, + modname: impl AsRef, openf: ffi::lua_CFunction, glb: c_int, ) -> Result<()> { diff --git a/src/scope.rs b/src/scope.rs index 4cb1db0..2273cfa 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -606,7 +606,7 @@ enum NonStaticMethod<'lua, T> { } struct NonStaticUserDataMethods<'lua, T: UserData> { - methods: Vec<(Vec, NonStaticMethod<'lua, T>)>, + methods: Vec<(String, NonStaticMethod<'lua, T>)>, meta_methods: Vec<(MetaMethod, NonStaticMethod<'lua, T>)>, } @@ -620,30 +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(&mut self, name: &S, method: M) + fn add_method(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result, { self.methods.push(( - name.as_ref().to_vec(), + name.as_ref().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(&mut self, name: &S, mut method: M) + fn add_method_mut(&mut self, name: impl AsRef, mut method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result, { self.methods.push(( - name.as_ref().to_vec(), + name.as_ref().into(), NonStaticMethod::MethodMut(Box::new(move |lua, ud, args| { method(lua, ud, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), @@ -651,10 +649,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(feature = "async")] - fn add_async_method(&mut self, _name: &S, _method: M) + fn add_async_method(&mut self, _name: impl AsRef, _method: M) where T: Clone, - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, @@ -665,30 +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(&mut self, name: &S, function: F) + fn add_function(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result, { self.methods.push(( - name.as_ref().to_vec(), + name.as_ref().into(), NonStaticMethod::Function(Box::new(move |lua, args| { function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), )); } - fn add_function_mut(&mut self, name: &S, mut function: F) + fn add_function_mut(&mut self, name: impl AsRef, mut function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result, { self.methods.push(( - name.as_ref().to_vec(), + name.as_ref().into(), NonStaticMethod::FunctionMut(Box::new(move |lua, args| { function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua) })), @@ -696,9 +691,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(feature = "async")] - fn add_async_function(&mut self, _name: &S, _function: F) + fn add_async_function(&mut self, _name: impl AsRef, _function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, @@ -709,9 +703,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l mlua_panic!("asynchronous functions are not supported for non-static userdata") } - fn add_meta_method(&mut self, meta: S, method: M) + fn add_meta_method(&mut self, meta: impl Into, method: M) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result, @@ -724,9 +717,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l )); } - fn add_meta_method_mut(&mut self, meta: S, mut method: M) + fn add_meta_method_mut(&mut self, meta: impl Into, mut method: M) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result, @@ -740,10 +732,9 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] - fn add_async_meta_method(&mut self, _meta: S, _method: M) + fn add_async_meta_method(&mut self, _meta: impl Into, _method: M) where T: Clone, - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, @@ -754,9 +745,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l mlua_panic!("asynchronous meta methods are not supported for non-static userdata") } - fn add_meta_function(&mut self, meta: S, function: F) + fn add_meta_function(&mut self, meta: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result, @@ -769,9 +759,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l )); } - fn add_meta_function_mut(&mut self, meta: S, mut function: F) + fn add_meta_function_mut(&mut self, meta: impl Into, mut function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result, @@ -785,9 +774,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] - fn add_async_meta_function(&mut self, _meta: S, _function: F) + fn add_async_meta_function(&mut self, _meta: impl Into, _function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, @@ -800,8 +788,8 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l } struct NonStaticUserDataFields<'lua, T: UserData> { - field_getters: Vec<(Vec, NonStaticMethod<'lua, T>)>, - field_setters: Vec<(Vec, NonStaticMethod<'lua, T>)>, + field_getters: Vec<(String, NonStaticMethod<'lua, T>)>, + field_setters: Vec<(String, NonStaticMethod<'lua, T>)>, #[allow(clippy::type_complexity)] meta_fields: Vec<(MetaMethod, Box Result>>)>, } @@ -817,56 +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(&mut self, name: &S, method: M) + fn add_field_method_get(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, R: ToLua<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> Result, { self.field_getters.push(( - name.as_ref().to_vec(), + name.as_ref().into(), NonStaticMethod::Method(Box::new(move |lua, ud, _| { method(lua, ud)?.to_lua_multi(lua) })), )); } - fn add_field_method_set(&mut self, name: &S, mut method: M) + fn add_field_method_set(&mut self, name: impl AsRef, mut method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLua<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().to_vec(), + name.as_ref().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(&mut self, name: &S, function: F) + fn add_field_function_get(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> Result, { self.field_getters.push(( - name.as_ref().to_vec(), + name.as_ref().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(&mut self, name: &S, mut function: F) + fn add_field_function_set(&mut self, name: impl AsRef, mut function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLua<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().to_vec(), + name.as_ref().into(), NonStaticMethod::FunctionMut(Box::new(move |lua, args| { let (ud, val) = <_>::from_lua_multi(args, lua)?; function(lua, ud, val)?.to_lua_multi(lua) @@ -874,9 +858,8 @@ impl<'lua, T: UserData> UserDataFields<'lua, T> for NonStaticUserDataFields<'lua )); } - fn add_meta_field_with(&mut self, meta: S, f: F) + fn add_meta_field_with(&mut self, meta: impl Into, f: F) where - S: Into, F: 'static + MaybeSend + Fn(&'lua Lua) -> Result, R: ToLua<'lua>, { diff --git a/src/userdata.rs b/src/userdata.rs index 4be27d2..baec57b 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -310,9 +310,8 @@ 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(&mut self, name: &S, method: M) + fn add_method(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result; @@ -322,9 +321,8 @@ 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(&mut self, name: &S, method: M) + fn add_method_mut(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result; @@ -339,10 +337,9 @@ 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(&mut self, name: &S, method: M) + fn add_async_method(&mut self, name: impl AsRef, method: M) where T: Clone, - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, @@ -358,9 +355,8 @@ pub trait UserDataMethods<'lua, T: UserData> { /// [`AnyUserData`]: crate::AnyUserData /// [`add_method`]: #method.add_method /// [`add_method_mut`]: #method.add_method_mut - fn add_function(&mut self, name: &S, function: F) + fn add_function(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result; @@ -370,9 +366,8 @@ 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(&mut self, name: &S, function: F) + fn add_function_mut(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result; @@ -387,9 +382,8 @@ 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(&mut self, name: &S, function: F) + fn add_async_function(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, @@ -403,9 +397,8 @@ pub trait UserDataMethods<'lua, T: UserData> { /// side has a metatable. To prevent this, use [`add_meta_function`]. /// /// [`add_meta_function`]: #method.add_meta_function - fn add_meta_method(&mut self, meta: S, method: M) + fn add_meta_method(&mut self, meta: impl Into, method: M) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result; @@ -418,9 +411,8 @@ pub trait UserDataMethods<'lua, T: UserData> { /// side has a metatable. To prevent this, use [`add_meta_function`]. /// /// [`add_meta_function`]: #method.add_meta_function - fn add_meta_method_mut(&mut self, meta: S, method: M) + fn add_meta_method_mut(&mut self, meta: impl Into, method: M) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result; @@ -435,10 +427,9 @@ pub trait UserDataMethods<'lua, T: UserData> { /// [`add_meta_method`]: #method.add_meta_method #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn add_async_meta_method(&mut self, name: S, method: M) + fn add_async_meta_method(&mut self, name: impl Into, method: M) where T: Clone, - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, @@ -449,9 +440,8 @@ pub trait UserDataMethods<'lua, T: UserData> { /// Metamethods for binary operators can be triggered if either the left or right argument to /// the binary operator has a metatable, so the first argument here is not necessarily a /// userdata of type `T`. - fn add_meta_function(&mut self, meta: S, function: F) + fn add_meta_function(&mut self, meta: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result; @@ -461,9 +451,8 @@ pub trait UserDataMethods<'lua, T: UserData> { /// This is a version of [`add_meta_function`] that accepts a FnMut argument. /// /// [`add_meta_function`]: #method.add_meta_function - fn add_meta_function_mut(&mut self, meta: S, function: F) + fn add_meta_function_mut(&mut self, meta: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result; @@ -477,9 +466,8 @@ pub trait UserDataMethods<'lua, T: UserData> { /// [`add_meta_function`]: #method.add_meta_function #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] #[cfg_attr(docsrs, doc(cfg(feature = "async")))] - fn add_async_meta_function(&mut self, name: S, function: F) + fn add_async_meta_function(&mut self, name: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, @@ -490,11 +478,11 @@ pub trait UserDataMethods<'lua, T: UserData> { // #[doc(hidden)] - fn add_callback(&mut self, _name: Vec, _callback: Callback<'lua, 'static>) {} + fn add_callback(&mut self, _name: String, _callback: Callback<'lua, 'static>) {} #[doc(hidden)] #[cfg(feature = "async")] - fn add_async_callback(&mut self, _name: Vec, _callback: AsyncCallback<'lua, 'static>) {} + fn add_async_callback(&mut self, _name: String, _callback: AsyncCallback<'lua, 'static>) {} #[doc(hidden)] fn add_meta_callback(&mut self, _meta: MetaMethod, _callback: Callback<'lua, 'static>) {} @@ -520,9 +508,8 @@ 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(&mut self, name: &S, method: M) + fn add_field_method_get(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, R: ToLua<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> Result; @@ -533,9 +520,8 @@ 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(&mut self, name: &S, method: M) + fn add_field_method_set(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLua<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<()>; @@ -546,9 +532,8 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// [`AnyUserData`]: crate::AnyUserData /// [`add_field_method_get`]: #method.add_field_method_get - fn add_field_function_get(&mut self, name: &S, function: F) + fn add_field_function_get(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> Result; @@ -559,9 +544,8 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// [`AnyUserData`]: crate::AnyUserData /// [`add_field_method_set`]: #method.add_field_method_set - fn add_field_function_set(&mut self, name: &S, function: F) + fn add_field_function_set(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLua<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()>; @@ -573,21 +557,20 @@ pub trait UserDataFields<'lua, T: UserData> { /// /// `mlua` will trigger an error on an attempt to define a protected metamethod, /// like `__gc` or `__metatable`. - fn add_meta_field_with(&mut self, meta: S, f: F) + fn add_meta_field_with(&mut self, meta: impl Into, f: F) where - S: Into, - F: 'static + MaybeSend + Fn(&'lua Lua) -> Result, - R: ToLua<'lua>; + R: ToLua<'lua>, + F: 'static + MaybeSend + Fn(&'lua Lua) -> Result; // // Below are internal methods used in generated code // #[doc(hidden)] - fn add_field_getter(&mut self, _name: Vec, _callback: Callback<'lua, 'static>) {} + fn add_field_getter(&mut self, _name: String, _callback: Callback<'lua, 'static>) {} #[doc(hidden)] - fn add_field_setter(&mut self, _name: Vec, _callback: Callback<'lua, 'static>) {} + fn add_field_setter(&mut self, _name: String, _callback: Callback<'lua, 'static>) {} } /// Trait for custom userdata types. @@ -991,9 +974,8 @@ 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(&self, name: &S, v: V) -> Result<()> + pub fn set_named_user_value(&self, name: impl AsRef, v: V) -> Result<()> where - S: AsRef<[u8]> + ?Sized, V: ToLua<'lua>, { let lua = self.0.lua; @@ -1030,9 +1012,8 @@ 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(&self, name: &S) -> Result + pub fn get_named_user_value(&self, name: impl AsRef) -> Result where - S: AsRef<[u8]> + ?Sized, V: FromLua<'lua>, { let lua = self.0.lua; diff --git a/src/userdata_impl.rs b/src/userdata_impl.rs index 944e20d..0c792be 100644 --- a/src/userdata_impl.rs +++ b/src/userdata_impl.rs @@ -24,9 +24,9 @@ use { }; pub(crate) struct StaticUserDataMethods<'lua, T: 'static + UserData> { - pub(crate) methods: Vec<(Vec, Callback<'lua, 'static>)>, + pub(crate) methods: Vec<(String, Callback<'lua, 'static>)>, #[cfg(feature = "async")] - pub(crate) async_methods: Vec<(Vec, AsyncCallback<'lua, 'static>)>, + pub(crate) async_methods: Vec<(String, AsyncCallback<'lua, 'static>)>, pub(crate) meta_methods: Vec<(MetaMethod, Callback<'lua, 'static>)>, #[cfg(feature = "async")] pub(crate) async_meta_methods: Vec<(MetaMethod, AsyncCallback<'lua, 'static>)>, @@ -48,80 +48,73 @@ 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(&mut self, name: &S, method: M) + fn add_method(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result, { self.methods - .push((name.as_ref().to_vec(), Self::box_method(method))); + .push((name.as_ref().into(), Self::box_method(method))); } - fn add_method_mut(&mut self, name: &S, method: M) + fn add_method_mut(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result, { self.methods - .push((name.as_ref().to_vec(), Self::box_method_mut(method))); + .push((name.as_ref().into(), Self::box_method_mut(method))); } #[cfg(feature = "async")] - fn add_async_method(&mut self, name: &S, method: M) + fn add_async_method(&mut self, name: impl AsRef, method: M) where T: Clone, - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, MR: 'lua + Future>, { self.async_methods - .push((name.as_ref().to_vec(), Self::box_async_method(method))); + .push((name.as_ref().into(), Self::box_async_method(method))); } - fn add_function(&mut self, name: &S, function: F) + fn add_function(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result, { self.methods - .push((name.as_ref().to_vec(), Self::box_function(function))); + .push((name.as_ref().into(), Self::box_function(function))); } - fn add_function_mut(&mut self, name: &S, function: F) + fn add_function_mut(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result, { self.methods - .push((name.as_ref().to_vec(), Self::box_function_mut(function))); + .push((name.as_ref().into(), Self::box_function_mut(function))); } #[cfg(feature = "async")] - fn add_async_function(&mut self, name: &S, function: F) + fn add_async_function(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, FR: 'lua + Future>, { self.async_methods - .push((name.as_ref().to_vec(), Self::box_async_function(function))); + .push((name.as_ref().into(), Self::box_async_function(function))); } - fn add_meta_method(&mut self, meta: S, method: M) + fn add_meta_method(&mut self, meta: impl Into, method: M) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T, A) -> Result, @@ -130,9 +123,8 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet .push((meta.into(), Self::box_method(method))); } - fn add_meta_method_mut(&mut self, meta: S, method: M) + fn add_meta_method_mut(&mut self, meta: impl Into, method: M) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result, @@ -142,10 +134,9 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet } #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] - fn add_async_meta_method(&mut self, meta: S, method: M) + fn add_async_meta_method(&mut self, meta: impl Into, method: M) where T: Clone, - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, T, A) -> MR, @@ -155,9 +146,8 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet .push((meta.into(), Self::box_async_method(method))); } - fn add_meta_function(&mut self, meta: S, function: F) + fn add_meta_function(&mut self, meta: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> Result, @@ -166,9 +156,8 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet .push((meta.into(), Self::box_function(function))); } - fn add_meta_function_mut(&mut self, meta: S, function: F) + fn add_meta_function_mut(&mut self, meta: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, A) -> Result, @@ -178,9 +167,8 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet } #[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))] - fn add_async_meta_function(&mut self, meta: S, function: F) + fn add_async_meta_function(&mut self, meta: impl Into, function: F) where - S: Into, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, A) -> FR, @@ -192,12 +180,12 @@ impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMet // Below are internal methods used in generated code - fn add_callback(&mut self, name: Vec, callback: Callback<'lua, 'static>) { + fn add_callback(&mut self, name: String, callback: Callback<'lua, 'static>) { self.methods.push((name, callback)); } #[cfg(feature = "async")] - fn add_async_callback(&mut self, name: Vec, callback: AsyncCallback<'lua, 'static>) { + fn add_async_callback(&mut self, name: String, callback: AsyncCallback<'lua, 'static>) { self.async_methods.push((name, callback)); } @@ -457,8 +445,8 @@ impl<'lua, T: 'static + UserData> StaticUserDataMethods<'lua, T> { } pub(crate) struct StaticUserDataFields<'lua, T: 'static + UserData> { - pub(crate) field_getters: Vec<(Vec, Callback<'lua, 'static>)>, - pub(crate) field_setters: Vec<(Vec, Callback<'lua, 'static>)>, + pub(crate) field_getters: Vec<(String, Callback<'lua, 'static>)>, + pub(crate) field_setters: Vec<(String, Callback<'lua, 'static>)>, #[allow(clippy::type_complexity)] pub(crate) meta_fields: Vec<( MetaMethod, @@ -479,59 +467,54 @@ 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(&mut self, name: &S, method: M) + fn add_field_method_get(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, R: ToLua<'lua>, M: 'static + MaybeSend + Fn(&'lua Lua, &T) -> Result, { self.field_getters.push(( - name.as_ref().to_vec(), + name.as_ref().into(), StaticUserDataMethods::box_method(move |lua, data, ()| method(lua, data)), )); } - fn add_field_method_set(&mut self, name: &S, method: M) + fn add_field_method_set(&mut self, name: impl AsRef, method: M) where - S: AsRef<[u8]> + ?Sized, A: FromLua<'lua>, M: 'static + MaybeSend + FnMut(&'lua Lua, &mut T, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().to_vec(), + name.as_ref().into(), StaticUserDataMethods::box_method_mut(method), )); } - fn add_field_function_get(&mut self, name: &S, function: F) + fn add_field_function_get(&mut self, name: impl AsRef, function: F) where - S: AsRef<[u8]> + ?Sized, R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua, AnyUserData<'lua>) -> Result, { self.field_getters.push(( - name.as_ref().to_vec(), + name.as_ref().into(), StaticUserDataMethods::::box_function(function), )); } - fn add_field_function_set(&mut self, name: &S, mut function: F) + fn add_field_function_set(&mut self, name: impl AsRef, mut function: F) where - S: AsRef<[u8]> + ?Sized, A: FromLua<'lua>, F: 'static + MaybeSend + FnMut(&'lua Lua, AnyUserData<'lua>, A) -> Result<()>, { self.field_setters.push(( - name.as_ref().to_vec(), + name.as_ref().into(), StaticUserDataMethods::::box_function_mut(move |lua, (data, val)| { function(lua, data, val) }), )); } - fn add_meta_field_with(&mut self, meta: S, f: F) + fn add_meta_field_with(&mut self, meta: impl Into, f: F) where - S: Into, R: ToLua<'lua>, F: 'static + MaybeSend + Fn(&'lua Lua) -> Result, { @@ -559,11 +542,11 @@ impl<'lua, T: 'static + UserData> UserDataFields<'lua, T> for StaticUserDataFiel // Below are internal methods - fn add_field_getter(&mut self, name: Vec, callback: Callback<'lua, 'static>) { + fn add_field_getter(&mut self, name: String, callback: Callback<'lua, 'static>) { self.field_getters.push((name, callback)); } - fn add_field_setter(&mut self, name: Vec, callback: Callback<'lua, 'static>) { + fn add_field_setter(&mut self, name: String, callback: Callback<'lua, 'static>) { self.field_setters.push((name, callback)); } } diff --git a/src/util.rs b/src/util.rs index 45a47f5..726661f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -272,11 +272,7 @@ pub unsafe fn push_table( } // Uses 4 stack spaces, does not call checkstack. -pub unsafe fn rawset_field(state: *mut ffi::lua_State, table: c_int, field: &S) -> Result<()> -where - S: AsRef<[u8]> + ?Sized, -{ - let field = field.as_ref(); +pub unsafe fn rawset_field(state: *mut ffi::lua_State, table: c_int, field: &str) -> Result<()> { ffi::lua_pushvalue(state, table); protect_lua!(state, 2, 0, |state| { ffi::lua_pushlstring(state, field.as_ptr() as *const c_char, field.len()); diff --git a/tests/tests.rs b/tests/tests.rs index a71691e..a984482 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -727,9 +727,9 @@ fn test_set_metatable_nil() -> Result<()> { fn test_named_registry_value() -> Result<()> { let lua = Lua::new(); - lua.set_named_registry_value::<_, i32>("test", 42)?; + lua.set_named_registry_value::("test", 42)?; let f = lua.create_function(move |lua, ()| { - assert_eq!(lua.named_registry_value::<_, i32>("test")?, 42); + assert_eq!(lua.named_registry_value::("test")?, 42); Ok(()) })?; diff --git a/tests/userdata.rs b/tests/userdata.rs index 28f15af..d2b59ca 100644 --- a/tests/userdata.rs +++ b/tests/userdata.rs @@ -408,9 +408,9 @@ fn test_user_values() -> Result<()> { ud.set_named_user_value("name", "alex")?; ud.set_named_user_value("age", 10)?; - assert_eq!(ud.get_named_user_value::<_, String>("name")?, "alex"); - assert_eq!(ud.get_named_user_value::<_, i32>("age")?, 10); - assert_eq!(ud.get_named_user_value::<_, Value>("nonexist")?, Value::Nil); + assert_eq!(ud.get_named_user_value::("name")?, "alex"); + assert_eq!(ud.get_named_user_value::("age")?, 10); + assert_eq!(ud.get_named_user_value::("nonexist")?, Value::Nil); Ok(()) }