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.
§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 DataTypeMismatch
if the userdata is not of type T
or if it’s
scoped.
Sourcepub fn borrow_scoped<T: 'static, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R>
pub fn borrow_scoped<T: 'static, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R>
Borrow this userdata immutably if it is of type T
, passing the borrowed value
to the closure.
This method is the only way to borrow scoped userdata (created inside Lua::scope
).
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
or if it’s
scoped.
Sourcepub fn borrow_mut_scoped<T: 'static, R>(
&self,
f: impl FnOnce(&mut T) -> R,
) -> Result<R>
pub fn borrow_mut_scoped<T: 'static, R>( &self, f: impl FnOnce(&mut T) -> R, ) -> Result<R>
Borrow this userdata mutably if it is of type T
, passing the borrowed value
to the closure.
This method is the only way to borrow scoped userdata (created inside Lua::scope
).
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 destroy(&self) -> Result<()>
pub fn destroy(&self) -> Result<()>
Destroys this userdata.
This is similar to AnyUserData::take
, but it doesn’t require a type.
This method works for non-scoped userdata only.
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 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 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 metatable(&self) -> Result<UserDataMetatable>
pub fn metatable(&self) -> Result<UserDataMetatable>
Returns a metatable of this AnyUserData
.
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 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 moreSource§impl PartialEq for AnyUserData
impl PartialEq for AnyUserData
Source§impl Serialize for AnyUserData
impl Serialize for AnyUserData
impl StructuralPartialEq 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§impl<T> FromLuaMulti for Twhere
T: FromLua,
impl<T> FromLuaMulti for Twhere
T: FromLua,
Source§fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<T, Error>
fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<T, Error>
fn from_lua_args( args: MultiValue, i: usize, to: Option<&str>, lua: &Lua, ) -> Result<T, Error>
unsafe fn from_stack_multi(nvals: i32, lua: &RawLua) -> Result<T, Error>
unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &RawLua, ) -> Result<T, Error>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more