diff --git a/contrib/lib/src/databases/connection.rs b/contrib/lib/src/databases/connection.rs index 5aa76495..5471181b 100644 --- a/contrib/lib/src/databases/connection.rs +++ b/contrib/lib/src/databases/connection.rs @@ -177,7 +177,7 @@ impl<'r, K: 'static, C: Poolable> FromRequest<'r> for Connection { #[inline] async fn from_request(request: &'r Request<'_>) -> Outcome { - match request.managed_state::>() { + match request.rocket().state::>() { Some(c) => c.get().await.into_outcome(Status::ServiceUnavailable), None => { error_!("Missing database fairing for `{}`", std::any::type_name::()); diff --git a/contrib/lib/src/templates/fairing.rs b/contrib/lib/src/templates/fairing.rs index a0cd5d21..8bb20c63 100644 --- a/contrib/lib/src/templates/fairing.rs +++ b/contrib/lib/src/templates/fairing.rs @@ -185,7 +185,7 @@ impl Fairing for TemplateFairing { #[cfg(debug_assertions)] async fn on_request(&self, req: &mut rocket::Request<'_>, _data: &mut rocket::Data) { - let cm = req.managed_state::() + let cm = req.rocket().state::() .expect("Template ContextManager registered in on_attach"); cm.reload_if_needed(&self.callback); diff --git a/contrib/lib/src/templates/mod.rs b/contrib/lib/src/templates/mod.rs index e2d6d344..3a4d3bab 100644 --- a/contrib/lib/src/templates/mod.rs +++ b/contrib/lib/src/templates/mod.rs @@ -420,7 +420,7 @@ impl Template { impl<'r> Responder<'r, 'static> for Template { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { let (render, content_type) = { - let ctxt = req.managed_state::().ok_or_else(|| { + let ctxt = req.rocket().state::().ok_or_else(|| { error_!("Uninitialized template context: missing fairing."); info_!("To use templates, you must attach `Template::fairing()`."); info_!("See the `Template` documentation for more information."); diff --git a/core/lib/src/config/config.rs b/core/lib/src/config/config.rs index 674ccb63..2813f5da 100644 --- a/core/lib/src/config/config.rs +++ b/core/lib/src/config/config.rs @@ -384,7 +384,7 @@ impl<'r> FromRequest<'r> for &'r Config { type Error = std::convert::Infallible; async fn from_request(req: &'r Request<'_>) -> request::Outcome { - request::Outcome::Success(req.config()) + request::Outcome::Success(req.rocket().config()) } } diff --git a/core/lib/src/config/secret_key.rs b/core/lib/src/config/secret_key.rs index a3553b79..19a35942 100644 --- a/core/lib/src/config/secret_key.rs +++ b/core/lib/src/config/secret_key.rs @@ -186,7 +186,7 @@ impl<'r> FromRequest<'r> for &'r SecretKey { type Error = std::convert::Infallible; async fn from_request(req: &'r Request<'_>) -> Outcome { - Outcome::Success(&req.config().secret_key) + Outcome::Success(&req.rocket().config().secret_key) } } diff --git a/core/lib/src/data/temp_file.rs b/core/lib/src/data/temp_file.rs index 0038433e..39c55f50 100644 --- a/core/lib/src/data/temp_file.rs +++ b/core/lib/src/data/temp_file.rs @@ -305,7 +305,7 @@ impl<'v> TempFile<'v> { .or_else(|| req.limits().get("file")) .unwrap_or(Limits::FILE); - let temp_dir = req.config().temp_dir.clone(); + let temp_dir = req.rocket().config().temp_dir.clone(); let file = tokio::task::spawn_blocking(move || { NamedTempFile::new_in(temp_dir) }).await.map_err(|_| { diff --git a/core/lib/src/local/asynchronous/response.rs b/core/lib/src/local/asynchronous/response.rs index 20d513b9..43394242 100644 --- a/core/lib/src/local/asynchronous/response.rs +++ b/core/lib/src/local/asynchronous/response.rs @@ -88,7 +88,7 @@ impl<'c> LocalResponse<'c> { async move { let response: Response<'c> = f(request).await; - let mut cookies = CookieJar::new(request.config()); + let mut cookies = CookieJar::new(request.rocket().config()); for cookie in response.cookies() { cookies.add_original(cookie.into_owned()); } diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index 9ed5e376..26696941 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -468,22 +468,40 @@ impl<'r> Request<'r> { } } - /// Returns the Rocket server configuration. + /// Returns the [`Rocket`] instance that is handling this request. /// /// # Example /// /// ```rust /// # let c = rocket::local::blocking::Client::debug_with(vec![]).unwrap(); /// # let request = c.get("/"); - /// let config = request.config(); + /// # type Pool = usize; + /// // Retrieve the application config via `Rocket::config()`. + /// let config = request.rocket().config(); + /// + /// // Retrieve managed state via `Rocket::state()`. + /// let state = request.rocket().state::(); + /// + /// // Get a list of all of the registered routes and catchers. + /// let routes = request.rocket().routes(); + /// let catchers = request.rocket().catchers(); /// ``` #[inline(always)] - pub fn config(&self) -> &'r Config { - &self.state.rocket.config + pub fn rocket(&self) -> &'r Rocket { + &self.state.rocket } /// Returns the configured application data limits. /// + /// This is convenience function equivalent to: + /// + /// ```rust + /// # let c = rocket::local::blocking::Client::debug_with(vec![]).unwrap(); + /// # let request = c.get("/"); + /// &request.rocket().config().limits + /// # ; + /// ``` + /// /// # Example /// /// ```rust @@ -499,7 +517,7 @@ impl<'r> Request<'r> { /// ``` #[inline(always)] pub fn limits(&self) -> &'r Limits { - &self.config().limits + &self.rocket().config().limits } /// Get the presently matched route, if any. @@ -541,23 +559,6 @@ impl<'r> Request<'r> { T::from_request(self) } - /// Retrieve managed state. - /// - /// # Example - /// - /// ```rust - /// # let c = rocket::local::blocking::Client::debug_with(vec![]).unwrap(); - /// # let request = c.get("/"); - /// # type Pool = usize; - /// let pool = request.managed_state::(); - /// ``` - #[inline(always)] - pub fn managed_state(&self) -> Option<&'r T> - where T: Send + Sync + 'static - { - self.state.rocket.state::() - } - /// Retrieves the cached value for type `T` from the request-local cached /// state of `self`. If no such value has previously been cached for this /// request, `f` is called to produce the value which is subsequently diff --git a/core/lib/src/state.rs b/core/lib/src/state.rs index a00841e2..d4030d5c 100644 --- a/core/lib/src/state.rs +++ b/core/lib/src/state.rs @@ -54,8 +54,8 @@ use crate::http::Status; /// /// Because `State` is itself a request guard, managed state can be retrieved /// from another request guard's implementation using either -/// [`Request::guard()`] or [`Request::managed_state()`]. In the following code -/// example, the `Item` request guard retrieves `MyConfig` from managed state: +/// [`Request::guard()`] or [`Rocket::state()`]. In the following code example, +/// the `Item` request guard retrieves `MyConfig` from managed state: /// /// ```rust /// use rocket::State; @@ -75,7 +75,7 @@ use crate::http::Status; /// .map(|my_config| Item(&my_config.inner().user_val)); /// /// // Or alternatively, using `Request::managed_state()`: -/// let outcome = request.managed_state::() +/// let outcome = request.rocket().state::() /// .map(|my_config| Item(&my_config.user_val)) /// .or_forward(()); /// @@ -174,7 +174,7 @@ impl<'r, T: Send + Sync + 'static> FromRequest<'r> for State<'r, T> { #[inline(always)] async fn from_request(req: &'r Request<'_>) -> request::Outcome { - match req.managed_state::() { + match req.rocket().state::() { Some(state) => Outcome::Success(State(state)), None => { error_!("Attempted to retrieve unmanaged state `{}`!", std::any::type_name::());