Struct zoon::State

source ·
pub struct State<T> {
    pub inner: UnsafeCell<Option<T>>,
}

Fields

inner: UnsafeCell<Option<T>>

Implementations

Creates a new empty state.

Gets the reference to the underlying value.

Returns None if the state is empty.

Gets the mutable reference to the underlying value.

Returns None if the state is empty.

Sets the contents of the state to value.

Errors

This method returns Ok(()) if the state was seted

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

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

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

Gets the contents of the state, initializing it with f if the state was empty.

Panics

If f panics, the panic is propagated to the caller, and the state remains uninitialized.

It is an error to reentrantly initialize the state from f. Doing so results in a panic.

Examples
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);

Gets the contents of the state, initializing it with f if the state was empty. If the state was empty and f failed, an error is returned.

Panics

If f panics, the panic is propagated to the caller, and the state remains uninitialized.

It is an error to reentrantly initialize the state from f. Doing so results in a panic.

Examples
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))

Consumes the state, returning the wrapped value.

Returns None if the state was empty.

Examples
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()));

Takes the value out of this State, moving it back to an uninitialized state.

Has no effect and returns None if the State hasn’t been initialized.

Safety is guaranteed by requiring a mutable reference.

Examples
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);

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Creates a new State<T> which already contains the given value.

This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.