Struct mlua::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: 'static>(&self) -> Result<Ref<'_, T>>
pub fn borrow<T: 'static>(&self) -> Result<Ref<'_, 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<RefMut<'_, T>>
pub fn borrow_mut<T: 'static>(&self) -> Result<RefMut<'_, 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<'lua>>(&self, v: V) -> Result<()>
pub fn set_user_value<V: IntoLua<'lua>>(&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<'lua>>(&self) -> Result<V>
pub fn user_value<V: FromLua<'lua>>(&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<'lua>>(&self, n: usize, v: V) -> Result<()>
pub fn set_nth_user_value<V: IntoLua<'lua>>(&self, n: usize, v: V) -> Result<()>
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: FromLua<'lua>>(&self, n: usize) -> Result<V>
pub fn nth_user_value<V: FromLua<'lua>>(&self, n: usize) -> Result<V>
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: IntoLua<'lua>>(
&self,
name: &str,
v: V
) -> Result<()>
pub fn set_named_user_value<V: IntoLua<'lua>>( &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<'lua>>(&self, name: &str) -> Result<V>
pub fn named_user_value<V: FromLua<'lua>>(&self, name: &str) -> Result<V>
Returns an associated value by name set by set_named_user_value
.
sourcepub fn get_metatable(&self) -> Result<UserDataMetatable<'lua>>
pub fn get_metatable(&self) -> Result<UserDataMetatable<'lua>>
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.
sourcepub fn into_owned(self) -> OwnedAnyUserData
Available on crate feature unstable
and non-crate feature send
only.
pub fn into_owned(self) -> OwnedAnyUserData
unstable
and non-crate feature send
only.Convert this handle to owned version.
source§impl<'lua> AnyUserData<'lua>
impl<'lua> AnyUserData<'lua>
sourcepub fn wrap<T: MaybeSend + 'static>(data: T) -> impl IntoLua<'lua>
pub fn wrap<T: MaybeSend + 'static>(data: T) -> impl IntoLua<'lua>
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: IntoLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V>
fn get<K: IntoLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V>
key
from the userdata, assuming it has __index
metamethod.source§fn set<K: IntoLua<'lua>, V: IntoLua<'lua>>(
&self,
key: K,
value: V
) -> Result<()>
fn set<K: IntoLua<'lua>, V: IntoLua<'lua>>( &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<'lua>,
R: FromLuaMulti<'lua>,
fn call<A, R>(&self, args: A) -> Result<R>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
__call
metamethod. Read moresource§fn call_async<A, R>(&self, args: A) -> LocalBoxFuture<'lua, Result<R>>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua> + 'lua,
fn call_async<A, R>(&self, args: A) -> LocalBoxFuture<'lua, Result<R>>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua> + 'lua,
async
only.__call
metamethod. Read moresource§fn call_method<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
fn call_method<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
__index
metamethod
and a function associated to name
.source§fn call_async_method<A, R>(
&self,
name: &str,
args: A
) -> LocalBoxFuture<'lua, Result<R>>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua> + 'lua,
fn call_async_method<A, R>(
&self,
name: &str,
args: A
) -> LocalBoxFuture<'lua, Result<R>>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua> + 'lua,
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<'lua>,
R: FromLuaMulti<'lua>,
fn call_function<A, R>(&self, name: &str, args: A) -> Result<R>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
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
) -> LocalBoxFuture<'lua, Result<R>>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua> + 'lua,
fn call_async_function<A, R>(
&self,
name: &str,
args: A
) -> LocalBoxFuture<'lua, Result<R>>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua> + 'lua,
async
only.key
from the table and asynchronously executes it,
passing args
as function arguments and returning Future. Read moresource§impl<'lua> AsRef<AnyUserData<'lua>> for AnyUserData<'lua>
impl<'lua> AsRef<AnyUserData<'lua>> for 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 more