Struct 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

Source

pub fn is<T: 'static>(&self) -> bool

Checks whether the type of this userdata is T.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

pub fn set_nth_user_value(&self, n: usize, v: impl IntoLua) -> 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.

Source

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.

Source

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.

Source

pub fn named_user_value<V: FromLua>(&self, name: &str) -> Result<V>

Returns an associated value by name set by set_named_user_value.

Source

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.

Source

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

Source

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 AsRef<AnyUserData> for AnyUserData

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for AnyUserData

Source§

fn clone(&self) -> AnyUserData

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AnyUserData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromLua for AnyUserData

Source§

fn from_lua(value: Value, _: &Lua) -> Result<AnyUserData>

Performs the conversion.
Source§

impl IntoLua for &AnyUserData

Source§

fn into_lua(self, _: &Lua) -> Result<Value>

Performs the conversion.
Source§

impl IntoLua for AnyUserData

Source§

fn into_lua(self, _: &Lua) -> Result<Value>

Performs the conversion.
Source§

impl ObjectLike for AnyUserData

Source§

fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V>

Gets the value associated to key from the object, assuming it has __index metamethod.
Source§

fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()>

Sets the value associated to key in the object, assuming it has __newindex metamethod.
Source§

fn call<R>(&self, args: impl IntoLuaMulti) -> Result<R>
where R: FromLuaMulti,

Calls the object as a function assuming it has __call metamethod. Read more
Source§

fn call_async<R>( &self, args: impl IntoLuaMulti, ) -> impl Future<Output = Result<R>>
where R: FromLuaMulti,

Available on crate feature async only.
Asynchronously calls the object as a function assuming it has __call metamethod. Read more
Source§

fn call_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
where R: FromLuaMulti,

Gets the function associated to key 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,

Available on crate feature async only.
Gets the function associated to key name from the object and asynchronously calls it, passing the object itself along with args as function arguments. Read more
Source§

fn call_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> Result<R>
where R: FromLuaMulti,

Gets the function associated to key name from the object and calls it, passing args as function arguments. Read more
Source§

fn call_async_function<R>( &self, name: &str, args: impl IntoLuaMulti, ) -> impl Future<Output = Result<R>>
where R: FromLuaMulti,

Available on crate feature async only.
Gets the function associated to key name from the object and asynchronously calls it, passing args as function arguments. Read more
Source§

fn to_string(&self) -> Result<StdString>

Converts the object to a string in a human-readable format. Read more
Source§

impl PartialEq for AnyUserData

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for AnyUserData

Source§

fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromLuaMulti for T
where T: FromLua,

Source§

fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<T, Error>

Performs the conversion. Read more
Source§

fn from_lua_args( args: MultiValue, i: usize, to: Option<&str>, lua: &Lua, ) -> Result<T, Error>

Source§

unsafe fn from_stack_multi(nvals: i32, lua: &RawLua) -> Result<T, Error>

Source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &RawLua, ) -> Result<T, Error>

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue, Error>

Performs the conversion.
Source§

unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<i32, Error>

Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> MaybeSend for T
where T: Send,