pub struct AnyUserData { /* private fields */ }Implementations§
Source§impl AnyUserData
impl AnyUserData
pub fn borrow<T>(&self) -> Result<Ref<'_, T>>where
T: 'static,
pub fn borrow_mut<T>(&self) -> Result<RefMut<'_, T>>where
T: 'static,
pub fn with_borrow<T, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R>where
T: 'static,
pub fn with_borrow_mut<T, R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R>where
T: 'static,
Sourcepub fn scoped_borrow<T, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R>where
T: 'static,
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.
Sourcepub fn scoped_borrow_mut<T, R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R>where
T: 'static,
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.
Sourcepub fn delegate<P, S, F>(&self, lua: &Lua, accessor: F) -> Result<AnyUserData>
pub fn delegate<P, S, F>(&self, lua: &Lua, accessor: F) -> Result<AnyUserData>
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?
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 }Sourcepub fn delegate_ref<P, S, F>(
&self,
lua: &Lua,
accessor: F,
) -> Result<AnyUserData>
pub fn delegate_ref<P, S, F>( &self, lua: &Lua, accessor: F, ) -> Result<AnyUserData>
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
impl Clone for AnyUserData
Source§fn clone(&self) -> AnyUserData
fn clone(&self) -> AnyUserData
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more