pub struct ObjectType<T: 'static, H: HostSchema = ()> { /* private fields */ }Expand description
A Rust type and the methods scripts may call on it.
Register the definition once, then create as many objects as needed. Each object keeps the method definition it was created with. Re-registering a type changes future objects; existing objects and detached methods keep their original behavior. Definitions survive decision reset.
use rustia::{ObjectType, Rustia};
struct Player { score: i64 }
let mut lua = Rustia::builder().build()?;
lua.register_object_type(ObjectType::<Player>::new("Player")
.method("score", 1, |player: &Player| player.score))?;
let player = lua.create_object(Player { score: 42 })?;
lua.globals()?.set_live(&mut lua, "player", &player.as_value())?;
assert_eq!(lua.eval::<i64>("return player:score()")?, 42);Read-only methods promise replay-stable input, including anything reached through captures or interior mutability. Use capability-specific methods for staged actions or external changes. Opaque Rust payload allocations are outside the VM memory limit, like callback captures. Payload destructors must not panic; the application must avoid ownership cycles in Rust data.
Implementations§
Source§impl<T: 'static, H: HostSchema> ObjectType<T, H>
impl<T: 'static, H: HostSchema> ObjectType<T, H>
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Starts a method definition for T, using name in diagnostics.
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the name used for this definition’s registration diagnostics.
Sourcepub const fn fuel_policy(&self) -> RustiaHostFuelPolicy
pub const fn fuel_policy(&self) -> RustiaHostFuelPolicy
Returns the versioned policy used to charge method-call fuel.
Sourcepub fn methods(&self) -> impl Iterator<Item = RustiaHostFunctionInfo<'_>> + '_
pub fn methods(&self) -> impl Iterator<Item = RustiaHostFunctionInfo<'_>> + '_
Describes each method before registration, in declaration order.
The first parameter is the object receiver. Borrowed capability views are supplied by Rustia and are not counted as guest arguments.
Sourcepub fn method<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoObjectMethod<T, A, R>,
pub fn method<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoObjectMethod<T, A, R>,
Adds a read-only method with an explicit host-call fuel cost.
The receiver comes first, followed by up to eight ordinary or live
arguments. The callback follows the same return and panic rules as
crate::HostFunction. It receives no VM borrow and cannot re-enter Luau.
Sourcepub fn method_with_capability<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
capability: OpCapability,
method: F,
) -> Selfwhere
F: IntoObjectMethod<T, A, R>,
pub fn method_with_capability<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
capability: OpCapability,
method: F,
) -> Selfwhere
F: IntoObjectMethod<T, A, R>,
Adds a read-only method with a declared captured-state capability.
A read-only Rust receiver does not make captured or interior state deterministic. Declare its actual capability here, or use the typed context helpers when borrowing per-invocation application state.
Sourcepub fn length<F, R>(self, fuel_cost: u32, length: F) -> Selfwhere
F: IntoObjectMethod<T, (), R>,
pub fn length<F, R>(self, fuel_cost: u32, length: F) -> Selfwhere
F: IntoObjectMethod<T, (), R>,
Defines #object with a typed, read-only payload callback.
Sourcepub fn length_with_capability<F, R>(
self,
fuel_cost: u32,
capability: OpCapability,
length: F,
) -> Selfwhere
F: IntoObjectMethod<T, (), R>,
pub fn length_with_capability<F, R>(
self,
fuel_cost: u32,
capability: OpCapability,
length: F,
) -> Selfwhere
F: IntoObjectMethod<T, (), R>,
Defines #object with an explicit captured-state capability.
Sourcepub fn iterator<Factory, Step, S, C, R>(
self,
fuel_cost: u32,
factory: Factory,
step: Step,
) -> Selfwhere
(Factory, Step): IntoObjectIterator<T, S, C, R>,
pub fn iterator<Factory, Step, S, C, R>(
self,
fuel_cost: u32,
factory: Factory,
step: Step,
) -> Selfwhere
(Factory, Step): IntoObjectIterator<T, S, C, R>,
Defines generalized iteration with independent owned state per loop.
The factory borrows the object once. The step mutates only that loop’s
state and receives the previous typed control. None completes iteration; the first returned value is Luau’s
next control value and first loop variable.
Sourcepub fn iterator_with_capability<Factory, Step, S, C, R>(
self,
fuel_cost: u32,
capability: OpCapability,
factory: Factory,
step: Step,
) -> Selfwhere
(Factory, Step): IntoObjectIterator<T, S, C, R>,
pub fn iterator_with_capability<Factory, Step, S, C, R>(
self,
fuel_cost: u32,
capability: OpCapability,
factory: Factory,
step: Step,
) -> Selfwhere
(Factory, Step): IntoObjectIterator<T, S, C, R>,
Defines generalized iteration with an explicit captured-state capability.
Sourcepub fn method_mut<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoMutObjectMethod<T, A, R>,
pub fn method_mut<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoMutObjectMethod<T, A, R>,
Adds a method that changes its Rust payload immediately.
This declares external mutable state and is rejected atomically by deterministic construction. Payload changes survive script errors and failed installation; reset drops the payload. Effects on external application state are not undone by reset.
Sourcepub fn method_mut_nondeterministic<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoMutObjectMethod<T, A, R>,
pub fn method_mut_nondeterministic<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoMutObjectMethod<T, A, R>,
Adds an immediately mutable method that also performs nondeterministic effects.
Source§impl<T: 'static, H: HostSchema> ObjectType<T, H>
impl<T: 'static, H: HostSchema> ObjectType<T, H>
Sourcepub fn input<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoInputObjectMethod<T, H, A, R>,
pub fn input<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoInputObjectMethod<T, H, A, R>,
Adds a read-only method over this invocation’s deterministic input.
Sourcepub fn slot_read<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoSlotReadObjectMethod<T, H, A, R>,
pub fn slot_read<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoSlotReadObjectMethod<T, H, A, R>,
Adds a read-only method over this invocation’s saved slot snapshot.
Sourcepub fn effect<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoEffectObjectMethod<T, H, A, R>,
pub fn effect<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoEffectObjectMethod<T, H, A, R>,
Adds a method that stages effects with &T and crate::Effects.
Effects share the invocation’s ordering and output limits. A failing method discards its own staged suffix; a failure escaping the outer invocation discards all its records.
Sourcepub fn slot_write<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoSlotWriteObjectMethod<T, H, A, R>,
pub fn slot_write<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoSlotWriteObjectMethod<T, H, A, R>,
Adds a method that stages saved-state changes with crate::SlotWrites.
Slot writes use the same journal, ordering, and limits as effects.
Sourcepub fn external<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalObjectMethod<T, H, A, R>,
pub fn external<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalObjectMethod<T, H, A, R>,
Adds a read-only payload method with immediate external-state access.
The callback receives &T, then crate::HostMut. This declares
external mutable state and is rejected by deterministic construction.
Application changes are outside invocation and installation rollback.
Sourcepub fn nondeterministic<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalObjectMethod<T, H, A, R>,
pub fn nondeterministic<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalObjectMethod<T, H, A, R>,
Adds a read-only payload method that performs nondeterministic external work.
The callback receives &T, then crate::HostMut. Deterministic
construction rejects the definition. External effects are immediate.
Sourcepub fn external_mut<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalMutObjectMethod<T, H, A, R>,
pub fn external_mut<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalMutObjectMethod<T, H, A, R>,
Adds a method with &mut T and immediate external-state access.
This declares external mutable state. Payload and application changes survive later script errors and failed installations. Reset drops the payload; it does not undo changes to the external application.
Sourcepub fn nondeterministic_mut<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalMutObjectMethod<T, H, A, R>,
pub fn nondeterministic_mut<F, A, R>(
self,
name: impl Into<String>,
fuel_cost: u32,
method: F,
) -> Selfwhere
F: IntoExternalMutObjectMethod<T, H, A, R>,
Adds a mutable payload method that performs nondeterministic external work.
Receives &mut T, then crate::HostMut. Both changes are immediate,
and deterministic construction rejects the complete definition.