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§
Sourcefn load<D>(&self, key: &str) -> Result<Option<D>, Error>where
D: DeserializeOwned,
fn load<D>(&self, key: &str) -> Result<Option<D>, Error>where
D: DeserializeOwned,
Try to load the object with key key
from the store.
Return None if no such object exists.
Sourcefn store<S>(&self, key: &str, val: &S) -> Result<(), Error>where
S: Serialize,
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
.
Sourcefn can_store(&self) -> bool
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
Sourcefn try_lock(&self) -> Result<LockStatus, Error>
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.
Provided Methods§
Sourcefn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
Make a new StorageHandle
to store values of particular type
at a particular key.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl StateMgr for FsStateMgr
impl StateMgr for TestingStateMgr
testing
only.