Skip to main content

StateExt

Trait StateExt 

Source
pub trait StateExt {
    // Required methods
    fn get_str(&self, key: &str) -> Option<&str>;
    fn get_bool(&self, key: &str) -> Option<bool>;
    fn get_u64(&self, key: &str) -> Option<u64>;
    fn get_i64(&self, key: &str) -> Option<i64>;
    fn get_f64(&self, key: &str) -> Option<f64>;
    fn get_json<T>(&self, key: &str) -> Result<T, StateError>
       where T: DeserializeOwned;
    fn require<T>(&self, key: &str) -> Result<T, StateError>
       where T: DeserializeOwned;
    fn set<T>(&mut self, key: impl Into<String>, value: T)
       where T: Serialize;
    fn remove(&mut self, key: &str) -> Option<Value>;
    fn contains(&self, key: &str) -> bool;
    fn reduce(
        &mut self,
        key: &str,
        value: Value,
        reducer: &StateReducer,
    ) -> Result<(), String>;
    fn append_array(&mut self, key: &str, items: Value) -> Result<(), String>;
}
Expand description

State 扩展方法 — 通过 Trait 为 HashMap 添加强类型访问与 Reducer 能力。

Required Methods§

Source

fn get_str(&self, key: &str) -> Option<&str>

获取 String 值。

Source

fn get_bool(&self, key: &str) -> Option<bool>

获取 bool 值。

Source

fn get_u64(&self, key: &str) -> Option<u64>

获取 u64 值。

Source

fn get_i64(&self, key: &str) -> Option<i64>

获取 i64 值。

Source

fn get_f64(&self, key: &str) -> Option<f64>

获取 f64 值。

Source

fn get_json<T>(&self, key: &str) -> Result<T, StateError>

反序列化为强类型。

Source

fn require<T>(&self, key: &str) -> Result<T, StateError>

获取并反序列化为强类型。key 不存在时返回错误。

Source

fn set<T>(&mut self, key: impl Into<String>, value: T)
where T: Serialize,

设置值(自动序列化)。

Source

fn remove(&mut self, key: &str) -> Option<Value>

移除并返回值。

Source

fn contains(&self, key: &str) -> bool

检查 key 是否存在。

Source

fn reduce( &mut self, key: &str, value: Value, reducer: &StateReducer, ) -> Result<(), String>

使用 Reducer 合并值到指定 key。

Source

fn append_array(&mut self, key: &str, items: Value) -> Result<(), String>

追加模式 — 内置的数组追加 Reducer。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§