Skip to main content

Scope

Struct Scope 

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

Source

pub fn create_function<F, A, R>(&'scope self, func: F) -> Result<Function>
where F: Fn(&Lua, A) -> Result<R> + 'scope, A: FromLuaMulti, R: IntoLuaMulti,

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.

Source

pub fn create_function_mut<F, A, R>(&'scope self, func: F) -> Result<Function>
where F: FnMut(&Lua, A) -> Result<R> + 'scope, A: FromLuaMulti, R: IntoLuaMulti,

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.

Source

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.

Source

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

Auto Trait Implementations§

§

impl<'scope, 'env> !Freeze for Scope<'scope, 'env>

§

impl<'scope, 'env> !RefUnwindSafe for Scope<'scope, 'env>

§

impl<'scope, 'env> !Send for Scope<'scope, 'env>

§

impl<'scope, 'env> !Sync for Scope<'scope, 'env>

§

impl<'scope, 'env> !UnwindSafe for Scope<'scope, 'env>

§

impl<'scope, 'env> Unpin for Scope<'scope, 'env>

§

impl<'scope, 'env> UnsafeUnpin for Scope<'scope, 'env>

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

Source§

impl<T> MaybeSync for T

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.