pub struct Storage { /* private fields */ }Expand description
A cart’s key-value store, plus its optional disk backing.
Default is a purely in-memory store (tests, the web player, headless
verify). Dropping a disk-backed store saves it, so every “cart stops”
path — stop, reload, reboot, console exit — persists without ceremony.
Two carts whose names sanitize to the same file stem share a save file, and two live instances running the same cart each hold a full in-memory copy that saves whole-store, last-complete-writer-wins — the name is the save identity, by design.
Implementations§
Source§impl Storage
impl Storage
Sourcepub fn for_cart(name: &str) -> Storage
pub fn for_cart(name: &str) -> Storage
The persistent store for a cart, loaded from (and saved to) a JSON file under the user’s cache directory, keyed by the cart’s name. Falls back to an in-memory store when no cache directory is discoverable. A missing, malformed, or wrong-version file starts empty rather than failing the cart.
Sourcepub fn for_cart_in(root: &Path, name: &str) -> Storage
pub fn for_cart_in(root: &Path, name: &str) -> Storage
Like Storage::for_cart, but rooted at an explicit directory
instead of the user’s cache directory. Frontends and tests use this
to keep saves out of (or hermetically inside) a chosen location.
Sourcepub fn at_path(path: PathBuf) -> Storage
pub fn at_path(path: PathBuf) -> Storage
A store backed by an explicit file path (used by for_cart and by
tests). The file need not exist yet. A missing, malformed,
wrong-version, or over-cap file starts empty.
Sourcepub fn set_json(&mut self, key: &str, json: &str) -> bool
pub fn set_json(&mut self, key: &str, json: &str) -> bool
Store json (JSON text) under key. Returns false — storing
nothing — when the text is not valid JSON or the store would exceed
MAX_BYTES serialized; the cap rejects the write, it never
clobbers old data.
Sourcepub fn get_json(&self, key: &str) -> Option<String>
pub fn get_json(&self, key: &str) -> Option<String>
The value under key as canonical JSON text, or None.
Sourcepub fn save_if_dirty(&mut self) -> Result<()>
pub fn save_if_dirty(&mut self) -> Result<()>
Write the store to its backing file if anything changed since the last save. In-memory stores are a no-op. The write goes through a per-process temp file + rename, so neither a crash mid-save nor another instance saving the same cart at the same moment can tear the file — concurrent saves land whole, last writer wins.