Skip to main content

AnyUserData

Struct AnyUserData 

Source
pub struct AnyUserData { /* private fields */ }

Implementations§

Source§

impl AnyUserData

Source

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

Source

pub fn borrow_mut<T>(&self) -> Result<RefMut<'_, T>>
where T: 'static,

Source

pub fn with_borrow<T, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R>
where T: 'static,

Source

pub fn with_borrow_mut<T, R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R>
where T: 'static,

Source

pub fn scoped_borrow<T, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R>
where T: 'static,

Rust-side shared borrow of a [Scope::create_userdata] payload. Routes through the scoped cell, so calls after the scope has dropped fail with the same “no longer valid” error a Lua method call would see, instead of returning a stale reference.

Source

pub fn scoped_borrow_mut<T, R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R>
where T: 'static,

Rust-side exclusive borrow of a [Scope::create_userdata] payload. Same invalidation guarantees as Self::scoped_borrow.

Source

pub fn delegate<P, S, F>(&self, lua: &Lua, accessor: F) -> Result<AnyUserData>
where P: UserData, S: UserData, F: Fn(&mut P) -> &mut S + 'static,

Create a sub-userdata in the same scope that re-acquires &mut S from this userdata’s payload via accessor on every method call. The sub-userdata holds no long-lived &mut S: every Lua method call borrows the parent (mut), applies accessor, runs the method, releases. If a script tries to call a parent method while inside a sub-userdata method body, the inner try_borrow_mut surfaces the same “already borrowed” error path scoped cells already use.

Receiver must be a Scope::create_userdata_ref_mut userdata of type P, or another delegated userdata of type P (chains compose).

Scope invalidation propagates: when the originating scope drops, both the parent and every delegated descendant become invalid.

Examples found in repository?
examples/scope_world.rs (line 60)
48    fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
49        m.add_method_mut("spawn", |_, this, ()| {
50            this.entities.push(Position::default());
51            Ok((this.entities.len() - 1) as i64)
52        });
53        m.add_method("count", |_, this, ()| Ok(this.entities.len() as i64));
54
55        // `world:position(i)` returns a sub-userdata holding a live `&mut
56        // Position`, re-borrowed from the World on every field access. No
57        // clone, no write-back: the script mutates the real component.
58        m.add_function("position", |lua, (this, idx): (AnyUserData, i64)| {
59            let i = idx as usize;
60            this.delegate::<World, Position, _>(lua, move |w| &mut w.entities[i])
61        });
62    }
Source

pub fn delegate_ref<P, S, F>( &self, lua: &Lua, accessor: F, ) -> Result<AnyUserData>
where P: UserData, S: UserData, F: Fn(&P) -> &S + 'static,

Shared counterpart to Self::delegate. The accessor takes &P and returns &S, the parent is borrowed shared per call, and the resulting sub-userdata is read-only: a mutating method on it fails with a clean runtime error. Used for &self -> &S accessors.

Trait Implementations§

Source§

impl Clone for AnyUserData

Source§

fn clone(&self) -> AnyUserData

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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: &Lua) -> Result<Self>

Source§

impl IntoLua for &AnyUserData

Source§

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

Source§

impl IntoLua for AnyUserData

Source§

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

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§

const NRESULTS: i32 = const NRESULTS: i32 = 1;

Source§

fn from_lua_multi(values: Vec<Value>, lua: &Lua) -> Result<T, LuaError>

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§

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.