mirror of https://github.com/rwf2/Rocket.git
Move 'Request::local_cache()' method for nicer docs.
This commit is contained in:
parent
7abfaafcf9
commit
948a9e6720
|
@ -81,37 +81,6 @@ impl<'r> Request<'r> {
|
||||||
f(&mut request);
|
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`.
|
/// Retrieve the method from `self`.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
@ -553,6 +522,37 @@ impl<'r> Request<'r> {
|
||||||
T::from_request(self)
|
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
|
/// 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
|
/// 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
|
/// params. Returns `Error::BadParse` if the parameter type `T` can't be
|
||||||
|
|
Loading…
Reference in New Issue