pub trait CacheKey{
type Hasher: Hasher;
// Required methods
fn hasher(&mut self) -> &mut Self::Hasher;
fn add_cache_key<P, O>(&mut self, msg: &Message<P, O>)
where P: Array<Item = u8> + AppendCopy<u8>,
O: OptionMap;
// Provided method
fn cache_key<P, O>(&mut self, msg: &Message<P, O>) -> u64
where P: Array<Item = u8> + AppendCopy<u8>,
O: OptionMap { ... }
}Expand description
The cache key can be used to compare messages for representing the same action against the same resource; for example requests with different IDs but the same method and cache-key affecting options (ex. path, query parameters) will yield the same cache-key.
Extends core::hash::Hash with the ability to build a cache-key of a message
in the hasher’s state.
DefaultCacheKey Provides a default implementation.
Required Associated Types§
Required Methods§
fn hasher(&mut self) -> &mut Self::Hasher
Sourcefn add_cache_key<P, O>(&mut self, msg: &Message<P, O>)
fn add_cache_key<P, O>(&mut self, msg: &Message<P, O>)
Add this message’s cache key to the hasher’s internal state.
After invoking this, to get the u64 hash use Hasher::finish.
Alternately, use CacheKey::cache_key to go directly to the u64 hash.
Provided Methods§
Sourcefn cache_key<P, O>(&mut self, msg: &Message<P, O>) -> u64
fn cache_key<P, O>(&mut self, msg: &Message<P, O>) -> u64
Add this message’s cache key to the hasher’s internal state and yield the u64 hash.
use core::hash::Hasher;
use toad_msg::alloc::Message;
use toad_msg::Type::Con;
use toad_msg::{CacheKey, Code, ContentFormat, DefaultCacheKey, Id, MessageOptions, Token};
let mut msg_a = Message::new(Con, Code::GET, Id(1), Token(Default::default()));
msg_a.set_path("foo/bar");
msg_a.set_accept(ContentFormat::Text);
let mut ha = DefaultCacheKey::new();
ha.cache_key(&msg_a);
let mut msg_b = Message::new(Con, Code::GET, Id(2), Token(Default::default()));
msg_b.set_accept(ContentFormat::Text);
msg_b.set_path("foo/bar");
let mut hb = DefaultCacheKey::new();
hb.cache_key(&msg_b);
assert_eq!(ha.hasher().finish(), hb.hasher().finish());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.