Struct mlua::AnyUserData
source · pub struct AnyUserData(/* 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 AnyUserData
impl AnyUserData
sourcepub fn borrow<T: 'static>(&self) -> Result<UserDataRef<T>>
pub fn borrow<T: 'static>(&self) -> Result<UserDataRef<T>>
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: 'static>(&self) -> Result<UserDataRefMut<T>>
pub fn borrow_mut<T: 'static>(&self) -> Result<UserDataRefMut<T>>
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: 'static>(&self) -> Result<T>
pub fn take<T: 'static>(&self) -> Result<T>
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: IntoLua>(&self, v: V) -> Result<()>
pub fn set_user_value<V: IntoLua>(&self, v: V) -> Result<()>
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: FromLua>(&self) -> Result<V>
pub fn user_value<V: FromLua>(&self) -> Result<V>
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: IntoLua>(&self, n: usize, v: V) -> Result<()>
pub fn set_nth_user_value<V: IntoLua>(&self, n: usize, v: V) -> Result<()>
Sets an associated nth 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: FromLua>(&self, n: usize) -> Result<V>
pub fn nth_user_value<V: FromLua>(&self, n: usize) -> Result<V>
Returns an associated nth 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: IntoLua>(&self, name: &str, v: V) -> Result<()>
pub fn set_named_user_value<V: IntoLua>(&self, name: &str, v: V) -> Result<()>
Sets an associated value to this AnyUserData by name.
The value can be retrieved with named_user_value.
sourcepub fn named_user_value<V: FromLua>(&self, name: &str) -> Result<V>
pub fn named_user_value<V: FromLua>(&self, name: &str) -> Result<V>
Returns an associated value by name set by set_named_user_value.
sourcepub fn get_metatable(&self) -> Result<UserDataMetatable>
pub fn get_metatable(&self) -> Result<UserDataMetatable>
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.
sourcepub fn to_pointer(&self) -> *const c_void
pub fn to_pointer(&self) -> *const c_void
Converts this userdata to a generic C pointer.
There is no way to convert the pointer back to its original value.
Typically this function is used only for hashing and debug information.
source§impl AnyUserData
impl AnyUserData
sourcepub fn wrap<T: MaybeSend + 'static>(data: T) -> impl IntoLua
pub fn wrap<T: MaybeSend + 'static>(data: T) -> impl IntoLua
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 AnyUserDataExt for AnyUserData
impl AnyUserDataExt for AnyUserData
source§fn get<K: IntoLua, V: FromLua>(&self, key: K) -> Result<V>
fn get<K: IntoLua, V: FromLua>(&self, key: K) -> Result<V>
key from the userdata, assuming it has __index metamethod.source§fn set<K: IntoLua, V: IntoLua>(&self, key: K, value: V) -> Result<()>
fn set<K: IntoLua, V: IntoLua>(&self, key: K, value: V) -> Result<()>
key in the userdata, assuming it has __newindex metamethod.source§fn call<A, R>(&self, args: A) -> Result<R>where
A: IntoLuaMulti,
R: FromLuaMulti,
fn call<A, R>(&self, args: A) -> Result<R>where
A: IntoLuaMulti,
R: FromLuaMulti,
__call metamethod. Read moresource§fn call_async<A, R>(&self, args: A) -> impl Future<Output = Result<R>>where
A: IntoLuaMulti,
R: FromLuaMulti,
fn call_async<A, R>(&self, args: A) -> impl Future<Output = Result<R>>where
A: IntoLuaMulti,
R: FromLuaMulti,
async only.__call metamethod. Read moresource§fn call_method<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti,
R: FromLuaMulti,
fn call_method<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti,
R: FromLuaMulti,
__index metamethod
and a function associated to name.source§fn call_async_method<A, R>(
&self,
name: &str,
args: A,
) -> impl Future<Output = Result<R>>where
A: IntoLuaMulti,
R: FromLuaMulti,
fn call_async_method<A, R>(
&self,
name: &str,
args: A,
) -> impl Future<Output = Result<R>>where
A: IntoLuaMulti,
R: FromLuaMulti,
async only.key from the table and asynchronously executes it,
passing the table itself along with args as function arguments and returning Future. Read moresource§fn call_function<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti,
R: FromLuaMulti,
fn call_function<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti,
R: FromLuaMulti,
key from the table and executes it,
passing args as function arguments. Read moresource§fn call_async_function<A, R>(
&self,
name: &str,
args: A,
) -> impl Future<Output = Result<R>>where
A: IntoLuaMulti,
R: FromLuaMulti,
fn call_async_function<A, R>(
&self,
name: &str,
args: A,
) -> impl Future<Output = Result<R>>where
A: IntoLuaMulti,
R: FromLuaMulti,
async only.key from the table and asynchronously executes it,
passing args as function arguments and returning Future. Read moresource§impl AsRef<AnyUserData> for AnyUserData
impl AsRef<AnyUserData> for AnyUserData
source§impl Clone for AnyUserData
impl Clone for AnyUserData
source§fn clone(&self) -> AnyUserData
fn clone(&self) -> AnyUserData
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for AnyUserData
impl Debug for AnyUserData
source§impl FromLua for AnyUserData
impl FromLua for AnyUserData
source§impl IntoLua for &AnyUserData
impl IntoLua for &AnyUserData
source§impl IntoLua for AnyUserData
impl IntoLua for AnyUserData
source§impl PartialEq for AnyUserData
impl PartialEq for AnyUserData
Auto Trait Implementations§
impl Freeze for AnyUserData
impl !RefUnwindSafe for AnyUserData
impl Send for AnyUserData
impl Sync for AnyUserData
impl Unpin for AnyUserData
impl !UnwindSafe for AnyUserData
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)