mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Rename AnyUserData::get_*_user_value to AnyUserData::*_user_value.
To be more consistent with function like `Lua::named_registry_value`, `Lua::add_data_ref` and os on.
This commit is contained in:
+2
-2
@@ -187,14 +187,14 @@ pub(crate) struct DestructedUserdata;
|
||||
/// Be warned, If you place this into Lua via a [`UserData`] type or a rust callback, it is *very
|
||||
/// easy* to accidentally cause reference cycles that the Lua garbage collector cannot resolve.
|
||||
/// Instead of placing a [`RegistryKey`] into a [`UserData`] type, prefer instead to use
|
||||
/// [`AnyUserData::set_user_value`] / [`AnyUserData::get_user_value`].
|
||||
/// [`AnyUserData::set_user_value`] / [`AnyUserData::user_value`].
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
/// [`RegistryKey`]: crate::RegistryKey
|
||||
/// [`Lua::remove_registry_value`]: crate::Lua::remove_registry_value
|
||||
/// [`Lua::expire_registry_values`]: crate::Lua::expire_registry_values
|
||||
/// [`AnyUserData::set_user_value`]: crate::AnyUserData::set_user_value
|
||||
/// [`AnyUserData::get_user_value`]: crate::AnyUserData::get_user_value
|
||||
/// [`AnyUserData::user_value`]: crate::AnyUserData::user_value
|
||||
pub struct RegistryKey {
|
||||
pub(crate) registry_id: c_int,
|
||||
pub(crate) is_nil: AtomicBool,
|
||||
|
||||
+30
-20
@@ -862,11 +862,11 @@ impl<'lua> AnyUserData<'lua> {
|
||||
|
||||
/// Sets an associated value to this `AnyUserData`.
|
||||
///
|
||||
/// The value may be any Lua value whatsoever, and can be retrieved with [`get_user_value`].
|
||||
/// The value may be any Lua value whatsoever, and can be retrieved with [`user_value`].
|
||||
///
|
||||
/// This is the same as calling [`set_nth_user_value`] with `n` set to 1.
|
||||
///
|
||||
/// [`get_user_value`]: #method.get_user_value
|
||||
/// [`user_value`]: #method.user_value
|
||||
/// [`set_nth_user_value`]: #method.set_nth_user_value
|
||||
#[inline]
|
||||
pub fn set_user_value<V: IntoLua<'lua>>(&self, v: V) -> Result<()> {
|
||||
@@ -875,25 +875,31 @@ impl<'lua> AnyUserData<'lua> {
|
||||
|
||||
/// Returns an associated value set by [`set_user_value`].
|
||||
///
|
||||
/// This is the same as calling [`get_nth_user_value`] with `n` set to 1.
|
||||
/// This is the same as calling [`nth_user_value`] with `n` set to 1.
|
||||
///
|
||||
/// [`set_user_value`]: #method.set_user_value
|
||||
/// [`get_nth_user_value`]: #method.get_nth_user_value
|
||||
/// [`nth_user_value`]: #method.nth_user_value
|
||||
#[inline]
|
||||
pub fn user_value<V: FromLua<'lua>>(&self) -> Result<V> {
|
||||
self.nth_user_value(1)
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.9.0", note = "please use `user_value` instead")]
|
||||
pub fn get_user_value<V: FromLua<'lua>>(&self) -> Result<V> {
|
||||
self.get_nth_user_value(1)
|
||||
self.nth_user_value(1)
|
||||
}
|
||||
|
||||
/// Sets an associated `n`th value to this `AnyUserData`.
|
||||
///
|
||||
/// The value may be any Lua value whatsoever, and can be retrieved with [`get_nth_user_value`].
|
||||
/// The value may be any Lua value whatsoever, and can be retrieved with [`nth_user_value`].
|
||||
/// `n` starts from 1 and can be up to 65535.
|
||||
///
|
||||
/// This is supported for all Lua versions.
|
||||
/// In Lua 5.4 first 7 elements are stored in a most efficient way.
|
||||
/// For other Lua versions this functionality is provided using a wrapping table.
|
||||
///
|
||||
/// [`get_nth_user_value`]: #method.get_nth_user_value
|
||||
/// [`nth_user_value`]: #method.nth_user_value
|
||||
pub fn set_nth_user_value<V: IntoLua<'lua>>(&self, n: usize, v: V) -> Result<()> {
|
||||
if n < 1 || n > u16::MAX as usize {
|
||||
return Err(Error::RuntimeError(
|
||||
@@ -949,7 +955,7 @@ impl<'lua> AnyUserData<'lua> {
|
||||
/// For other Lua versions this functionality is provided using a wrapping table.
|
||||
///
|
||||
/// [`set_nth_user_value`]: #method.set_nth_user_value
|
||||
pub fn get_nth_user_value<V: FromLua<'lua>>(&self, n: usize) -> Result<V> {
|
||||
pub fn nth_user_value<V: FromLua<'lua>>(&self, n: usize) -> Result<V> {
|
||||
if n < 1 || n > u16::MAX as usize {
|
||||
return Err(Error::RuntimeError(
|
||||
"user value index out of bounds".to_string(),
|
||||
@@ -986,18 +992,20 @@ impl<'lua> AnyUserData<'lua> {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.9.0", note = "please use `nth_user_value` instead")]
|
||||
pub fn get_nth_user_value<V: FromLua<'lua>>(&self, n: usize) -> Result<V> {
|
||||
self.nth_user_value(n)
|
||||
}
|
||||
|
||||
/// Sets an associated value to this `AnyUserData` by name.
|
||||
///
|
||||
/// The value can be retrieved with [`get_named_user_value`].
|
||||
/// The value can be retrieved with [`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<()>
|
||||
where
|
||||
V: IntoLua<'lua>,
|
||||
{
|
||||
/// [`named_user_value`]: #method.named_user_value
|
||||
pub fn set_named_user_value<V: IntoLua<'lua>>(&self, name: &str, v: V) -> Result<()> {
|
||||
let lua = self.0.lua;
|
||||
let state = lua.state();
|
||||
let name = name.as_ref();
|
||||
unsafe {
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 5)?;
|
||||
@@ -1030,13 +1038,9 @@ 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>
|
||||
where
|
||||
V: FromLua<'lua>,
|
||||
{
|
||||
pub fn named_user_value<V: FromLua<'lua>>(&self, name: &str) -> Result<V> {
|
||||
let lua = self.0.lua;
|
||||
let state = lua.state();
|
||||
let name = name.as_ref();
|
||||
unsafe {
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 4)?;
|
||||
@@ -1057,6 +1061,12 @@ impl<'lua> AnyUserData<'lua> {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.9.0", note = "please use `named_user_value` instead")]
|
||||
pub fn get_named_user_value<V: FromLua<'lua>>(&self, name: &str) -> Result<V> {
|
||||
self.named_user_value(name)
|
||||
}
|
||||
|
||||
/// Returns a metatable of this `UserData`.
|
||||
///
|
||||
/// Returned [`UserDataMetatable`] object wraps the original metatable and
|
||||
|
||||
+10
-10
@@ -411,21 +411,21 @@ fn test_user_values() -> Result<()> {
|
||||
ud.set_nth_user_value(1, "hello")?;
|
||||
ud.set_nth_user_value(2, "world")?;
|
||||
ud.set_nth_user_value(65535, 321)?;
|
||||
assert_eq!(ud.get_nth_user_value::<String>(1)?, "hello");
|
||||
assert_eq!(ud.get_nth_user_value::<String>(2)?, "world");
|
||||
assert_eq!(ud.get_nth_user_value::<Value>(3)?, Value::Nil);
|
||||
assert_eq!(ud.get_nth_user_value::<i32>(65535)?, 321);
|
||||
assert_eq!(ud.nth_user_value::<String>(1)?, "hello");
|
||||
assert_eq!(ud.nth_user_value::<String>(2)?, "world");
|
||||
assert_eq!(ud.nth_user_value::<Value>(3)?, Value::Nil);
|
||||
assert_eq!(ud.nth_user_value::<i32>(65535)?, 321);
|
||||
|
||||
assert!(ud.get_nth_user_value::<Value>(0).is_err());
|
||||
assert!(ud.get_nth_user_value::<Value>(65536).is_err());
|
||||
assert!(ud.nth_user_value::<Value>(0).is_err());
|
||||
assert!(ud.nth_user_value::<Value>(65536).is_err());
|
||||
|
||||
// Named user values
|
||||
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.named_user_value::<String>("name")?, "alex");
|
||||
assert_eq!(ud.named_user_value::<i32>("age")?, 10);
|
||||
assert_eq!(ud.named_user_value::<Value>("nonexist")?, Value::Nil);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -495,7 +495,7 @@ fn test_fields() -> Result<()> {
|
||||
});
|
||||
|
||||
// Use userdata "uservalue" storage
|
||||
fields.add_field_function_get("uval", |_, ud| ud.get_user_value::<Option<String>>());
|
||||
fields.add_field_function_get("uval", |_, ud| ud.user_value::<Option<String>>());
|
||||
fields
|
||||
.add_field_function_set("uval", |_, ud, s| ud.set_user_value::<Option<String>>(s));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user