pub struct HostFunction<C: HostSchema = ()> { /* private fields */ }Expand description
A Rust callback and its declared capabilities and fuel cost.
Pass it to Rustia::create_function to create a live function, or to
Rustia::register_function to keep the registration across decision resets.
It can also be added to a crate::HostModule. Plain closures work for
simple callbacks; use this type to choose capabilities or change the default
cost of one fuel unit per call.
use rustia::{HostFunction, Rustia};
let mut lua = Rustia::builder().build()?;
lua.register_function("double", HostFunction::pure(|n: i64| n * 2).fuel_cost(2))?;
assert_eq!(lua.load("return double(21)").eval::<i64>()?, 42);Arguments and tuple results through arity eight can mix ordinary Rust data
with live crate::Value, crate::Table, and Function handles. Calls
are synchronous and receive no VM borrow, so they cannot call back into Luau.
A callback can return Result to report a failure.
Closures must be Send + Sync + 'static; use the capability-specific
constructors to borrow per-invocation host state. Captured allocations are
outside the VM memory limit. Captured state must honor the declared capability,
and captured destructors must not panic. In unwinding builds, a callback panic
is caught and blocks further guest execution until Rustia::reset_decision;
in aborting builds, it terminates or traps instead.
Implementations§
Source§impl<C: HostSchema> HostFunction<C>
impl<C: HostSchema> HostFunction<C>
Sourcepub fn pure<F, A, R>(function: F) -> Selfwhere
F: IntoHostFunction<A, R>,
pub fn pure<F, A, R>(function: F) -> Selfwhere
F: IntoHostFunction<A, R>,
Declares a callback that neither reads nor changes external mutable state.
Rustia trusts this declaration; it cannot inspect the closure’s captures.
Sourcepub fn with_capability<F, A, R>(capability: OpCapability, function: F) -> Selfwhere
F: IntoHostFunction<A, R>,
pub fn with_capability<F, A, R>(capability: OpCapability, function: F) -> Selfwhere
F: IntoHostFunction<A, R>,
Sourcepub fn input<F, A, R>(function: F) -> Selfwhere
F: IntoInputFunction<C, A, R>,
pub fn input<F, A, R>(function: F) -> Selfwhere
F: IntoInputFunction<C, A, R>,
Creates a callback that borrows the invocation’s deterministic input.
Sourcepub fn slot_read<F, A, R>(function: F) -> Selfwhere
F: IntoSlotReadFunction<C, A, R>,
pub fn slot_read<F, A, R>(function: F) -> Selfwhere
F: IntoSlotReadFunction<C, A, R>,
Creates a callback that borrows the invocation’s deterministic slot state.
Sourcepub fn effect<F, A, R>(function: F) -> Selfwhere
F: IntoEffectFunction<C, A, R>,
pub fn effect<F, A, R>(function: F) -> Selfwhere
F: IntoEffectFunction<C, A, R>,
Creates a callback that stages effects in the invocation’s record journal.
Sourcepub fn slot_write<F, A, R>(function: F) -> Selfwhere
F: IntoSlotWriteFunction<C, A, R>,
pub fn slot_write<F, A, R>(function: F) -> Selfwhere
F: IntoSlotWriteFunction<C, A, R>,
Creates a callback that stages slot writes in the invocation’s record journal.
Sourcepub fn external<F, A, R>(function: F) -> Selfwhere
F: IntoExternalFunction<C, A, R>,
pub fn external<F, A, R>(function: F) -> Selfwhere
F: IntoExternalFunction<C, A, R>,
An external-state callback, rejected by deterministic profiles.
Sourcepub fn nondeterministic<F, A, R>(function: F) -> Selfwhere
F: IntoExternalFunction<C, A, R>,
pub fn nondeterministic<F, A, R>(function: F) -> Selfwhere
F: IntoExternalFunction<C, A, R>,
A nondeterministic callback, rejected by deterministic profiles.