pub struct ETag(/* private fields */);Expand description
An entity tag naming the version of what a page shows: weak (W/"<hash>", the default) or strong ("<hash>").
Build it from everything the page displays, so it changes when the page
would: the records, plus the locale, the signed-in user and the flash when
the page shows them. The value is the first 128 bits of a SHA-256, as 32
hex characters. It implements [IntoResponseParts] (sets ETag); pass it
to Conditional::fresh_when to answer 304s. Like Rails, tags are weak
unless built with strong.
§Examples
use ocre::cache::ETag;
let a = ETag::new("post-42-1767225600");
assert_eq!(a, ETag::new("post-42-1767225600"));
assert_ne!(a, ETag::new("post-42-1767225601"));
assert!(a.as_str().starts_with("W/\""));Implementations§
Source§impl ETag
impl ETag
Sourcepub fn strong(version: impl AsRef<[u8]>) -> Self
pub fn strong(version: impl AsRef<[u8]>) -> Self
A strong tag ("<hash>", Rails’ strong_etag:) for a version string you build.
A strong tag promises that two responses with the same tag are
byte-for-byte identical, which caches and range requests rely on; use
it for a file or an exact body, not for a page whose HTML may vary
(nonces, CSRF tokens). If-None-Match compares weakly, so a strong
tag also answers 304s through Conditional.
§Examples
use ocre::cache::ETag;
let etag = ETag::strong("report-2026-09.csv");
assert!(etag.as_str().starts_with('"'));
assert_eq!(etag.as_str().len(), 34); // " + 32 hex + "
assert!(etag.is_strong() && !ETag::new("x").is_strong());Sourcepub fn of<T: Serialize + ?Sized>(data: &T) -> Result<Self>
pub fn of<T: Serialize + ?Sized>(data: &T) -> Result<Self>
A tag for any serializable data, hashed as its JSON: ETag::of(&(&posts, i18n.locale()))?.
§Errors
Error::Internal (500) when data does not serialize to JSON (e.g.
a map with non-string keys).
§Examples
use ocre::cache::ETag;
assert_eq!(ETag::of(&("Ada", 1))?, ETag::of(&("Ada", 1))?);
assert_ne!(ETag::of(&("Ada", 1))?, ETag::of(&("Ada", 2))?);Trait Implementations§
impl Eq for ETag
Source§impl IntoResponseParts for ETag
impl IntoResponseParts for ETag
Source§type Error = Infallible
type Error = Infallible
Source§fn into_response_parts(
self,
res: ResponseParts,
) -> Result<ResponseParts, Self::Error>
fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::Error>
impl StructuralPartialEq for ETag
Auto Trait Implementations§
impl Freeze for ETag
impl RefUnwindSafe for ETag
impl Send for ETag
impl Sync for ETag
impl Unpin for ETag
impl UnsafeUnpin for ETag
impl UnwindSafe for ETag
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.