pub struct Scope<'scope, 'env: 'scope> { /* private fields */ }Expand description
A scope for creating lifetime-bounded Lua callbacks and userdata.
Mirrors mlua::Scope. See the module docs and Lua::scope for the
full picture, including the soundness argument for the lifetime erasure.
Implementations§
Source§impl<'scope, 'env: 'scope> Scope<'scope, 'env>
impl<'scope, 'env: 'scope> Scope<'scope, 'env>
Sourcepub fn create_function<F, A, R>(&'scope self, func: F) -> Result<Function>
pub fn create_function<F, A, R>(&'scope self, func: F) -> Result<Function>
Wrap a non-'static Rust closure into a callable Lua Function that is
invalidated when the scope ends.
This is the scoped version of Lua::create_function: the closure may
borrow data living for 'scope. See the module docs for why the
lifetime erasure is sound.
Sourcepub fn create_function_mut<F, A, R>(&'scope self, func: F) -> Result<Function>
pub fn create_function_mut<F, A, R>(&'scope self, func: F) -> Result<Function>
Wrap a non-'static mutable Rust closure into a callable Lua Function
that is invalidated when the scope ends.
This is the scoped version of create_function_mut. The closure is
guarded by a RefCell; re-entrant calls (the callback triggering Lua
that calls the same callback) surface as a runtime error rather than a
borrow panic, matching mlua’s RecursiveMutCallback intent.
Sourcepub fn create_userdata<T>(&'scope self, data: T) -> Result<AnyUserData>where
T: UserData + 'env,
pub fn create_userdata<T>(&'scope self, data: T) -> Result<AnyUserData>where
T: UserData + 'env,
Create a Lua userdata wrapping a non-'static T: UserData, invalidated
when the scope ends.
This is the scoped version of Lua::create_userdata: T need not be
'static, so it may borrow 'env data. The trade-off (matching mlua) is
that the userdata carries no TypeId, so the value cannot be read back
out by concrete type from an AnyUserData handle — only metatable
method/field/meta dispatch is supported. After the scope ends, any access
from Lua errors with Error::UserDataDestructed.
Sourcepub fn add_destructor(&'scope self, destructor: impl FnOnce() + 'env)
pub fn add_destructor(&'scope self, destructor: impl FnOnce() + 'env)
Register an arbitrary destructor to run when the scope ends.
Mirrors mlua::Scope::add_destructor. Useful for cleaning up resources
tied to the scope. Destructors run in reverse registration order on every
exit path (normal, ?, or panic).