Trait toad_msg::CacheKey

source ·
pub trait CacheKeywhere
    Self: Sized + Debug,{
    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§

source

type Hasher: Hasher

Type used to generate hashes

Required Methods§

source

fn hasher(&mut self) -> &mut Self::Hasher

source

fn add_cache_key<P, O>(&mut self, msg: &Message<P, O>)where P: Array<Item = u8> + AppendCopy<u8>, O: OptionMap,

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§

source

fn cache_key<P, O>(&mut self, msg: &Message<P, O>) -> u64where P: Array<Item = u8> + AppendCopy<u8>, O: OptionMap,

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

Implementations on Foreign Types§

source§

impl<T> CacheKey for &mut Twhere T: CacheKey,

§

type Hasher = <T as CacheKey>::Hasher

source§

fn hasher(&mut self) -> &mut Self::Hasher

source§

fn add_cache_key<P, O>(&mut self, msg: &Message<P, O>)where P: Array<Item = u8> + AppendCopy<u8>, O: OptionMap,

Implementors§