zoon

Crates.iozoon
lib.rszoon
version0.1.6
sourcesrc
created_at2022-11-04 01:53:08.491749
updated_at2022-11-16 21:56:26.45312
descriptionFlexible,Predictable State Management
homepage
repositoryhttps://gitlab.com/andrew_ryan/zoon
max_upload_size
id704749
size12,333
zinso (zinso)

documentation

https://docs.rs/zoon

README

State management

Usage

Include state in your Cargo.toml [dependencies]:

[dependencies]
zoon = "0.1.6"

get

use zoon::State;
let state = State::new();
assert!(state.get().is_none());

assert_eq!(state.set(92), Ok(()));

assert!(state.get().is_some());

get_or_init

use zoon::State;
let state = State::new();
let value = state.get_or_init(|| 92);
assert_eq!(value, &92);
let value = state.get_or_init(|| unreachable!());
assert_eq!(value, &92);

get_or_try_init

use zoon::State;
let state = State::new();
assert_eq!(state.get_or_try_init(|| Err(())), Err(()));
assert!(state.get().is_none());
let value = state.get_or_try_init(|| -> Result<i32, ()> {
    Ok(92)
});
assert_eq!(value, Ok(&92));
assert_eq!(state.get(), Some(&92))

into_inner

use zoon::State;
let state: State<String> = State::new();
assert_eq!(state.into_inner(), None);

let state = State::new();
state.set("hello".to_string()).unwrap();
assert_eq!(state.into_inner(), Some("hello".to_string()));

take

use zoon::State;
let mut state: State<String> = State::new();
assert_eq!(state.take(), None);

let mut state = State::new();
state.set("hello".to_string()).unwrap();
assert_eq!(state.take(), Some("hello".to_string()));
assert_eq!(state.get(), None);
Commit count: 10

cargo fmt