pub struct Scope<'scope, 'env: 'scope> { /* private fields */ }
Expand description
Constructed by the Lua::scope
method, allows temporarily creating Lua userdata and
callbacks that are not required to be Send
or 'static
.
See Lua::scope
for more details.
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>
Wraps a Rust function or closure, creating a callable Lua function handle to it.
This is a version of Lua::create_function
that creates a callback which expires on
scope drop. See Lua::scope
for more details.
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>
Wraps a Rust mutable closure, creating a callable Lua function handle to it.
This is a version of Lua::create_function_mut
that creates a callback which expires
on scope drop. See Lua::scope
and Scope::create_function
for more details.
Sourcepub fn create_userdata_ref<T>(
&'scope self,
data: &'env T,
) -> Result<AnyUserData>where
T: UserData + 'static,
pub fn create_userdata_ref<T>(
&'scope self,
data: &'env T,
) -> Result<AnyUserData>where
T: UserData + 'static,
Creates a Lua userdata object from a reference to custom userdata type.
This is a version of Lua::create_userdata
that creates a userdata which expires on
scope drop, and does not require that the userdata type be Send. This method takes
non-’static reference to the data. See Lua::scope
for more details.
Userdata created with this method will not be able to be mutated from Lua.
Sourcepub fn create_userdata_ref_mut<T>(
&'scope self,
data: &'env mut T,
) -> Result<AnyUserData>where
T: UserData + 'static,
pub fn create_userdata_ref_mut<T>(
&'scope self,
data: &'env mut T,
) -> Result<AnyUserData>where
T: UserData + 'static,
Creates a Lua userdata object from a mutable reference to custom userdata type.
This is a version of Lua::create_userdata
that creates a userdata which expires on
scope drop, and does not require that the userdata type be Send. This method takes
non-’static mutable reference to the data. See Lua::scope
for more details.
Sourcepub fn create_any_userdata_ref<T>(
&'scope self,
data: &'env T,
) -> Result<AnyUserData>where
T: 'static,
pub fn create_any_userdata_ref<T>(
&'scope self,
data: &'env T,
) -> Result<AnyUserData>where
T: 'static,
Creates a Lua userdata object from a reference to custom Rust type.
This is a version of Lua::create_any_userdata
that creates a userdata which expires on
scope drop, and does not require that the Rust type be Send. This method takes non-’static
reference to the data. See Lua::scope
for more details.
Userdata created with this method will not be able to be mutated from Lua.
Sourcepub fn create_any_userdata_ref_mut<T>(
&'scope self,
data: &'env mut T,
) -> Result<AnyUserData>where
T: 'static,
pub fn create_any_userdata_ref_mut<T>(
&'scope self,
data: &'env mut T,
) -> Result<AnyUserData>where
T: 'static,
Creates a Lua userdata object from a mutable reference to custom Rust type.
This is a version of Lua::create_any_userdata
that creates a userdata which expires on
scope drop, and does not require that the Rust type be Send. This method takes non-’static
mutable reference to the data. See Lua::scope
for more details.
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,
Creates a Lua userdata object from a custom userdata type.
This is a version of Lua::create_userdata
that creates a userdata which expires on
scope drop, and does not require that the userdata type be Send
or 'static
. See
Lua::scope
for more details.
The main limitation that comes from using non-’static userdata is that the produced userdata
will no longer have a TypeId
associated with it, because TypeId
can only work for
'static
types. This means that it is impossible, once the userdata is created, to get a
reference to it back out of an AnyUserData
handle. This also implies that the
“function” type methods that can be added via UserDataMethods
(the ones that accept
AnyUserData
as a first parameter) are vastly less useful. Also, there is no way to
re-use a single metatable for multiple non-’static types, so there is a higher cost
associated with creating the userdata metatable each time a new userdata is created.
Sourcepub fn create_any_userdata<T>(
&'scope self,
data: T,
register: impl FnOnce(&mut UserDataRegistry<T>),
) -> Result<AnyUserData>where
T: 'env,
pub fn create_any_userdata<T>(
&'scope self,
data: T,
register: impl FnOnce(&mut UserDataRegistry<T>),
) -> Result<AnyUserData>where
T: 'env,
Creates a Lua userdata object from a custom Rust type.
Since the Rust type is not required to be static and implement UserData
trait,
you need to provide a function to register fields or methods for the object.
See also Scope::create_userdata
for more details about non-static limitations.
Sourcepub fn add_destructor(&'scope self, destructor: impl FnOnce() + 'env)
pub fn add_destructor(&'scope self, destructor: impl FnOnce() + 'env)
Adds a destructor function to be run when the scope ends.
This functionality is useful for cleaning up any resources after the scope ends.
§Example
let lua = Lua::new();
let ud = lua.create_any_userdata(String::from("hello"))?;
lua.scope(|scope| {
scope.add_destructor(|| {
_ = ud.take::<String>();
});
// Run the code that uses `ud` here
Ok(())
})?;
assert!(matches!(ud.borrow::<String>(), Err(Error::UserDataDestructed)));
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> Unpin for Scope<'scope, 'env>
impl<'scope, 'env> !UnwindSafe for Scope<'scope, 'env>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more