Skip to main content

HostFunction

Struct HostFunction 

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

Source

pub fn pure<F, A, R>(function: F) -> Self
where 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.

Source

pub fn with_capability<F, A, R>(capability: OpCapability, function: F) -> Self
where F: IntoHostFunction<A, R>,

Declares the capability used by a callback’s captured state.

For a typed host schema, prefer constructors such as input and effect, which provide the appropriate borrowed view.

Source

pub fn input<F, A, R>(function: F) -> Self
where F: IntoInputFunction<C, A, R>,

Creates a callback that borrows the invocation’s deterministic input.

Source

pub fn slot_read<F, A, R>(function: F) -> Self
where F: IntoSlotReadFunction<C, A, R>,

Creates a callback that borrows the invocation’s deterministic slot state.

Source

pub fn effect<F, A, R>(function: F) -> Self
where F: IntoEffectFunction<C, A, R>,

Creates a callback that stages effects in the invocation’s record journal.

Source

pub fn slot_write<F, A, R>(function: F) -> Self
where F: IntoSlotWriteFunction<C, A, R>,

Creates a callback that stages slot writes in the invocation’s record journal.

Source

pub fn external<F, A, R>(function: F) -> Self
where F: IntoExternalFunction<C, A, R>,

An external-state callback, rejected by deterministic profiles.

Source

pub fn nondeterministic<F, A, R>(function: F) -> Self
where F: IntoExternalFunction<C, A, R>,

A nondeterministic callback, rejected by deterministic profiles.

Source

pub fn fuel_cost(self, fuel_cost: u32) -> Self

Sets the fuel charged for each call. The default is one; zero is rejected when the function is created or registered.

Auto Trait Implementations§

§

impl<C> Freeze for HostFunction<C>

§

impl<C = ()> !RefUnwindSafe for HostFunction<C>

§

impl<C> Send for HostFunction<C>

§

impl<C> Sync for HostFunction<C>

§

impl<C> Unpin for HostFunction<C>

§

impl<C> UnsafeUnpin for HostFunction<C>

§

impl<C = ()> !UnwindSafe for HostFunction<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.
Source§

impl<C, Signature, F> IntoHostFunctionDefinition<C, Signature> for F
where C: HostSchema, F: Definition<C, Signature>,