Skip to main content

FileStorage

Struct FileStorage 

Source
pub struct FileStorage { /* private fields */ }
Expand description

A Storage backed by one file per key, under the OS’s per-app data directory (data_dir).

The folder holds <key>.ron, the value, and <key>.ron.bak, the value before it (ticket 3045). A <key>.ron.tmp is there only between a crash and the next write, which removes it. A sync of the folder (Steam Auto-Cloud) must carry the .bak files with the .ron files: a key whose .ron is gone reads its good .bak, so a save deleted on one machine would come back from its backup on another.

A write puts the new value in <key>.ron.tmp and flushes it to the disk, copies the old <key>.ron (when it is a good copy: not empty, not all zero bytes, UTF-8) to <key>.ron.bak and flushes that, renames the .tmp onto the .ron, and syncs the folder. A read of a .ron that is missing, empty, all zero bytes or not UTF-8 beside a good .bak reads the .bak, and logs a warning. What that promises:

  • a crash of the game, on every OS: the key reads the old value or the new one;
  • a power cut or a crash of the system, on Linux (the Steam Deck) and macOS: the key reads the old value or the new one, and a write that returned Ok reads the new one, since the file and the folder are flushed before it returns (F_FULLFSYNC on macOS). A file system that can’t sync a folder (it answers Unsupported) keeps the rename only as well as it journals it;
  • a power cut on Windows: std opens no folder for a sync, so the folder isn’t flushed. NTFS journals the rename, so the key reads the old value or the new one, never damage; a write that returned in the moments before the cut may read the old one.

A key written "" or only \0 characters keeps no backup, so it reads back as written and not as damage.

Implementations§

Source§

impl FileStorage

Source

pub fn new(name: &str) -> Result<Self, StorageError>

Locates (but does not create) the data directory of the game whose storage is called name (data_dir). Fails only if the OS has no usable home directory.

Trait Implementations§

Source§

impl Debug for FileStorage

Source§

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

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

impl Storage for FileStorage

Source§

fn read(&self, key: &str) -> Result<Option<String>, StorageError>

The value stored at key, or None if nothing has been written there (or it was deleted).
Source§

fn write(&mut self, key: &str, value: &str) -> Result<(), StorageError>

Stores value at key, replacing whatever was there.
Source§

fn delete(&mut self, key: &str) -> Result<(), StorageError>

Removes key. Not an error if it didn’t exist.
Source§

fn list(&self) -> Result<Vec<String>, StorageError>

Every key currently stored, in no particular order.

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
§

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.

§

impl<S> FromSample<S> for S

§

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.

§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

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.