pub trait Storage {
    type Error: Error;

    // Required methods
    fn get_string<'a>(&'a self, key: &'a str) -> BoxFuture<'_, Option<String>>;
    fn get_int<'a>(&'a self, key: &'a str) -> BoxFuture<'_, Option<i64>>;
    fn get_bool<'a>(&'a self, key: &'a str) -> BoxFuture<'_, Option<bool>>;
    fn set_string<'a>(
        &'a mut self,
        key: &'a str,
        value: &'a str
    ) -> BoxFuture<'_, Result<(), Self::Error>>;
    fn set_int<'a>(
        &'a mut self,
        key: &'a str,
        value: i64
    ) -> BoxFuture<'_, Result<(), Self::Error>>;
    fn set_bool<'a>(
        &'a mut self,
        key: &'a str,
        value: bool
    ) -> BoxFuture<'_, Result<(), Self::Error>>;
    fn remove<'a>(
        &'a mut self,
        key: &'a str
    ) -> BoxFuture<'_, Result<(), Self::Error>>;
    fn commit(&mut self) -> BoxFuture<'_, Result<(), Self::Error>>;
}
Expand description

The Storage trait is used to access typed key=value storage, for persisting protocol state and other data between runs of the update check process.

Implementations of this trait should cache values until commit() is called, and then perform an atomic committing of all outstanding value changes. On a given instance of Storage, a get() following a set(), but before a commit() should also return the set() value (not the previous value).

However, the expected usage of this trait within the library is to perform a series of get()’s at startup, and then only set()+commit() after that. The expectation being that this is being used to persist state that needs to be maintained for continuity over a reboot (or power outage).

A note on using the wrong type with a key: the result should be as if there is no value for the key. This is so that the Result::uwrap_or(<…default…>) pattern will work.

use omaha_client::storage::{MemStorage, Storage};
use omaha_client::unless::Unless;
let mut storage = MemStorage::new();
storage.set_int("key", 345);

// value should be None:
let value: Option<String> = storage.get_string("key").await;
assert_eq!(None, value);


// value should be "default":
let value: String = storage.get_string("key").await.unwrap_or("default".to_string());
assert_eq!("default", value);

Required Associated Types§

Required Methods§

source

fn get_string<'a>(&'a self, key: &'a str) -> BoxFuture<'_, Option<String>>

Get a string from the backing store. Returns None if there is no value for the given key, or if the value for the key has a different type.

source

fn get_int<'a>(&'a self, key: &'a str) -> BoxFuture<'_, Option<i64>>

Get an int from the backing store. Returns None if there is no value for the given key, or if the value for the key has a different type.

source

fn get_bool<'a>(&'a self, key: &'a str) -> BoxFuture<'_, Option<bool>>

Get a boolean from the backing store. Returns None if there is no value for the given key, or if the value for the key has a different type.

source

fn set_string<'a>( &'a mut self, key: &'a str, value: &'a str ) -> BoxFuture<'_, Result<(), Self::Error>>

Set a value to be stored in the backing store. The implementation should cache the value until the |commit()| fn is called, and then persist all cached values at that time.

source

fn set_int<'a>( &'a mut self, key: &'a str, value: i64 ) -> BoxFuture<'_, Result<(), Self::Error>>

Set a value to be stored in the backing store. The implementation should cache the value until the |commit()| fn is called, and then persist all cached values at that time.

source

fn set_bool<'a>( &'a mut self, key: &'a str, value: bool ) -> BoxFuture<'_, Result<(), Self::Error>>

Set a value to be stored in the backing store. The implementation should cache the value until the |commit()| fn is called, and then persist all cached values at that time.

source

fn remove<'a>( &'a mut self, key: &'a str ) -> BoxFuture<'_, Result<(), Self::Error>>

Remove the value for |key| from the backing store. The implementation should cache that the value has been removed until the |commit()| fn is called, and then persist all changes at that time.

If there is no value for the key, this should return without error.

source

fn commit(&mut self) -> BoxFuture<'_, Result<(), Self::Error>>

Persist all cached values to storage.

Implementors§

source§

impl Storage for MemStorage

§

type Error = StorageErrors