Skip to main content

VariableStorage

Trait VariableStorage 

Source
pub trait VariableStorage {
    // Required methods
    fn get(&self, name: &str) -> Option<Value>;
    fn set(&mut self, name: &str, value: Value);

    // Provided methods
    fn get_ref(&self, name: &str) -> Option<Cow<'_, Value>> { ... }
    fn all_variables(&self) -> Vec<(String, Value)> { ... }
}
Expand description

Pluggable variable storage consumed by the runner.

Implement this trait to back variables with your game’s own data model (e.g. an ECS component, a database row, or a save-file entry).

§Example

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

let mut s = HashMapStorage::new();
s.set("$score", Value::Number(10.0));
assert_eq!(s.get("$score"), Some(Value::Number(10.0)));
assert_eq!(s.get("$missing"), None);

Required Methods§

Source

fn get(&self, name: &str) -> Option<Value>

Returns the current value of name, or None if the variable has not been set.

This is the ergonomic read path: it always returns an owned Value, cloning if the backing store holds one by reference. New impls are encouraged to override get_ref as well so hot expression-evaluation paths can avoid cloning Value::Text.

Source

fn set(&mut self, name: &str, value: Value)

Stores value under name, replacing any previous value.

Provided Methods§

Source

fn get_ref(&self, name: &str) -> Option<Cow<'_, Value>>

Returns a reference to the current value of name, or None if the variable has not been set.

The runner prefers this over get during expression evaluation so string variables can be observed without an allocation. The default implementation simply forwards to get and wraps the result in Cow::Owned, so existing implementations keep working unchanged. Stores that already own their values (such as HashMapStorage) should override this to return Cow::Borrowed.

Source

fn all_variables(&self) -> Vec<(String, Value)>

Returns every (variable_name, value) pair this storage currently holds.

Intended for debug overlays, save editors, and tests. The default implementation returns an empty vector; override it when you can enumerate variables (as HashMapStorage does).

Implementors§