Struct gotham::state::State

source ·
pub struct State { /* private fields */ }
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§

source§

impl State

source

pub fn from_request(req: Request<Body>, client_addr: SocketAddr) -> Self

Instantiate a new State for a given Request. This is primarily useful if you’re calling Gotham from your own Hyper service.

source

pub fn put<T>(&mut self, t: T)
where 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>().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);
source

pub fn has<T>(&self) -> bool
where 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>().value, 1);

assert!(!state.has::<AnotherStruct>());
source

pub fn try_borrow<T>(&self) -> Option<&T>
where T: StateData,

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

pub fn borrow<T>(&self) -> &T
where T: StateData,

Borrows a value from the State storage.

§Panics

If a value of type T is not present in State.

§Examples
state.put(MyStruct { value: 1 });
assert_eq!(state.borrow::<MyStruct>().value, 1);
source

pub fn try_borrow_mut<T>(&mut self) -> Option<&mut T>
where T: StateData,

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

pub fn borrow_mut<T>(&mut self) -> &mut T
where T: StateData,

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

pub fn try_take<T>(&mut self) -> Option<T>
where T: StateData,

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

pub fn take<T>(&mut self) -> T
where T: StateData,

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 Freeze for State

§

impl !RefUnwindSafe for State

§

impl Send for State

§

impl !Sync for State

§

impl Unpin for State

§

impl !UnwindSafe for State

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more