Struct mlua::AnyUserData

source ·
pub struct AnyUserData<'lua>(_);
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>

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

source

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.

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

source

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.

source

pub fn set_nth_user_value<V: IntoLua<'lua>>(&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.

source

pub fn nth_user_value<V: FromLua<'lua>>(&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<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.

source

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.

source

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.

source

pub fn into_owned(self) -> OwnedAnyUserData

Available on crate feature unstable and non-crate feature send only.

Convert this handle to owned version.

source§

impl<'lua> AnyUserData<'lua>

source

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>

source§

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

Gets the value associated to key from the userdata, assuming it has __index metamethod.
source§

fn set<K: IntoLua<'lua>, V: IntoLua<'lua>>( &self, key: K, value: V ) -> Result<()>

Sets the value associated to 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>,

Calls the userdata as a function assuming it has __call metamethod. Read more
source§

fn call_async<A, R>(&self, args: A) -> LocalBoxFuture<'lua, Result<R>>where A: IntoLuaMulti<'lua>, R: FromLuaMulti<'lua> + 'lua,

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

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

Calls the userdata method, assuming it has __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,

Available on crate feature async only.
Gets the function associated to key from the table and asynchronously executes it, passing the table itself along with args as function arguments and returning Future. Read more
source§

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

Gets the function associated to key from the table and executes it, passing args as function arguments. Read more
source§

fn call_async_function<A, R>( &self, name: &str, args: A ) -> LocalBoxFuture<'lua, Result<R>>where A: IntoLuaMulti<'lua>, R: FromLuaMulti<'lua> + 'lua,

Available on crate feature async only.
Gets the function associated to key from the table and asynchronously executes it, passing args as function arguments and returning Future. Read more
source§

impl<'lua> AsRef<AnyUserData<'lua>> for AnyUserData<'lua>

source§

fn as_ref(&self) -> &Self

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

impl<'lua> Clone for AnyUserData<'lua>

source§

fn clone(&self) -> AnyUserData<'lua>

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<'lua> Debug for AnyUserData<'lua>

source§

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

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

impl<'lua> FromLua<'lua> for AnyUserData<'lua>

source§

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

Performs the conversion.
source§

impl<'lua> IntoLua<'lua> for AnyUserData<'lua>

source§

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

Performs the conversion.
source§

impl<'lua> PartialEq<AnyUserData<'lua>> for AnyUserData<'lua>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'lua> Serialize for AnyUserData<'lua>

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§

§

impl<'lua> !RefUnwindSafe for AnyUserData<'lua>

§

impl<'lua> !Send for AnyUserData<'lua>

§

impl<'lua> !Sync for AnyUserData<'lua>

§

impl<'lua> Unpin for AnyUserData<'lua>

§

impl<'lua> !UnwindSafe for AnyUserData<'lua>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<'lua, T> FromLuaMulti<'lua> for Twhere T: FromLua<'lua>,

source§

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

Performs the conversion. Read more
source§

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

source§

impl<T, U> Into<U> for Twhere 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<'lua, T> IntoLuaMulti<'lua> for Twhere T: IntoLua<'lua>,

source§

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

Performs the conversion.
source§

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

source§

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.