Struct rlua::AnyUserData
source · pub struct AnyUserData<'lua>(/* private fields */);
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§
source§impl<'lua> AnyUserData<'lua>
impl<'lua> AnyUserData<'lua>
sourcepub fn borrow<T>(&self) -> Result<Ref<'_, T>, Error>where
T: 'static,
pub fn borrow<T>(&self) -> Result<Ref<'_, T>, Error>where
T: 'static,
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,
pub fn borrow_mut<T>(&self) -> Result<RefMut<'_, T>, Error>where
T: 'static,
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,
pub fn take<T>(&self) -> Result<T, Error>where
T: 'static,
Takes the value out of this userdata. Sets the special “destructed” metatable that prevents any further operations with this userdata.
Keeps associated user values unchanged (they will be collected by Lua’s GC).
sourcepub fn set_user_value<V>(&self, v: V) -> Result<(), Error>where
V: IntoLua<'lua>,
pub fn set_user_value<V>(&self, v: V) -> Result<(), Error>where
V: IntoLua<'lua>,
Sets an associated value to this AnyUserData
.
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.
sourcepub fn user_value<V>(&self) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn 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 nth_user_value
with n
set to 1.
sourcepub fn set_nth_user_value<V>(&self, n: usize, v: V) -> Result<(), Error>where
V: IntoLua<'lua>,
pub fn set_nth_user_value<V>(&self, n: usize, v: V) -> Result<(), Error>where
V: IntoLua<'lua>,
Sets an associated n
th value to this AnyUserData
.
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.
sourcepub fn nth_user_value<V>(&self, n: usize) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn 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<V>(&self, name: &str, v: V) -> Result<(), Error>where
V: IntoLua<'lua>,
pub fn set_named_user_value<V>(&self, name: &str, v: V) -> Result<(), Error>where
V: IntoLua<'lua>,
Sets an associated value to this AnyUserData
by name.
The value can be retrieved with named_user_value
.
sourcepub fn named_user_value<V>(&self, name: &str) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn named_user_value<V>(&self, name: &str) -> Result<V, Error>where
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: 'static
returned metatable is shared among all instances of type T
.
source§impl<'lua> AnyUserData<'lua>
impl<'lua> AnyUserData<'lua>
sourcepub fn wrap<T>(data: T) -> impl IntoLua<'lua>where
T: MaybeSend + 'static,
pub fn wrap<T>(data: T) -> impl IntoLua<'lua>where
T: MaybeSend + 'static,
Wraps any Rust type, returning an opaque type that implements IntoLua
trait.
This function uses Lua::create_any_userdata()
under the hood.
Trait Implementations§
source§impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua>
impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua>
source§fn get<K, V>(&self, key: K) -> Result<V, Error>
fn get<K, V>(&self, key: K) -> Result<V, Error>
key
from the userdata, assuming it has __index
metamethod.source§fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
key
in the userdata, assuming it has __newindex
metamethod.source§fn call<A, R>(&self, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
fn call<A, R>(&self, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
__call
metamethod. Read moresource§fn call_method<A, R>(&self, name: &str, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
fn call_method<A, R>(&self, name: &str, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
__index
metamethod
and a function associated to name
.source§fn call_function<A, R>(&self, name: &str, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
fn call_function<A, R>(&self, name: &str, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
key
from the table and executes it,
passing args
as function arguments. Read moresource§impl<'lua> AsRef<AnyUserData<'lua>> for AnyUserData<'lua>
impl<'lua> AsRef<AnyUserData<'lua>> for AnyUserData<'lua>
source§fn as_ref(&self) -> &AnyUserData<'lua>
fn as_ref(&self) -> &AnyUserData<'lua>
source§impl<'lua> Clone for AnyUserData<'lua>
impl<'lua> Clone for AnyUserData<'lua>
source§fn clone(&self) -> AnyUserData<'lua>
fn clone(&self) -> AnyUserData<'lua>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'lua> Debug for AnyUserData<'lua>
impl<'lua> Debug for AnyUserData<'lua>
source§impl<'lua> FromLua<'lua> for AnyUserData<'lua>
impl<'lua> FromLua<'lua> for AnyUserData<'lua>
source§impl<'lua> IntoLua<'lua> for &AnyUserData<'lua>
impl<'lua> IntoLua<'lua> for &AnyUserData<'lua>
source§impl<'lua> IntoLua<'lua> for AnyUserData<'lua>
impl<'lua> IntoLua<'lua> for AnyUserData<'lua>
source§impl<'lua> PartialEq for AnyUserData<'lua>
impl<'lua> PartialEq for AnyUserData<'lua>
source§fn eq(&self, other: &AnyUserData<'lua>) -> bool
fn eq(&self, other: &AnyUserData<'lua>) -> bool
self
and other
values to be equal, and is used
by ==
.