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

This commit is contained in:
Sergio Benitez 2018-08-13 00:45:18 -07:00
parent 7abfaafcf9
commit 948a9e6720
1 changed files with 31 additions and 31 deletions

View File

@ -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