From f4acddb0562e457a204530d5c91da29605c7b10b Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sat, 11 Jul 2020 16:58:29 -0700 Subject: [PATCH] [dyn-cache] Hashed is also typed by its hasher --- dyn-cache/CHANGELOG.md | 1 + dyn-cache/src/namespace.rs | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/dyn-cache/CHANGELOG.md b/dyn-cache/CHANGELOG.md index b9410e97a..8ed1d01ca 100644 --- a/dyn-cache/CHANGELOG.md +++ b/dyn-cache/CHANGELOG.md @@ -12,6 +12,7 @@ invocations. - Crate extracted from `topo::cache` module. - `{LocalCache,SendCache}::cache` wraps `cache_with` for types that impl `Clone`. - `Gc` trait is public, inner cache types implement it. +- `Hashed` has an additional type parameter for the type of hasher used to create it. ### Changed diff --git a/dyn-cache/src/namespace.rs b/dyn-cache/src/namespace.rs index 0911c05f5..b76718253 100644 --- a/dyn-cache/src/namespace.rs +++ b/dyn-cache/src/namespace.rs @@ -8,19 +8,21 @@ use std::{ borrow::Borrow, fmt::{Debug, Formatter, Result as FmtResult}, hash::{BuildHasher, Hash, Hasher}, + marker::PhantomData, }; /// A query key that was hashed as part of an initial lookup and which can be /// used to store fresh values back to the cache. #[derive(Clone, Copy, Debug)] -pub struct Hashed { +pub struct Hashed { key: K, hash: u64, + hasher: PhantomData, } /// A namespace stores all cached values for a particular query type. -pub(super) struct Namespace { - inner: HashMap>, +pub(super) struct Namespace { + inner: HashMap, H>, } impl Default for Namespace { @@ -29,25 +31,26 @@ impl Default for Namespace { } } -impl Namespace +impl Namespace where Scope: Eq + Hash + 'static, Input: 'static, Output: 'static, + H: BuildHasher, { - fn hashed<'k, Key>(&self, key: &'k Key) -> Hashed<&'k Key> + fn hashed<'k, Key>(&self, key: &'k Key) -> Hashed<&'k Key, H> where Key: Hash + ?Sized, { let mut hasher = self.inner.hasher().build_hasher(); key.hash(&mut hasher); - Hashed { key, hash: hasher.finish() } + Hashed { key, hash: hasher.finish(), hasher: PhantomData } } fn entry<'k, Key>( &mut self, - hashed: &Hashed<&'k Key>, - ) -> RawEntryMut, DefaultHashBuilder> + hashed: &Hashed<&'k Key, H>, + ) -> RawEntryMut, H> where Key: Eq + ?Sized, Scope: Borrow, @@ -59,7 +62,7 @@ where &mut self, key: &'k Key, input: &Arg, - ) -> Result<&Output, Hashed<&'k Key>> + ) -> Result<&Output, Hashed<&'k Key, H>> where Key: Eq + Hash + ?Sized, Scope: Borrow, @@ -74,7 +77,7 @@ where } } - pub(super) fn store(&mut self, hashed: Hashed<&Key>, input: Input, output: Output) + pub(super) fn store(&mut self, hashed: Hashed<&Key, H>, input: Input, output: Output) where Key: Eq + Hash + ToOwned + ?Sized, Scope: Borrow,