From 351b8f7c37d45f689529c213f3cac6553c9a568a Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sat, 7 Jul 2018 18:51:26 -0700 Subject: [PATCH] Small fixes to request-local state cache implementation. --- Cargo.toml | 2 +- core/lib/src/request/request.rs | 24 +++++++++---------- .../Cargo.toml | 2 +- .../src/main.rs | 1 + .../src/tests.rs | 3 +-- 5 files changed, 15 insertions(+), 17 deletions(-) rename examples/{request_state_local_cache => request_local_state}/Cargo.toml (82%) rename examples/{request_state_local_cache => request_local_state}/src/main.rs (99%) rename examples/{request_state_local_cache => request_local_state}/src/tests.rs (91%) diff --git a/Cargo.toml b/Cargo.toml index bd003e43..74475d03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ members = [ "examples/content_types", "examples/ranking", "examples/testing", - "examples/request_state_local_cache", + "examples/request_local_state", "examples/request_guard", "examples/stream", "examples/json", diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index e93a90f2..c362a73d 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -82,8 +82,8 @@ impl<'r> Request<'r> { } /// 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 + /// 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 @@ -92,26 +92,24 @@ impl<'r> Request<'r> { /// # use rocket::http::Method; /// # use rocket::Request; /// # struct User; - /// fn current_user() -> User { - /// // Load 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); + /// let user = request.local_cache(|| current_user(request)); /// # }); /// ``` pub fn local_cache(&self, f: F) -> &T - where T: Send + Sync + 'static, - F: FnOnce() -> T { - - match self.state.cache.try_get() { - Some(cached) => cached, - None => { + 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`. diff --git a/examples/request_state_local_cache/Cargo.toml b/examples/request_local_state/Cargo.toml similarity index 82% rename from examples/request_state_local_cache/Cargo.toml rename to examples/request_local_state/Cargo.toml index 0c720023..0de3d204 100644 --- a/examples/request_state_local_cache/Cargo.toml +++ b/examples/request_local_state/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "request_state_local_cache" +name = "request_local_state" version = "0.0.0" workspace = "../../" publish = false diff --git a/examples/request_state_local_cache/src/main.rs b/examples/request_local_state/src/main.rs similarity index 99% rename from examples/request_state_local_cache/src/main.rs rename to examples/request_local_state/src/main.rs index c51cefa1..8d90f2e7 100644 --- a/examples/request_state_local_cache/src/main.rs +++ b/examples/request_local_state/src/main.rs @@ -1,5 +1,6 @@ #![feature(plugin, decl_macro)] #![plugin(rocket_codegen)] + extern crate rocket; use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/examples/request_state_local_cache/src/tests.rs b/examples/request_local_state/src/tests.rs similarity index 91% rename from examples/request_state_local_cache/src/tests.rs rename to examples/request_local_state/src/tests.rs index 1e16f7d8..602b1694 100644 --- a/examples/request_state_local_cache/src/tests.rs +++ b/examples/request_local_state/src/tests.rs @@ -1,7 +1,6 @@ use std::sync::atomic::{Ordering}; -use ::Atomics; -use super::rocket; +use super::{rocket, Atomics}; use rocket::local::Client; #[test]