Macro rocket::request::local_cache
source · pub macro local_cache($request:expr, $v:expr $(,)?) { ... }
Expand description
Store and immediately retrieve a vector-like value $v
(String
or
Vec<T>
) in $request
’s local cache using a locally generated
anonymous type to avoid type conflicts.
Unlike local_cache_once
, this macro’s generated code always returns
a unique reference to request-local cache.
Note
The value $v
must be of type String
or Vec<T>
, that is, a type
that implements the sealed trait Shareable
bb).
Example
use rocket::request::{local_cache, local_cache_once};
// The first store into local cache for a given type wins.
for i in 0..4 {
assert_eq!(request.local_cache(|| i.to_string()), "0");
}
// This shows that we cannot cache different values of the same type; we
// _must_ use a proxy type. To avoid the need to write these manually, use
// `local_cache!`, which generates one of the fly.
for i in 0..4 {
assert_eq!(local_cache!(request, i.to_string()), i.to_string());
}
// Note that while `local_cache_once!` generates a new type for the
// _macro_ invocation, that type is the same per run-time invocation, so
// all "calls" to `local_cache_once!` on the same line return the same
// reference for a given request.
for i in 1..4 {
// Note that this is `1`, so _not_ the `String` from line 4.
assert_eq!(local_cache_once!(request, i.to_string()), "1");
}