Skip to main content

ETag

Struct ETag 

Source
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

Source

pub fn new(version: impl AsRef<[u8]>) -> Self

A tag for a version string you build, e.g. format!("{}-{}", post.id, post.updated_at).

Cheaper than of for large data: only version is hashed.

§Examples
let etag = ocre::cache::ETag::new("v1");
assert_eq!(etag.as_str().len(), 36); // W/" + 32 hex + "
Source

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());
Source

pub fn is_strong(&self) -> bool

Whether the tag is strong (built with strong).

§Examples
assert!(!ocre::cache::ETag::new("v1").is_strong());
Source

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))?);
Source

pub fn as_str(&self) -> &str

The header value, W/"<32 hex characters>" (weak) or "<32 hex characters>" (strong).

§Examples
let etag = ocre::cache::ETag::new("v1");
assert!(etag.as_str().starts_with("W/\"") && etag.as_str().ends_with('"'));

Trait Implementations§

Source§

impl Clone for ETag

Source§

fn clone(&self) -> ETag

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ETag

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ETag

Source§

impl IntoResponseParts for ETag

Source§

type Error = Infallible

The type returned in the event of an error. Read more
Source§

fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::Error>

Set parts of the response
Source§

impl PartialEq for ETag

Source§

fn eq(&self, other: &ETag) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V