Struct me3_framework::scripting::lua::prelude::LuaAnyUserData
source · [−]pub struct LuaAnyUserData<'lua>(_);
Expand description
Handle to an internal Lua userdata for any type that implements UserData
.
Similar to std::any::Any
, this provides an interface for dynamic type checking via the is
and borrow
methods.
Internally, instances are stored in a RefCell
, to best match the mutable semantics of the Lua
language.
Note
This API should only be used when necessary. Implementing UserData
already allows defining
methods which check the type and acquire a borrow behind the scenes.
Implementations
sourceimpl<'lua> AnyUserData<'lua>
impl<'lua> AnyUserData<'lua>
sourcepub fn is<T>(&self) -> boolwhere
T: 'static + UserData,
pub fn is<T>(&self) -> boolwhere
T: 'static + UserData,
Checks whether the type of this userdata is T
.
sourcepub fn borrow<T>(&self) -> Result<Ref<'_, T>, Error>where
T: 'static + UserData,
pub fn borrow<T>(&self) -> Result<Ref<'_, T>, Error>where
T: 'static + UserData,
Borrow this userdata immutably if it is of type T
.
Errors
Returns a UserDataBorrowError
if the userdata is already mutably borrowed. Returns a
UserDataTypeMismatch
if the userdata is not of type T
.
sourcepub fn borrow_mut<T>(&self) -> Result<RefMut<'_, T>, Error>where
T: 'static + UserData,
pub fn borrow_mut<T>(&self) -> Result<RefMut<'_, T>, Error>where
T: 'static + UserData,
Borrow this userdata mutably if it is of type T
.
Errors
Returns a UserDataBorrowMutError
if the userdata cannot be mutably borrowed.
Returns a UserDataTypeMismatch
if the userdata is not of type T
.
sourcepub fn take<T>(&self) -> Result<T, Error>where
T: 'static + UserData,
pub fn take<T>(&self) -> Result<T, Error>where
T: 'static + UserData,
Takes out the value of UserData
and sets the special “destructed” metatable that prevents
any further operations with this userdata.
All associated user values will be also cleared.
sourcepub fn set_user_value<V>(&self, v: V) -> Result<(), Error>where
V: ToLua<'lua>,
pub fn set_user_value<V>(&self, v: V) -> Result<(), Error>where
V: ToLua<'lua>,
Sets an associated value to this AnyUserData
.
The value may be any Lua value whatsoever, and can be retrieved with get_user_value
.
This is the same as calling set_nth_user_value
with n
set to 1.
sourcepub fn get_user_value<V>(&self) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn get_user_value<V>(&self) -> Result<V, Error>where
V: FromLua<'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.
sourcepub fn set_nth_user_value<V>(&self, n: usize, v: V) -> Result<(), Error>where
V: ToLua<'lua>,
pub fn set_nth_user_value<V>(&self, n: usize, v: V) -> Result<(), Error>where
V: ToLua<'lua>,
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
.
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.
sourcepub fn get_nth_user_value<V>(&self, n: usize) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn get_nth_user_value<V>(&self, n: usize) -> Result<V, Error>where
V: FromLua<'lua>,
Returns an associated n
th value set by set_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.
sourcepub fn set_named_user_value<S, V>(&self, name: &S, v: V) -> Result<(), Error>where
S: AsRef<[u8]> + ?Sized,
V: ToLua<'lua>,
pub fn set_named_user_value<S, V>(&self, name: &S, v: V) -> Result<(), Error>where
S: AsRef<[u8]> + ?Sized,
V: ToLua<'lua>,
Sets an associated value to this AnyUserData
by name.
The value can be retrieved with get_named_user_value
.
sourcepub fn get_named_user_value<S, V>(&self, name: &S) -> Result<V, Error>where
S: AsRef<[u8]> + ?Sized,
V: FromLua<'lua>,
pub fn get_named_user_value<S, V>(&self, name: &S) -> Result<V, Error>where
S: AsRef<[u8]> + ?Sized,
V: FromLua<'lua>,
Returns an associated value by name set by set_named_user_value
.
sourcepub fn get_metatable(&self) -> Result<UserDataMetatable<'lua>, Error>
pub fn get_metatable(&self) -> Result<UserDataMetatable<'lua>, Error>
Returns a metatable of this UserData
.
Returned UserDataMetatable
object wraps the original metatable and
provides safe access to its methods.
For T: UserData + 'static
returned metatable is shared among all instances of type T
.
Trait Implementations
sourceimpl<'lua> AsRef<AnyUserData<'lua>> for AnyUserData<'lua>
impl<'lua> AsRef<AnyUserData<'lua>> for AnyUserData<'lua>
sourcefn as_ref(&self) -> &AnyUserData<'lua>
fn as_ref(&self) -> &AnyUserData<'lua>
sourceimpl<'lua> Clone for AnyUserData<'lua>
impl<'lua> Clone for AnyUserData<'lua>
sourcefn clone(&self) -> AnyUserData<'lua>
fn clone(&self) -> AnyUserData<'lua>
1.0.0 · sourceconst fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more