Struct direkuta::State

source ·
pub struct State { /* private fields */ }
Expand description

A wrapper around IndexMap<TypeId, Any>, used to store server state.

Stored state cannot be dynamically created and must be static.

Implementations§

Constructs a new State.

Examples
let state = State::new();

Sets the value of whatever type is passed.

Please note that you cannot have two states of the same types, one will overwrite the other.

Examples
let mut state = State::new();

state.set(String::from("Hello World!"));

Attempt to get a value based on type.

Use this if you are not sure if the type exists.

Examples
let mut state = State::new();

state.set(String::from("Hello World!"));

match state.try_get::<String>() {
    Some(s) => {
        println!("{}", s);
    },
    None => {
        println!("String not found in state");
    },
}

Attempt to get a value based on type.

Use this if you are not sure if the type exists and want an Result instead of Option.

Examples
let mut state = State::new();

state.set(String::from("Hello World!"));

match state.get_err::<String>() {
    Ok(s) => {
        println!("{}", s);
    },
    Err(e) => {
        println!("String not found in state");
    },
}

Get a value based on type.

This is a wrapper around try_get.

Examples
let mut state = State::new();

state.set(String::from("Hello World!"));

println!("{}", state.get::<String>());
Panics

If the key does not exist the function will panic.

If you do not know if the type exists use try_get.

Trait Implementations§

Returns the “default value” for a type. 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

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 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.