Make inner 'LenientForm' field public. Add 'State::from()'.

This commit is contained in:
Sergio Benitez 2018-11-03 01:51:00 -07:00
parent b4f85daaf2
commit 983ee9b32d
3 changed files with 64 additions and 1 deletions

View File

@ -59,7 +59,7 @@ use http::uri::FromUriParam;
/// forms = 524288 /// forms = 524288
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct LenientForm<T>(crate T); pub struct LenientForm<T>(pub T);
impl<T> LenientForm<T> { impl<T> LenientForm<T> {
/// Consumes `self` and returns the parsed value. /// Consumes `self` and returns the parsed value.

View File

@ -1,5 +1,6 @@
use std::ops::Deref; use std::ops::Deref;
use Rocket;
use request::{self, FromRequest, Request}; use request::{self, FromRequest, Request};
use outcome::Outcome; use outcome::Outcome;
use http::Status; use http::Status;
@ -78,6 +79,29 @@ use http::Status;
/// } /// }
/// } /// }
/// ``` /// ```
///
/// # 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");
/// ```
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct State<'r, T: Send + Sync + 'static>(&'r T); pub struct State<'r, T: Send + Sync + 'static>(&'r T);
@ -112,6 +136,33 @@ impl<'r, T: Send + Sync + 'static> State<'r, T> {
pub fn inner(&self) -> &'r T { pub fn inner(&self) -> &'r T {
self.0 self.0
} }
/// 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)
}
} }
impl<'a, 'r, T: Send + Sync + 'static> FromRequest<'a, 'r> for State<'r, T> { impl<'a, 'r, T: Send + Sync + 'static> FromRequest<'a, 'r> for State<'r, T> {

View File

@ -25,6 +25,18 @@ fn test_count() {
assert_eq!(get_count(&client), 100); assert_eq!(get_count(&client), 100);
} }
#[test]
fn test_raw_state_count() {
use rocket::State;
use super::{count, index};
let rocket = super::rocket();
assert_eq!(count(State::from(&rocket).unwrap()), "0");
assert!(index(State::from(&rocket).unwrap()).0.contains("Visits: 1"));
assert_eq!(count(State::from(&rocket).unwrap()), "1");
}
// Cargo runs each test in parallel on different threads. We use all of these // Cargo runs each test in parallel on different threads. We use all of these
// tests below to show (and assert) that state is managed per-Rocket instance. // tests below to show (and assert) that state is managed per-Rocket instance.
#[test] fn test_count_parallel() { test_count() } #[test] fn test_count_parallel() { test_count() }