mirror of https://github.com/rwf2/Rocket.git
Small fixes to request-local state cache implementation.
This commit is contained in:
parent
97c6b3ace8
commit
351b8f7c37
|
@ -22,7 +22,7 @@ members = [
|
||||||
"examples/content_types",
|
"examples/content_types",
|
||||||
"examples/ranking",
|
"examples/ranking",
|
||||||
"examples/testing",
|
"examples/testing",
|
||||||
"examples/request_state_local_cache",
|
"examples/request_local_state",
|
||||||
"examples/request_guard",
|
"examples/request_guard",
|
||||||
"examples/stream",
|
"examples/stream",
|
||||||
"examples/json",
|
"examples/json",
|
||||||
|
|
|
@ -82,8 +82,8 @@ impl<'r> Request<'r> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the cached value for type `T` from the request-local cached
|
/// Retrieves the cached value for type `T` from the request-local cached
|
||||||
/// state of `self`. If no such value has previously been cached for
|
/// state of `self`. If no such value has previously been cached for this
|
||||||
/// this request, `f` is called to produce the value which is subsequently
|
/// request, `f` is called to produce the value which is subsequently
|
||||||
/// returned.
|
/// returned.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
@ -92,26 +92,24 @@ impl<'r> Request<'r> {
|
||||||
/// # use rocket::http::Method;
|
/// # use rocket::http::Method;
|
||||||
/// # use rocket::Request;
|
/// # use rocket::Request;
|
||||||
/// # struct User;
|
/// # struct User;
|
||||||
/// fn current_user() -> User {
|
/// fn current_user(request: &Request) -> User {
|
||||||
/// // Load user...
|
/// // Validate request for a given user, load from database, etc.
|
||||||
/// # User
|
/// # User
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// # Request::example(Method::Get, "/uri", |request| {
|
/// # Request::example(Method::Get, "/uri", |request| {
|
||||||
/// let user = request.local_cache(current_user);
|
/// let user = request.local_cache(|| current_user(request));
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
pub fn local_cache<T, F>(&self, f: F) -> &T
|
pub fn local_cache<T, F>(&self, f: F) -> &T
|
||||||
where T: Send + Sync + 'static,
|
where F: FnOnce() -> T,
|
||||||
F: FnOnce() -> T {
|
T: Send + Sync + 'static
|
||||||
|
{
|
||||||
match self.state.cache.try_get() {
|
self.state.cache.try_get()
|
||||||
Some(cached) => cached,
|
.unwrap_or_else(|| {
|
||||||
None => {
|
|
||||||
self.state.cache.set(f());
|
self.state.cache.set(f());
|
||||||
self.state.cache.get()
|
self.state.cache.get()
|
||||||
}
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve the method from `self`.
|
/// Retrieve the method from `self`.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "request_state_local_cache"
|
name = "request_local_state"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
publish = false
|
publish = false
|
|
@ -1,5 +1,6 @@
|
||||||
#![feature(plugin, decl_macro)]
|
#![feature(plugin, decl_macro)]
|
||||||
#![plugin(rocket_codegen)]
|
#![plugin(rocket_codegen)]
|
||||||
|
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
@ -1,7 +1,6 @@
|
||||||
use std::sync::atomic::{Ordering};
|
use std::sync::atomic::{Ordering};
|
||||||
|
|
||||||
use ::Atomics;
|
use super::{rocket, Atomics};
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
Loading…
Reference in New Issue