Trait tor_persist::StateMgr

source ·
pub trait StateMgr: Clone {
    // Required methods
    fn load<D>(&self, key: &str) -> Result<Option<D>, Error>
       where D: DeserializeOwned;
    fn store<S>(&self, key: &str, val: &S) -> Result<(), Error>
       where S: Serialize;
    fn can_store(&self) -> bool;
    fn try_lock(&self) -> Result<LockStatus, Error>;
    fn unlock(&self) -> Result<(), Error>;

    // Provided method
    fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
       where Self: Send + Sync + Sized + 'static,
             T: Serialize + DeserializeOwned + 'static { ... }
}
Expand description

An object that can manage persistent state.

State is implemented as a simple key-value store, where the values are objects that can be serialized and deserialized.

§Warnings

Current implementations may place additional limits on the types of objects that can be stored. This is not a great example of OO design: eventually we should probably clarify that more.

Required Methods§

source

fn load<D>(&self, key: &str) -> Result<Option<D>, Error>

Try to load the object with key key from the store.

Return None if no such object exists.

source

fn store<S>(&self, key: &str, val: &S) -> Result<(), Error>
where S: Serialize,

Try to save val with key key in the store.

Replaces any previous value associated with key.

source

fn can_store(&self) -> bool

Return true if this is a read-write state manager.

If it returns false, then attempts to store will fail with an error of kind BadApiUsage

source

fn try_lock(&self) -> Result<LockStatus, Error>

Try to become a read-write state manager if possible, without blocking.

This function will return an error only if something really unexpected went wrong. It may return Ok(_) even if we don’t acquire the lock: check the return value or call [StateMgr::can_store()] to see if the lock is held.

source

fn unlock(&self) -> Result<(), Error>

Release any locks held and become a read-only state manager again. If no locks were held, do nothing.

Provided Methods§

source

fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
where Self: Send + Sync + Sized + 'static, T: Serialize + DeserializeOwned + 'static,

Make a new StorageHandle to store values of particular type at a particular key.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl StateMgr for FsStateMgr

Available on non-WebAssembly only.
source§

impl StateMgr for TestingStateMgr

Available on crate feature testing only.