1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ use std:: alloc:: { Allocator , Global } ;
1516use std:: cmp:: Eq ;
1617use std:: hash:: { BuildHasher , Hash } ;
1718use std:: ops:: { Deref , DerefMut } ;
1819
1920use lru:: { DefaultHasher , LruCache } ;
2021
2122/// A wrapper for [`LruCache`] which provides manual eviction.
22- pub struct EvictableHashMap < K , V , S = DefaultHasher > {
23- inner : LruCache < K , V , S > ,
23+ pub struct EvictableHashMap < K , V , S = DefaultHasher , A : Clone + Allocator = Global > {
24+ inner : LruCache < K , V , S , A > ,
2425
2526 /// Target capacity to keep when calling `evict_to_target_cap`.
2627 target_cap : usize ,
2728}
2829
30+ impl < K : Hash + Eq , V , A : Clone + Allocator > EvictableHashMap < K , V , DefaultHasher , A > {
31+ /// Create a [`EvictableHashMap`] with the given target capacity and allocator.
32+ pub fn new_in ( target_cap : usize , alloc : A ) -> Self {
33+ Self :: with_hasher_in ( target_cap, DefaultHasher :: new ( ) , alloc)
34+ }
35+ }
36+
37+ impl < K : Hash + Eq , V , S : BuildHasher , A : Clone + Allocator > EvictableHashMap < K , V , S , A > {
38+ /// Create a [`EvictableHashMap`] with the given target capacity, hasher and allocator.
39+ pub fn with_hasher_in ( target_cap : usize , hasher : S , alloc : A ) -> Self {
40+ Self {
41+ inner : LruCache :: unbounded_with_hasher_in ( hasher, alloc) ,
42+ target_cap,
43+ }
44+ }
45+ }
46+
2947impl < K : Hash + Eq , V > EvictableHashMap < K , V > {
3048 /// Create a [`EvictableHashMap`] with the given target capacity.
3149 pub fn new ( target_cap : usize ) -> EvictableHashMap < K , V > {
@@ -41,7 +59,9 @@ impl<K: Hash + Eq, V, S: BuildHasher> EvictableHashMap<K, V, S> {
4159 target_cap,
4260 }
4361 }
62+ }
4463
64+ impl < K : Hash + Eq , V , S : BuildHasher , A : Clone + Allocator > EvictableHashMap < K , V , S , A > {
4565 pub fn target_cap ( & self ) -> usize {
4666 self . target_cap
4767 }
@@ -79,15 +99,15 @@ impl<K: Hash + Eq, V, S: BuildHasher> EvictableHashMap<K, V, S> {
7999 }
80100}
81101
82- impl < K , V , S > Deref for EvictableHashMap < K , V , S > {
83- type Target = LruCache < K , V , S > ;
102+ impl < K , V , S , A : Clone + Allocator > Deref for EvictableHashMap < K , V , S , A > {
103+ type Target = LruCache < K , V , S , A > ;
84104
85105 fn deref ( & self ) -> & Self :: Target {
86106 & self . inner
87107 }
88108}
89109
90- impl < K , V , S > DerefMut for EvictableHashMap < K , V , S > {
110+ impl < K , V , S , A : Clone + Allocator > DerefMut for EvictableHashMap < K , V , S , A > {
91111 fn deref_mut ( & mut self ) -> & mut Self :: Target {
92112 & mut self . inner
93113 }
0 commit comments