2017-01-21 03:31:46 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::Rocket;
|
|
|
|
use crate::request::{self, FromRequest, Request};
|
|
|
|
use crate::outcome::Outcome;
|
|
|
|
use crate::http::Status;
|
2017-01-21 03:31:46 +00:00
|
|
|
|
2017-02-03 02:00:18 +00:00
|
|
|
/// Request guard to retrieve managed state.
|
|
|
|
///
|
|
|
|
/// This type can be used as a request guard to retrieve the state Rocket is
|
|
|
|
/// managing for some type `T`. This allows for the sharing of state across any
|
|
|
|
/// number of handlers. A value for the given type must previously have been
|
2018-10-06 13:25:17 +00:00
|
|
|
/// registered to be managed by Rocket via
|
2019-06-13 01:48:02 +00:00
|
|
|
/// [`Rocket::manage()`]. The type being managed must be
|
2018-10-06 13:25:17 +00:00
|
|
|
/// thread safe and sendable across thread boundaries. In other words, it must
|
|
|
|
/// implement [`Send`] + [`Sync`] + 'static`.
|
2017-02-03 02:00:18 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Imagine you have some configuration struct of the type `MyConfig` that you'd
|
|
|
|
/// like to initialize at start-up and later access it in several handlers. The
|
|
|
|
/// following example does just this:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-10-06 04:56:46 +00:00
|
|
|
/// # #![feature(proc_macro_hygiene, decl_macro)]
|
2018-06-28 15:55:15 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
2017-02-03 02:00:18 +00:00
|
|
|
/// use rocket::State;
|
|
|
|
///
|
|
|
|
/// // In a real application, this would likely be more complex.
|
2017-07-03 22:13:21 +00:00
|
|
|
/// struct MyConfig {
|
|
|
|
/// user_val: String
|
|
|
|
/// }
|
2017-02-03 02:00:18 +00:00
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// fn index(state: State<MyConfig>) -> String {
|
2017-07-03 22:13:21 +00:00
|
|
|
/// format!("The config value is: {}", state.user_val)
|
2017-02-03 02:00:18 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[get("/raw")]
|
|
|
|
/// fn raw_config_value<'r>(state: State<'r, MyConfig>) -> &'r str {
|
|
|
|
/// // use `inner()` to get a lifetime longer than `deref` gives us
|
2017-07-03 22:13:21 +00:00
|
|
|
/// state.inner().user_val.as_str()
|
2017-02-03 02:00:18 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-07-03 22:13:21 +00:00
|
|
|
/// let config = MyConfig {
|
|
|
|
/// user_val: "user input".to_string()
|
|
|
|
/// };
|
|
|
|
///
|
2017-02-03 02:00:18 +00:00
|
|
|
/// # if false { // We don't actually want to launch the server in an example.
|
|
|
|
/// rocket::ignite()
|
|
|
|
/// .mount("/", routes![index, raw_config_value])
|
|
|
|
/// .manage(config)
|
2017-03-16 05:10:09 +00:00
|
|
|
/// .launch();
|
2017-02-03 02:00:18 +00:00
|
|
|
/// # }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-07-03 22:13:21 +00:00
|
|
|
///
|
|
|
|
/// # Within Request Guards
|
|
|
|
///
|
|
|
|
/// Because `State` is itself a request guard, managed state can be retrieved
|
|
|
|
/// from another request guard's implementation. In the following code example,
|
2018-10-06 13:25:17 +00:00
|
|
|
/// `Item` retrieves the `MyConfig` managed state in its [`FromRequest`]
|
2017-07-03 22:13:21 +00:00
|
|
|
/// implementation using the [`Request::guard()`] method.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::State;
|
|
|
|
/// use rocket::request::{self, Request, FromRequest};
|
|
|
|
///
|
|
|
|
/// # struct MyConfig{ user_val: String };
|
|
|
|
/// struct Item(String);
|
|
|
|
///
|
2019-06-13 01:48:02 +00:00
|
|
|
/// impl FromRequest<'_, '_> for Item {
|
2017-07-03 22:13:21 +00:00
|
|
|
/// type Error = ();
|
|
|
|
///
|
2019-06-13 01:48:02 +00:00
|
|
|
/// fn from_request(request: &Request<'_>) -> request::Outcome<Item, ()> {
|
2017-07-03 22:13:21 +00:00
|
|
|
/// request.guard::<State<MyConfig>>()
|
|
|
|
/// .map(|my_config| Item(my_config.user_val.clone()))
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-11-03 08:51:00 +00:00
|
|
|
///
|
|
|
|
/// # Testing with `State`
|
|
|
|
///
|
|
|
|
/// When unit testing your application, you may find it necessary to manually
|
|
|
|
/// construct a type of `State` to pass to your functions. To do so, use the
|
|
|
|
/// [`State::from()`] static method:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::State;
|
|
|
|
///
|
|
|
|
/// struct MyManagedState(usize);
|
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(state: State<MyManagedState>) -> String {
|
|
|
|
/// state.0.to_string()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let rocket = rocket::ignite().manage(MyManagedState(127));
|
|
|
|
/// let state = State::from(&rocket).expect("managing `MyManagedState`");
|
|
|
|
/// assert_eq!(handler(state), "127");
|
|
|
|
/// ```
|
2017-07-03 22:39:24 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2017-01-21 03:31:46 +00:00
|
|
|
pub struct State<'r, T: Send + Sync + 'static>(&'r T);
|
|
|
|
|
|
|
|
impl<'r, T: Send + Sync + 'static> State<'r, T> {
|
2018-07-12 03:44:09 +00:00
|
|
|
/// Retrieve a borrow to the underlying value with a lifetime of `'r`.
|
2017-01-21 03:31:46 +00:00
|
|
|
///
|
2018-10-06 13:25:17 +00:00
|
|
|
/// Using this method is typically unnecessary as `State` implements
|
|
|
|
/// [`Deref`] with a [`Deref::Target`] of `T`. This means Rocket will
|
|
|
|
/// automatically coerce a `State<T>` to an `&T` as required. This method
|
|
|
|
/// should only be used when a longer lifetime is required.
|
2017-07-03 22:13:21 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::State;
|
|
|
|
///
|
|
|
|
/// struct MyConfig {
|
|
|
|
/// user_val: String
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Use `inner()` to get a lifetime of `'r`
|
|
|
|
/// fn handler1<'r>(config: State<'r, MyConfig>) -> &'r str {
|
|
|
|
/// &config.inner().user_val
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Use the `Deref` implementation which coerces implicitly
|
|
|
|
/// fn handler2(config: State<MyConfig>) -> String {
|
|
|
|
/// config.user_val.clone()
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-03-28 10:10:18 +00:00
|
|
|
#[inline(always)]
|
2017-01-21 03:31:46 +00:00
|
|
|
pub fn inner(&self) -> &'r T {
|
|
|
|
self.0
|
|
|
|
}
|
2018-11-03 08:51:00 +00:00
|
|
|
|
|
|
|
/// Returns the managed state value in `rocket` for the type `T` if it is
|
|
|
|
/// being managed by `rocket`. Otherwise, returns `None`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::State;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug, PartialEq)]
|
|
|
|
/// struct Managed(usize);
|
|
|
|
///
|
|
|
|
/// #[derive(Debug, PartialEq)]
|
|
|
|
/// struct Unmanaged(usize);
|
|
|
|
///
|
|
|
|
/// let rocket = rocket::ignite().manage(Managed(7));
|
|
|
|
///
|
|
|
|
/// let state: Option<State<Managed>> = State::from(&rocket);
|
|
|
|
/// assert_eq!(state.map(|s| s.inner()), Some(&Managed(7)));
|
|
|
|
///
|
|
|
|
/// let state: Option<State<Unmanaged>> = State::from(&rocket);
|
|
|
|
/// assert_eq!(state, None);
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn from(rocket: &'r Rocket) -> Option<Self> {
|
|
|
|
rocket.state.try_get::<T>().map(State)
|
|
|
|
}
|
2017-01-21 03:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
impl<'r, T: Send + Sync + 'static> FromRequest<'_, 'r> for State<'r, T> {
|
2017-01-21 03:31:46 +00:00
|
|
|
type Error = ();
|
|
|
|
|
2017-03-28 10:10:18 +00:00
|
|
|
#[inline(always)]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn from_request(req: &Request<'r>) -> request::Outcome<State<'r, T>, ()> {
|
2018-09-20 04:14:30 +00:00
|
|
|
match req.state.managed.try_get::<T>() {
|
2017-03-28 10:10:18 +00:00
|
|
|
Some(state) => Outcome::Success(State(state)),
|
|
|
|
None => {
|
|
|
|
error_!("Attempted to retrieve unmanaged state!");
|
|
|
|
Outcome::Failure((Status::InternalServerError, ()))
|
2017-01-21 03:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
impl<T: Send + Sync + 'static> Deref for State<'_, T> {
|
2017-01-21 03:31:46 +00:00
|
|
|
type Target = T;
|
|
|
|
|
2017-03-28 10:10:18 +00:00
|
|
|
#[inline(always)]
|
2017-01-21 03:31:46 +00:00
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|