Skip to main content

ObjectType

Struct ObjectType 

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

Source

pub fn new(name: impl Into<String>) -> Self

Starts a method definition for T, using name in diagnostics.

Source

pub fn name(&self) -> &str

Returns the name used for this definition’s registration diagnostics.

Source

pub const fn fuel_policy(&self) -> RustiaHostFuelPolicy

Returns the versioned policy used to charge method-call fuel.

Source

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.

Source

pub fn method<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn method_with_capability<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, capability: OpCapability, method: F, ) -> Self
where 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.

Source

pub fn length<F, R>(self, fuel_cost: u32, length: F) -> Self
where F: IntoObjectMethod<T, (), R>,

Defines #object with a typed, read-only payload callback.

Source

pub fn length_with_capability<F, R>( self, fuel_cost: u32, capability: OpCapability, length: F, ) -> Self
where F: IntoObjectMethod<T, (), R>,

Defines #object with an explicit captured-state capability.

Source

pub fn iterator<Factory, Step, S, C, R>( self, fuel_cost: u32, factory: Factory, step: Step, ) -> Self

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.

Source

pub fn iterator_with_capability<Factory, Step, S, C, R>( self, fuel_cost: u32, capability: OpCapability, factory: Factory, step: Step, ) -> Self

Defines generalized iteration with an explicit captured-state capability.

Source

pub fn method_mut<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn method_mut_nondeterministic<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where F: IntoMutObjectMethod<T, A, R>,

Adds an immediately mutable method that also performs nondeterministic effects.

Source§

impl<T: 'static, H: HostSchema> ObjectType<T, H>

Source

pub fn input<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where F: IntoInputObjectMethod<T, H, A, R>,

Adds a read-only method over this invocation’s deterministic input.

Source

pub fn slot_read<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where F: IntoSlotReadObjectMethod<T, H, A, R>,

Adds a read-only method over this invocation’s saved slot snapshot.

Source

pub fn effect<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn slot_write<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn external<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn nondeterministic<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn external_mut<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Source

pub fn nondeterministic_mut<F, A, R>( self, name: impl Into<String>, fuel_cost: u32, method: F, ) -> Self
where 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.

Auto Trait Implementations§

§

impl<T, H> Freeze for ObjectType<T, H>

§

impl<T, H = ()> !RefUnwindSafe for ObjectType<T, H>

§

impl<T, H> Send for ObjectType<T, H>

§

impl<T, H> Sync for ObjectType<T, H>

§

impl<T, H> Unpin for ObjectType<T, H>

§

impl<T, H> UnsafeUnpin for ObjectType<T, H>

§

impl<T, H = ()> !UnwindSafe for ObjectType<T, H>

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.