From 948a9e6720d99ec9f5487a3320916de6f8fadce0 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 13 Aug 2018 00:45:18 -0700 Subject: [PATCH] Move 'Request::local_cache()' method for nicer docs. --- core/lib/src/request/request.rs | 62 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index b87f6267..d8be7a09 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -81,37 +81,6 @@ impl<'r> Request<'r> { f(&mut request); } - /// 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 - /// returned. - /// - /// # Example - /// - /// ```rust - /// # use rocket::http::Method; - /// # use rocket::Request; - /// # struct User; - /// fn current_user(request: &Request) -> User { - /// // Validate request for a given user, load from database, etc. - /// # User - /// } - /// - /// # Request::example(Method::Get, "/uri", |request| { - /// let user = request.local_cache(|| current_user(request)); - /// # }); - /// ``` - pub fn local_cache(&self, f: F) -> &T - where F: FnOnce() -> T, - T: Send + Sync + 'static - { - self.state.cache.try_get() - .unwrap_or_else(|| { - self.state.cache.set(f()); - self.state.cache.get() - }) - } - /// Retrieve the method from `self`. /// /// # Example @@ -553,6 +522,37 @@ impl<'r> Request<'r> { T::from_request(self) } + /// 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 + /// returned. + /// + /// # Example + /// + /// ```rust + /// # use rocket::http::Method; + /// # use rocket::Request; + /// # struct User; + /// fn current_user(request: &Request) -> User { + /// // Validate request for a given user, load from database, etc. + /// # User + /// } + /// + /// # Request::example(Method::Get, "/uri", |request| { + /// let user = request.local_cache(|| current_user(request)); + /// # }); + /// ``` + pub fn local_cache(&self, f: F) -> &T + where F: FnOnce() -> T, + T: Send + Sync + 'static + { + self.state.cache.try_get() + .unwrap_or_else(|| { + self.state.cache.set(f()); + self.state.cache.get() + }) + } + /// Retrieves and parses into `T` the 0-indexed `n`th dynamic parameter from /// the request. Returns `Error::NoKey` if `n` is greater than the number of /// params. Returns `Error::BadParse` if the parameter type `T` can't be