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(&self, v: impl IntoLua) -> Result<()>
pub fn set_user_value(&self, v: impl IntoLua) -> 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(&self, n: usize, v: impl IntoLua) -> Result<()>
pub fn set_nth_user_value(&self, n: usize, v: impl IntoLua) -> 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>(&self, n: usize) -> Result<V>
pub fn nth_user_value<V: FromLua>(&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(&self, name: &str, v: impl IntoLua) -> Result<()>
pub fn set_named_user_value(&self, name: &str, v: impl IntoLua) -> 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
Trait Implementations§
Source§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 ObjectLike for AnyUserData
impl ObjectLike for AnyUserData
Source§fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V>
fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V>
key
from the object, assuming it has __index
metamethod.Source§fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()>
fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()>
key
in the object, assuming it has __newindex
metamethod.Source§fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
__call
metamethod. Read moreSource§fn call_async<R>(
&self,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
fn call_async<R>(
&self,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
async
only.__call
metamethod. Read moreSource§fn call_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
fn call_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
name
from the object and calls it,
passing the object itself along with args
as function arguments.Source§fn call_async_method<R>(
&self,
name: &str,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
fn call_async_method<R>(
&self,
name: &str,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
async
only.name
from the object and asynchronously calls it,
passing the object itself along with args
as function arguments. Read moreSource§fn call_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
fn call_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
name
from the object and calls it,
passing args
as function arguments. Read moreSource§fn call_async_function<R>(
&self,
name: &str,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
fn call_async_function<R>(
&self,
name: &str,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
async
only.name
from the object and asynchronously calls it,
passing args
as function arguments. Read more