Skip to main content

RunnerSnapshot

Struct RunnerSnapshot 

Source
pub struct RunnerSnapshot {
    pub current_node: Option<String>,
    pub visits: HashMap<String, u32>,
    pub once_seen: HashSet<String>,
}
Expand description

A point-in-time snapshot of the dialogue session’s mutable state.

Use Runner::snapshot to capture and Runner::restore to apply.

The snapshot records:

  • which node was active (current_node),
  • how many times each node has been visited (visits),
  • which <<once>> blocks have already fired (once_seen).

Variable storage is not included - it is the host’s responsibility to persist HashMapStorage (or their own VariableStorage impl) alongside the snapshot when building a full save file.

With the serde feature, this type also derives Serialize / Deserialize so you can write it to disk as JSON or another format.

§Save / load example

Both the snapshot and variable storage must be serialised together when you persist to disk. Neither is complete without the other.

use bubbles::{HashMapStorage, Runner, Value, VariableStorage, compile};

let src = "title: A\n---\n<<set $gold = 10>>\nHello!\n===\n";
let prog = compile(src).unwrap();

// First session
let mut runner = Runner::new(prog.clone(), HashMapStorage::new());
runner.start("A").unwrap();
let _ = runner.next_event(); // NodeStarted
let _ = runner.next_event(); // (set $gold side-effect, then Line)

let snap = runner.snapshot();
let snap_json = serde_json::to_string(&snap).unwrap();
let vars_json = serde_json::to_string(runner.storage()).unwrap();

let saved_vars: HashMapStorage = serde_json::from_str(&vars_json).unwrap();
let saved_snap: bubbles::RunnerSnapshot = serde_json::from_str(&snap_json).unwrap();

let mut runner2 = Runner::new(prog, saved_vars);
runner2.restore(saved_snap).unwrap();
// runner2 is back at the beginning of node "A" with $gold already set.

Fields§

§current_node: Option<String>

The node that was executing when the snapshot was taken.

When restoring, Runner::restore will restart execution from the beginning of this node. This is intentional: the in-progress statement list is not serialised because it would require the full AST to be round-tripped.

§visits: HashMap<String, u32>

How many times each node has been visited.

§once_seen: HashSet<String>

IDs of <<once>> blocks (and once-line-variants) that have already fired and must not fire again.

Trait Implementations§

Source§

impl Clone for RunnerSnapshot

Source§

fn clone(&self) -> RunnerSnapshot

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

impl Debug for RunnerSnapshot

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for RunnerSnapshot

Source§

impl PartialEq for RunnerSnapshot

Source§

fn eq(&self, other: &RunnerSnapshot) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RunnerSnapshot

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.