Rocket

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 948a9e6720d99ec9f5487a3320916de6f8fadce0
parent 7abfaafcf9b72d56f9315cf7f4e8444a62f88407
Author: Sergio Benitez <sb@sergio.bz>
Date:   Mon, 13 Aug 2018 00:45:18 -0700

Move 'Request::local_cache()' method for nicer docs.

Diffstat:
Mcore/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 @@ -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<T, F>(&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<T, F>(&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