silk.machine.child.

Child machines: a machine that a parent starts, sends events to and stops.

When to use#

Put a Child in the parent's context for each machine the parent owns. Start it in the parent's enter, stop it in exit to tie it to a state as xstate's invoke does, or start and stop it in handle to tie it to the parent as spawn does.

Details#

A child is a Snapshot inside the parent's context, so the parent's snapshot holds its children and they are saved and restored with it. A child takes its steps inside the parent's step, in the order the parent calls it, so the whole tree stays deterministic.

A child queues effects of its own type. Child.drain moves them into the parent's queue, wrapped in a variant of the parent's effect type, and the parent's performer hands them to the child's performer. Replies.forward wraps the events that performer sends back, so they reach the parent, which passes them to the child with Child.send.

A child's delayed transitions use the parent's clock. Start the child with enq.clock, return Child.nextDeadline from the parent's Machine.deadline, and call Child.advance and Child.drain in the parent's Machine.tick. The parent's snapshot then reports the child's deadline as its own, and an actor wakes up for it.

Import as Child with import silk.machine.child { Child }.

Public declarations: 2.

Child#

silk
pub struct Child<S, E, C, X>

A child machine of states S, events E, context C and effects X. It is absent until it is started and after it is stopped.

Field snapshot#

silk
pub snapshot: Option<Snapshot<S, E, C, X>>

Implementation Schema for Child<S, E, C, X>#

silk
impl<S: Schema, E: Schema, C: Schema, X: Schema> Schema for Child<S, E, C, X>

Implementation Child<S, E, C, X>#

silk
impl<S: Machine<E, C, X>, E, C, X> Child<S, E, C, X>

Associated function absent#

silk
pub fn absent() -> Child<S, E, C, X>

Returns a child that is not started.

Method start#

silk
pub effect fn start(self: &mut Self, context: C, now: u64) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Starts the child with context at the wall clock time now, in nanoseconds. A child that was running is replaced.

Method stop#

silk
pub fn stop(self: &mut Self) -> ()

Stops the child and drops its state.

Method isActive#

silk
pub fn isActive(self: &Self) -> bool

Returns whether the child is started and can still take events.

Method isDone#

silk
pub fn isDone(self: &Self) -> bool

Returns whether the child reached a final state.

Method send#

silk
pub effect fn send(self: &mut Self, event: E) -> bool
    ! OutOfMemoryError
    ? &mut Allocator

Sends event to the child and returns whether one of its states handled it. A child that is absent or finished handles nothing. A child stuck in eventless transitions is marked Status.Failed.

Method advance#

silk
pub effect fn advance(self: &mut Self, now: u64) -> bool
    ! OutOfMemoryError
    ? &mut Allocator

Moves the child's clock to now and takes its delayed transitions that are due. Returns whether one was taken.

Method nextDeadline#

silk
pub fn nextDeadline(self: &Self) -> Option<u64>

Returns the deadline of the child's timer that fires first, in nanoseconds of wall clock time. Return it from the parent's Machine.deadline, and call Child.advance in the parent's Machine.tick, so the parent's actor wakes up for the child's delayed transitions.

Method drain#

silk
pub effect fn drain<PE, PX>(
    self: &mut Self,
    enq: &mut Enqueue<PE, PX>,
    wrap: fn<'static>(X) -> PX,
  ) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Moves the effects the child queued into the parent's queue, each wrapped by wrap.

Replies#

silk
pub struct Replies

Implementation Replies#

silk
impl Replies

Associated function make#

silk
pub fn make<E>() -> Reply<E>

Returns an empty reply for a child's performer to fill.

Associated function forward#

silk
pub effect fn forward<E, PE>(from: Reply<E>, to: &mut Reply<PE>, wrap: fn<'static>(E) -> PE) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Moves the events a child's performer sent into the parent's reply, each wrapped by wrap.

Category: State machines.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page