pub struct LuaAnyUserData<'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>

source

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

Checks whether the type of this userdata is T.

source

pub fn borrow<T>(&self) -> Result<Ref<'_, T>, Error>
where T: 'static,

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>(&self) -> Result<RefMut<'_, T>, Error>
where T: 'static,

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>(&self) -> Result<T, Error>
where T: 'static,

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>(&self, v: V) -> Result<(), Error>
where V: IntoLua<'lua>,

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>(&self) -> Result<V, Error>
where V: FromLua<'lua>,

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>(&self, n: usize, v: V) -> Result<(), Error>
where V: IntoLua<'lua>,

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>(&self, n: usize) -> Result<V, Error>
where V: FromLua<'lua>,

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>(&self, name: &str, v: V) -> Result<(), Error>
where V: IntoLua<'lua>,

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>(&self, name: &str) -> Result<V, Error>
where V: FromLua<'lua>,

Returns an associated value by name set by set_named_user_value.

source

pub fn get_metatable(&self) -> Result<UserDataMetatable<'lua>, Error>

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§

impl<'lua> AnyUserData<'lua>

source

pub fn wrap<T>(data: T) -> impl IntoLua<'lua>
where T: MaybeSend + 'static,

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, V>(&self, key: K) -> Result<V, Error>
where K: IntoLua<'lua>, V: FromLua<'lua>,

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

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

Sets the value associated to key in the userdata, assuming it has __newindex metamethod.
source§

fn call<A, R>(&self, args: A) -> Result<R, Error>
where A: IntoLuaMulti<'lua>, R: FromLuaMulti<'lua>,

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, Error>
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_function<A, R>(&self, name: &str, args: A) -> Result<R, Error>
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§

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

source§

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

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<(), Error>

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>, Error>

Performs the conversion.
source§

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

source§

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

Performs the conversion.
source§

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

source§

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

Performs the conversion.
source§

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

source§

fn eq(&self, other: &AnyUserData<'lua>) -> 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.

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 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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<'lua, T> FromLuaMulti<'lua> for T
where 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_args( args: MultiValue<'lua>, i: usize, to: Option<&str>, lua: &'lua Lua ) -> Result<T, Error>

source§

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

source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &'lua Lua ) -> 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<'lua, T> IntoLuaMulti<'lua> for T
where T: IntoLua<'lua>,

source§

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

Performs the conversion.
source§

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

source§

impl<'lua, T> ToLuaCompat<'lua> for T
where T: IntoLua<'lua>,

source§

fn to_lua(self, context: &'lua Lua) -> Result<Value<'lua>, Error>

👎Deprecated: ToLua::to_lua has become IntoLua::into_lua
source§

impl<T> ToOwned for T
where 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 T
where 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 T
where 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.