Struct gotham::state::State [−][src]
pub struct State { /* fields omitted */ }
Expand description
Provides storage for request state, and stores one item of each type. The types used for
storage must implement the StateData
trait to allow its storage, which is usually done
by adding #[derive(StateData)]
on the type in question.
Examples
use gotham::state::{State, StateData};
#[derive(StateData)]
struct MyStruct {
value: i32,
}
state.put(MyStruct { value: 1 });
assert_eq!(state.borrow::<MyStruct>().value, 1);
Implementations
Instantiate a new State
for a given Request
. This is primarily useful if you’re calling
Gotham from your own Hyper service.
Puts a value into the State
storage. One value of each type is retained. Successive calls
to put
will overwrite the existing value of the same type.
Examples
state.put(MyStruct { value: 1 });
assert_eq!(state.borrow::<MyStruct>().value, 1);
state.put(AnotherStruct { value: "a string" });
state.put(MyStruct { value: 100 });
assert_eq!(state.borrow::<AnotherStruct>().value, "a string");
assert_eq!(state.borrow::<MyStruct>().value, 100);
Determines if the current value exists in State
storage.
Examples
state.put(MyStruct { value: 1 });
assert!(state.has::<MyStruct>());
assert_eq!(state.borrow::<MyStruct>().value, 1);
assert!(!state.has::<AnotherStruct>());
Tries to borrow a value from the State
storage.
Examples
state.put(MyStruct { value: 1 });
assert!(state.try_borrow::<MyStruct>().is_some());
assert_eq!(state.try_borrow::<MyStruct>().unwrap().value, 1);
assert!(state.try_borrow::<AnotherStruct>().is_none());
Tries to mutably borrow a value from the State
storage.
Examples
state.put(MyStruct { value: 100 });
if let Some(a) = state.try_borrow_mut::<MyStruct>() {
a.value += 10;
}
assert_eq!(state.borrow::<MyStruct>().value, 110);
assert!(state.try_borrow_mut::<AnotherStruct>().is_none());
Mutably borrows a value from the State
storage.
Panics
If a value of type T
is not present in State
.
Examples
state.put(MyStruct { value: 100 });
{
let a = state.borrow_mut::<MyStruct>();
a.value += 10;
}
assert_eq!(state.borrow::<MyStruct>().value, 110);
assert!(state.try_borrow_mut::<AnotherStruct>().is_none());
Tries to move a value out of the State
storage and return ownership.
Examples
state.put(MyStruct { value: 110 });
assert_eq!(state.try_take::<MyStruct>().unwrap().value, 110);
assert!(state.try_take::<MyStruct>().is_none());
assert!(state.try_borrow_mut::<MyStruct>().is_none());
assert!(state.try_borrow::<MyStruct>().is_none());
assert!(state.try_take::<AnotherStruct>().is_none());
Moves a value out of the State
storage and returns ownership.
Panics
If a value of type T
is not present in State
.
Examples
state.put(MyStruct { value: 110 });
assert_eq!(state.take::<MyStruct>().value, 110);
assert!(state.try_take::<MyStruct>().is_none());
assert!(state.try_borrow_mut::<MyStruct>().is_none());
assert!(state.try_borrow::<MyStruct>().is_none());
Auto Trait Implementations
impl !RefUnwindSafe for State
impl !UnwindSafe for State
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more