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§
source§impl State
impl State
sourcepub fn set<T: Any + Send + Sync + 'static>(&mut self, ctx: T)
pub fn set<T: Any + Send + Sync + 'static>(&mut self, ctx: T)
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!"));
sourcepub fn try_get<T: Any + Send + Sync + 'static>(&self) -> Option<&T>
pub fn try_get<T: Any + Send + Sync + 'static>(&self) -> Option<&T>
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");
},
}
sourcepub fn get_err<T: Any + Send + Sync + 'static>(&self) -> Result<&T, DireError>
pub fn get_err<T: Any + Send + Sync + 'static>(&self) -> Result<&T, DireError>
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");
},
}