Skip to main content

Story

Struct Story 

Source
pub struct Story<'p, R: StoryRng = FastRng> { /* private fields */ }
Expand description

Per-instance mutable state for executing stories.

Created from a Program via Story::new. Holds all mutable state (stacks, globals, output buffer) while the immutable program data lives in Program.

Generic over R: StoryRng — defaults to FastRng. Use DotNetRng for .NET-compatible deterministic output.

Implementations§

Source§

impl<'p, R: StoryRng> Story<'p, R>

Source

pub fn new(program: &'p Program, line_tables: Vec<Vec<LineEntry>>) -> Self

Create a new story instance from a linked program and its line tables.

Source

pub fn set_plural_resolver(&mut self, resolver: Box<dyn PluralResolver>)

Set the plural resolver for Select resolution in localized lines.

Source

pub fn set_line_tables(&mut self, tables: Vec<Vec<LineEntry>>)

Replace the active line tables (e.g. for locale swapping).

Source

pub fn line_tables(&self) -> &[Vec<LineEntry>]

Read-only access to the current line tables.

Source

pub fn transcript(&self) -> &[OutputPart]

The full append-only transcript of all output parts produced so far.

Source

pub fn transcript_len(&self) -> usize

Number of parts in the transcript.

Source

pub fn reset_cursor(&mut self)

Reset the transcript read cursor to the beginning (for re-rendering).

Source

pub fn resolve_transcript_slice( &self, range: Range<usize>, ) -> Vec<(String, Vec<String>)>

Resolve a slice of the transcript against the current line tables. Returns (text, tags) tuples — one per line in the resolved output.

Source

pub fn pending_choices(&self) -> Vec<Choice>

Re-resolve all pending choices against the current line tables. Returns the same choices that would appear in Line::Choices, but freshly resolved (useful after locale switch).

Source

pub fn resolve_fragment(&self, idx: u32) -> String

Resolve a fragment against the current line tables.

Source

pub fn choice_fragment_idx(&self, choice_index: usize) -> Option<u32>

Get the fragment index for a pending choice’s display text, if any.

Source

pub fn fragments(&self) -> &[Fragment]

Read-only access to the fragment store (for transcript serialization).

Source

pub fn program(&self) -> &Program

Read-only access to the program.

Source

pub fn into_snapshot(self) -> (StorySnapshot<R>, Vec<Vec<LineEntry>>)

Detach story state from the program, consuming the story.

Source

pub fn from_snapshot( program: &'p Program, snapshot: StorySnapshot<R>, line_tables: Vec<Vec<LineEntry>>, ) -> Self

Reattach a snapshot to a program with line tables.

Source

pub fn continue_single(&mut self) -> Result<Line, RuntimeError>

Execute until one line of content (up to newline), or until a yield point (choices/end) if no newline occurs first.

The returned Line variant tells you what to do next:

Source

pub fn continue_single_observed( &mut self, observer: &mut dyn WriteObserver, ) -> Result<Line, RuntimeError>

Like continue_single but with a WriteObserver that receives notifications for every state mutation.

Source

pub fn continue_single_with( &mut self, handler: &dyn ExternalFnHandler, ) -> Result<Line, RuntimeError>

Like continue_single but with a custom external function handler.

Source

pub fn continue_maximally(&mut self) -> Result<Vec<Line>, RuntimeError>

Execute until the next yield point, collecting all lines.

Returns a Vec<Line> where the last element is always Line::Choices or Line::End, and all preceding elements are Line::Text.

Source

pub fn continue_maximally_with( &mut self, handler: &dyn ExternalFnHandler, ) -> Result<Vec<Line>, RuntimeError>

Like continue_maximally but with a custom external function handler.

Source

pub fn continue_maximally_observed( &mut self, observer: &mut dyn WriteObserver, ) -> Result<Vec<Line>, RuntimeError>

Execute until the next yield point with a WriteObserver that receives notifications for every state mutation.

Source

pub fn choose(&mut self, index: usize) -> Result<(), RuntimeError>

Select a choice by index, then resume with continue_single or continue_maximally.

Source

pub fn stats(&self) -> &Stats

Read-only access to the default flow’s VM statistics.

Source

pub fn has_pending_external(&self) -> bool

Returns true if the default flow has a pending external call (an External frame on top of the call stack).

Source

pub fn resolve_external(&mut self, value: Value)

Resolve a pending external call on the default flow by providing the return value. For fire-and-forget calls, pass Value::Null.

After resolving, call continue_maximally to continue execution.

Source

pub fn invoke_fallback(&mut self) -> Result<(), RuntimeError>

Resolve a pending external call on the default flow by invoking the ink-defined fallback body. The fallback is a function call whose output becomes the return value.

After invoking, call continue_maximally to continue execution.

Source

pub fn spawn_flow( &mut self, name: &str, entry_point: DefinitionId, ) -> Result<(), RuntimeError>

Spawn a new flow instance starting at the given entry point.

entry_point is the DefinitionId of the target container (e.g., a knot). Each flow instance gets its own globals, visit counts, and execution state.

Source

pub fn continue_flow_maximally( &mut self, name: &str, ) -> Result<Vec<Line>, RuntimeError>

Run a named flow instance until the next yield point.

Source

pub fn continue_flow_maximally_with( &mut self, name: &str, handler: &dyn ExternalFnHandler, ) -> Result<Vec<Line>, RuntimeError>

Run a named flow instance with an external function handler.

Source

pub fn choose_flow( &mut self, name: &str, index: usize, ) -> Result<(), RuntimeError>

Select a choice in a named flow.

Source

pub fn destroy_flow(&mut self, name: &str) -> Result<(), RuntimeError>

Destroy a named flow instance.

Source

pub fn flow_names(&self) -> Vec<&str>

List active flow names.

Source

pub fn debug_state(&self) -> String

Dump the current execution state for debugging.

Returns a human-readable summary of the call stack, current position, value stack, output buffer, globals, and pending choices.

Source

pub fn did_safe_exit(&self) -> bool

Returns whether the last execution cycle ended with a safe exit (explicit -> DONE opcode). If false after a Done line, the story ran out of content.

Source

pub fn did_unsafe_yield(&self) -> bool

Returns whether the last execution cycle passed through an empty choice set (a Yield opcode with no pending choices).

Source

pub fn step_once( &mut self, ) -> Result<Option<(String, u32, usize)>, RuntimeError>

Execute a single VM step and return a debug trace of what happened.

Returns (opcode_description, container_idx, offset_before) or None if the step didn’t decode an opcode (frame exhaustion, thread completion, etc).

Trait Implementations§

Source§

impl<R: StoryRng> Clone for Story<'_, R>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'p, R = FastRng> !RefUnwindSafe for Story<'p, R>

§

impl<'p, R = FastRng> !Send for Story<'p, R>

§

impl<'p, R = FastRng> !Sync for Story<'p, R>

§

impl<'p, R = FastRng> !UnwindSafe for Story<'p, R>

§

impl<'p, R> Freeze for Story<'p, R>

§

impl<'p, R> Unpin for Story<'p, R>
where R: Unpin,

§

impl<'p, R> UnsafeUnpin for Story<'p, R>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.