Struct Chunk

Source
pub struct Chunk<'a> { /* private fields */ }
Available on crate feature mlua only.
Expand description

Returned from Lua::load and is used to finalize loading and executing Lua main chunks.

Implementations§

Source§

impl Chunk<'_>

Source

pub fn name(&self) -> &str

Returns the name of this chunk.

Source

pub fn set_name(self, name: impl Into<String>) -> Chunk<'_>

Sets the name of this chunk, which results in more informative error traces.

Possible name prefixes:

  • @ - file path (when truncation is needed, the end of the file path is kept, as this is more useful for identifying the file)
  • = - custom chunk name (when truncation is needed, the beginning of the name is kept)
Source

pub fn environment(&self) -> Option<&Table>

Returns the environment of this chunk.

Source

pub fn set_environment(self, env: Table) -> Chunk<'_>

Sets the environment of the loaded chunk to the given value.

In Lua >=5.2 main chunks always have exactly one upvalue, and this upvalue is used as the _ENV variable inside the chunk. By default this value is set to the global environment.

Calling this method changes the _ENV upvalue to the value provided, and variables inside the chunk will refer to the given environment rather than the global one.

All global variables (including the standard library!) are looked up in _ENV, so it may be necessary to populate the environment in order for scripts using custom environments to be useful.

Source

pub fn mode(&self) -> ChunkMode

Returns the mode (auto-detected by default) of this chunk.

Source

pub fn set_mode(self, mode: ChunkMode) -> Chunk<'_>

Sets whether the chunk is text or binary (autodetected by default).

Be aware, Lua does not check the consistency of the code inside binary chunks. Running maliciously crafted bytecode can crash the interpreter.

Source

pub fn exec(self) -> Result<(), Error>

Execute this chunk of code.

This is equivalent to calling the chunk function with no arguments and no return values.

Source

pub fn eval<R>(self) -> Result<R, Error>
where R: FromLuaMulti,

Evaluate the chunk as either an expression or block.

If the chunk can be parsed as an expression, this loads and executes the chunk and returns the value that it evaluates to. Otherwise, the chunk is interpreted as a block as normal, and this is equivalent to calling exec.

Source

pub fn call<R>(self, args: impl IntoLuaMulti) -> Result<R, Error>
where R: FromLuaMulti,

Load the chunk function and call it with the given arguments.

This is equivalent to into_function and calling the resulting function.

Source

pub fn into_function(self) -> Result<Function, Error>

Load this chunk into a regular Function.

This simply compiles the chunk without actually executing it.

Auto Trait Implementations§

§

impl<'a> Freeze for Chunk<'a>

§

impl<'a> !RefUnwindSafe for Chunk<'a>

§

impl<'a> !Send for Chunk<'a>

§

impl<'a> !Sync for Chunk<'a>

§

impl<'a> Unpin for Chunk<'a>

§

impl<'a> !UnwindSafe for Chunk<'a>

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> IntoResult<T> for T

Source§

type Error = Infallible

The error type in the returned Result.
Source§

fn into_result(self) -> Result<T, <T as IntoResult<T>>::Error>

Converts the value into a Result.
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.
Source§

impl<T> MaybeSend for T