pub struct State<'a> { /* private fields */ }Implementations§
Source§impl<'a> State<'a>
impl<'a> State<'a>
Sourcepub fn new(manifest_dir: &str, load: Load<'a>) -> Self
pub fn new(manifest_dir: &str, load: Load<'a>) -> Self
Creates a new State with the given manifest directory and load handler.
§Examples
use state_engine::State;
use state_engine::load::Load;
let state = State::new("./examples/manifest", Load::new());pub fn with_in_memory(self, client: &'a mut dyn InMemoryClient) -> Self
pub fn with_kvs_client(self, client: &'a mut dyn KVSClient) -> Self
Sourcepub fn get(&mut self, key: &str) -> Result<Option<Value>, StateError>
pub fn get(&mut self, key: &str) -> Result<Option<Value>, StateError>
Returns the value for key, checking state cache → _store → _load in order.
§Examples
use state_engine::State;
use state_engine::load::Load;
use state_engine::InMemoryClient;
use serde_json::{json, Value};
struct MockInMemory { data: std::collections::HashMap<String, Value> }
impl MockInMemory { fn new() -> Self { Self { data: Default::default() } } }
impl InMemoryClient for MockInMemory {
fn get(&self, key: &str) -> Option<Value> { self.data.get(key).cloned() }
fn set(&mut self, key: &str, value: Value) { self.data.insert(key.to_string(), value); }
fn delete(&mut self, key: &str) -> bool { self.data.remove(key).is_some() }
}
let mut client = MockInMemory::new();
let mut state = State::new("./examples/manifest", Load::new())
.with_in_memory(&mut client);
// set then get
state.set("connection.common", json!({"host": "localhost"}), None).unwrap();
assert!(state.get("connection.common").unwrap().is_some());Sourcepub fn set(
&mut self,
key: &str,
value: Value,
ttl: Option<u64>,
) -> Result<bool, StateError>
pub fn set( &mut self, key: &str, value: Value, ttl: Option<u64>, ) -> Result<bool, StateError>
Writes value to the _store backend for key.
§Examples
let mut client = MockInMemory::new();
let mut state = State::new("./examples/manifest", Load::new())
.with_in_memory(&mut client);
assert!(state.set("connection.common", json!({"host": "localhost"}), None).unwrap());Sourcepub fn delete(&mut self, key: &str) -> Result<bool, StateError>
pub fn delete(&mut self, key: &str) -> Result<bool, StateError>
Removes the value for key from the _store backend.
§Examples
let mut client = MockInMemory::new();
let mut state = State::new("./examples/manifest", Load::new())
.with_in_memory(&mut client);
state.set("connection.common", json!({"host": "localhost"}), None).unwrap();
assert!(state.delete("connection.common").unwrap());
// delete後はstoreにデータがなく、_loadも試みるが今回はEnvClientなしのため値なし
assert!(state.get("connection.common").is_err() || state.get("connection.common").unwrap().is_none());Sourcepub fn exists(&mut self, key: &str) -> Result<bool, StateError>
pub fn exists(&mut self, key: &str) -> Result<bool, StateError>
Returns true if a value exists for key in state cache or _store.
Does not trigger _load.
§Examples
let mut client = MockInMemory::new();
let mut state = State::new("./examples/manifest", Load::new())
.with_in_memory(&mut client);
assert!(!state.exists("connection.common").unwrap());
state.set("connection.common", json!({"host": "localhost"}), None).unwrap();
assert!(state.exists("connection.common").unwrap());Auto Trait Implementations§
impl<'a> Freeze for State<'a>
impl<'a> !RefUnwindSafe for State<'a>
impl<'a> Send for State<'a>
impl<'a> Sync for State<'a>
impl<'a> Unpin for State<'a>
impl<'a> UnsafeUnpin for State<'a>
impl<'a> !UnwindSafe for State<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more