Skip to main content

Storage

Struct Storage 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_json(&self, key: &str) -> Option<String>

The value under key as canonical JSON text, or None.

Source

pub fn remove(&mut self, key: &str) -> bool

Remove key; true if it existed.

Source

pub fn clear(&mut self)

Remove every key.

Source

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.

Trait Implementations§

Source§

impl Default for Storage

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Storage

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.