Skip to main content

Chunk

Struct Chunk 

Source
pub struct Chunk<'rustia, 'source, C = ()>
where C: HostSchema,
{ /* private fields */ }
Expand description

Luau source prepared for one execution or module installation.

Created by Rustia::load. Give it a source_name for useful diagnostics, then choose how to run it:

  • eval returns ordinary Rust data.
  • eval_serde deserializes into a Rust structure.
  • eval_live keeps a script value alive without copying it.
  • exec discards the result.
  • install returns exports and rolls back guest state on failure.

Loading alone does not parse or execute the source. Each operation consumes the chunk; use Rustia::compile_module to compile once and install repeatedly.

Implementations§

Source§

impl<'rustia, 'source, C> Chunk<'rustia, 'source, C>
where C: HostSchema,

Source

pub fn source_name(self, source_name: impl Into<String>) -> Self

Labels the source in errors and module resolution.

Use a filename or another meaningful label, such as "rules.luau". The default is "chunk". This name is also the referrer passed to the module resolver for require; setting it does not read a file.

Source

pub fn source_map(self, mapping: SourceMap) -> Self

Maps generated wrapper text out of human-facing diagnostics.

This only changes error presentation; it does not change script execution or the source names and lines visible to scripts. See crate::SourceMap.

Source

pub fn environment(self, environment: impl Into<Table>) -> Self

Selects the exact table used as this chunk’s lexical environment.

Cross-VM and reset-stale handles are rejected before parsing or compilation. For install, mutations to this table commit only when the complete installation succeeds. This replaces the default environment; globals are inherited only if the supplied table’s metatable provides that behavior.

Source

pub fn eval<T>(self) -> Result<T>
where T: FromRustia,

Runs the script and converts its first result to Rust data.

Conversion uses FromRustia over an owned snapshot. No returned value means Luau nil; extra results are ignored. Use eval_live for functions and cyclic tables, or to preserve shared table identity.

Invalid source, execution failures, and incompatible result types return Error. Script mutations remain in place even when execution or result conversion fails; use install for guest-state rollback.

let scores: Vec<i32> = vm.load("return {10, 20, 30}").eval()?;
assert_eq!(scores, vec![10, 20, 30]);
Source

pub fn eval_live(self) -> Result<Value>

Runs the script and returns a live handle to its first result.

The handle keeps the value alive in this interpreter and preserves shared and cyclic objects. It remains usable until Rustia::reset_decision, and cannot be used with another interpreter. No returned value becomes a handle to nil. As with eval, errors do not undo mutations.

Source

pub fn eval_serde<T>(self) -> Result<T>

Runs the script and deserializes its first result into Rust data.

Reads the live VM graph without an intermediate crate::OwnedValue. The destination still owns its strings and collections. Invalid source, execution failures, and data that does not match T return Error; mutations remain in place on failure.

#[derive(serde::Deserialize)]
struct Settings {
    volume: f64,
}
let settings: Settings = vm.load("return { volume = 0.5 }").eval_serde()?;
assert_eq!(settings.volume, 0.5);
Source

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

Executes the chunk and discards its returned value.

Parse, compilation, and execution errors are returned as Error. Changes made before an error are retained, as with eval.

Source

pub fn install(self) -> Result<Exports>

Runs the script as a module and keeps its returned exports alive.

Without environment, installation creates a fresh module environment that inherits VM globals and binds _G to itself. Compile, runtime, quota, and result-retention failures leave prior guest-visible state unchanged. External effects from Rust callbacks or the module resolver cannot be undone. A successful installation does not invalidate earlier exports; the application chooses when to replace its active handle.

let exports = vm.load("return { double = function(n) return n * 2 end }")
    .source_name("math.luau")
    .install()?;
let double = exports.function(&mut vm, "double")?;
let answer: i32 = double.call(&mut vm, (21,))?;
assert_eq!(answer, 42);

Trait Implementations§

Source§

impl<C> Debug for Chunk<'_, '_, C>
where C: HostSchema,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'rustia, 'source, C> Freeze for Chunk<'rustia, 'source, C>

§

impl<'rustia, 'source, C = ()> !RefUnwindSafe for Chunk<'rustia, 'source, C>

§

impl<'rustia, 'source, C = ()> !Send for Chunk<'rustia, 'source, C>

§

impl<'rustia, 'source, C = ()> !Sync for Chunk<'rustia, 'source, C>

§

impl<'rustia, 'source, C> Unpin for Chunk<'rustia, 'source, C>

§

impl<'rustia, 'source, C> UnsafeUnpin for Chunk<'rustia, 'source, C>

§

impl<'rustia, 'source, C = ()> !UnwindSafe for Chunk<'rustia, 'source, C>

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