Add 'Rocket::state()' for managed state retrieval.

This commit is contained in:
Jeb Rosen 2017-12-27 16:45:30 -07:00 committed by Sergio Benitez
parent d79cb9d8f0
commit 80e7339ebe
1 changed files with 20 additions and 0 deletions

View File

@ -604,6 +604,26 @@ impl Rocket {
self
}
/// Returns `Some` of the managed state value for the type `T` if it is
/// being managed by this instance of Rocket. Otherwise, returns `None`.
///
/// # Example
///
/// ```rust
/// #[derive(PartialEq, Debug)]
/// struct MyState(&'static str);
///
/// let rocket = rocket::ignite().manage(MyState("hello!"));
/// assert_eq!(rocket.state::<MyState>(), Some(&MyState("hello!")));
///
/// let client = rocket::local::Client::new(rocket).expect("valid rocket");
/// assert_eq!(client.rocket().state::<MyState>(), Some(&MyState("hello!")));
/// ```
#[inline(always)]
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.state.try_get()
}
/// Attaches a fairing to this instance of Rocket.
///
/// # Example