Struct gotham::state::State
[−]
[src]
pub struct State { /* fields omitted */ }
Provides storage for request state, and stores one item of each type. The types used for
storage must implement the gotham::state::StateData
trait to allow its storage.
Gotham provides StateData
to ease this implementation via derive
.
Examples
extern crate gotham; #[macro_use] extern crate gotham_derive; use gotham::state::State; #[derive(StateData)] struct MyStruct { value: i32 } let mut state = State::new(); state.put(MyStruct { value: 1 }); assert_eq!(state.borrow::<MyStruct>().unwrap().value, 1);
Methods
impl State
[src]
fn new() -> State
[src]
Creates a new, empty State
fn put<T>(&mut self, t: T) where
T: StateData,
[src]
T: StateData,
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>().unwrap().value, 1); state.put(AnotherStruct { value: "a string" }); state.put(MyStruct { value: 100 }); assert_eq!(state.borrow::<AnotherStruct>().unwrap().value, "a string"); assert_eq!(state.borrow::<MyStruct>().unwrap().value, 100);
fn has<T>(&self) -> bool where
T: StateData,
[src]
T: StateData,
Determines if the current value exists in State
storage.
Examples
state.put(MyStruct { value: 1 }); assert!(state.has::<MyStruct>()); assert_eq!(state.borrow::<MyStruct>().unwrap().value, 1); assert!(!state.has::<AnotherStruct>());
fn borrow<T>(&self) -> Option<&T> where
T: StateData,
[src]
T: StateData,
Borrows a value from the State
storage.
Examples
state.put(MyStruct { value: 1 }); assert!(state.borrow::<MyStruct>().is_some()); assert_eq!(state.borrow::<MyStruct>().unwrap().value, 1); assert!(state.borrow::<AnotherStruct>().is_none());
fn borrow_mut<T>(&mut self) -> Option<&mut T> where
T: StateData,
[src]
T: StateData,
Mutably borrows a value from the State
storage.
Examples
state.put(MyStruct { value: 100 }); { let a = state.borrow_mut::<MyStruct>().unwrap(); a.value += 10; } assert_eq!(state.borrow::<MyStruct>().unwrap().value, 110); assert!(state.borrow_mut::<AnotherStruct>().is_none());
fn take<T>(&mut self) -> Option<T> where
T: StateData,
[src]
T: StateData,
Moves a value out of the State
storage, and returns ownership.
Examples
state.put(MyStruct { value: 110 }); assert_eq!(state.take::<MyStruct>().unwrap().value, 110); assert!(state.take::<MyStruct>().is_none()); assert!(state.borrow_mut::<MyStruct>().is_none()); assert!(state.borrow::<MyStruct>().is_none()); assert!(state.take::<AnotherStruct>().is_none());