rocket/request/
mod.rs

1//! Types and traits for request parsing and handling.
2
3mod request;
4mod from_param;
5mod from_request;
6mod atomic_method;
7
8#[cfg(test)]
9mod tests;
10
11pub use self::request::Request;
12pub use self::from_request::{FromRequest, Outcome};
13pub use self::from_param::{FromParam, FromSegments};
14
15#[doc(hidden)]
16pub use rocket_codegen::FromParam;
17
18#[doc(inline)]
19pub use crate::response::flash::FlashMessage;
20
21pub(crate) use self::request::ConnectionMeta;
22pub(crate) use self::atomic_method::AtomicMethod;
23
24crate::export! {
25    /// Store and immediately retrieve a vector-like value `$v` (`String` or
26    /// `Vec<T>`) in `$request`'s local cache using a locally generated
27    /// anonymous type to avoid type conflicts.
28    ///
29    /// Unlike `local_cache_once`, this macro's generated code _always_ returns
30    /// a unique reference to request-local cache.
31    ///
32    /// # Note
33    ///
34    /// The value `$v` must be of type `String` or `Vec<T>`, that is, a type
35    /// that implements the sealed trait [`Shareable`](crate::form::Shareable)bb).
36    ///
37    /// # Example
38    ///
39    /// ```rust
40    /// use rocket::request::{local_cache, local_cache_once};
41    /// # let c = rocket::local::blocking::Client::debug_with(vec![]).unwrap();
42    /// # let request = c.get("/");
43    ///
44    /// // The first store into local cache for a given type wins.
45    /// for i in 0..4 {
46    ///     assert_eq!(request.local_cache(|| i.to_string()), "0");
47    /// }
48    ///
49    /// // This shows that we cannot cache different values of the same type; we
50    /// // _must_ use a proxy type. To avoid the need to write these manually, use
51    /// // `local_cache!`, which generates one of the fly.
52    /// for i in 0..4 {
53    ///     assert_eq!(local_cache!(request, i.to_string()), i.to_string());
54    /// }
55    ///
56    /// // Note that while `local_cache_once!` generates a new type for the
57    /// // _macro_ invocation, that type is the same per run-time invocation, so
58    /// // all "calls" to `local_cache_once!` on the same line return the same
59    /// // reference for a given request.
60    /// for i in 1..4 {
61    ///     // Note that this is `1`, so _not_ the `String` from line 4.
62    ///     assert_eq!(local_cache_once!(request, i.to_string()), "1");
63    /// }
64    /// ```
65    macro_rules! local_cache {
66        ($request:expr, $v:expr $(,)?) => ({
67            struct Local<T: $crate::form::Shareable>($crate::form::SharedStack<T>);
68            let stack = $request.local_cache(|| Local($crate::form::SharedStack::new()));
69            stack.0.push_owned($v)
70        })
71    }
72}
73
74crate::export! {
75    /// Store and immediately retrieve a value `$v` in `$request`'s local cache
76    /// using a locally generated anonymous type to avoid type conflicts.
77    ///
78    /// The code generated by this macro is expected to be invoked at-most once
79    /// per-request. This is because while `local_cache_once!` generates a new
80    /// type for the _macro_ invocation, that type is the same per run-time
81    /// invocation. Thus, for a given request, a `local_cache_once!` invocation
82    /// always returns the same reference.
83    ///
84    /// To get a unique request-local reference to string-like values, use
85    /// [`local_cache!`] instead.
86    ///
87    /// # Example
88    ///
89    /// ```rust
90    /// use rocket::request::local_cache_once;
91    /// # let c = rocket::local::blocking::Client::debug_with(vec![]).unwrap();
92    /// # let request = c.get("/");
93    ///
94    /// // The first store into local cache for a given type wins.
95    /// assert_eq!(request.local_cache(|| String::from("hello")), "hello");
96    ///
97    /// // The following returns the cached, previously stored value for the type.
98    /// assert_eq!(request.local_cache(|| String::from("goodbye")), "hello");
99    ///
100    /// // This shows that we cannot cache different values of the same type;
101    /// // we _must_ use a proxy type. To avoid the need to write these manually,
102    /// // use `local_cache_once!`, which generates one of the fly.
103    /// assert_eq!(local_cache_once!(request, String::from("hello")), "hello");
104    /// assert_eq!(local_cache_once!(request, String::from("goodbye")), "goodbye");
105    ///
106    /// // But a macro invocation for the same request always resolves to the
107    /// // first reference as the unique type is generated at compile-time.
108    /// for i in 1..4 {
109    ///     assert_eq!(local_cache_once!(request, i.to_string()), "1");
110    /// }
111    /// ```
112    macro_rules! local_cache_once {
113        ($request:expr, $v:expr $(,)?) => ({
114            struct Local<T>(T);
115            &$request.local_cache(move || Local($v)).0
116        })
117    }
118}