use crate::weight::{WeightScale, ZeroWeightScale}; use std::collections::hash_map::RandomState; use std::hash::BuildHasher; use std::marker::PhantomData; use std::num::NonZeroUsize; /// A configuration structure used to create an LRU cache. pub struct CLruCacheConfig { pub(crate) capacity: NonZeroUsize, pub(crate) hash_builder: S, pub(crate) reserve: Option, pub(crate) scale: W, _marker: PhantomData<(K, V)>, } impl CLruCacheConfig { /// Creates a new configuration that will create an LRU cache /// that will hold at most `capacity` elements and default parameters. pub fn new(capacity: NonZeroUsize) -> Self { Self { capacity, hash_builder: RandomState::default(), reserve: None, scale: ZeroWeightScale, _marker: PhantomData, } } } impl> CLruCacheConfig { /// Configure the provided hash builder. pub fn with_hasher(self, hash_builder: O) -> CLruCacheConfig { let Self { capacity, reserve, scale, _marker, .. } = self; CLruCacheConfig { capacity, hash_builder, reserve, scale, _marker, } } /// Configure the amount of pre-allocated memory in order to hold at least `reserve` elements /// without reallocating. pub fn with_memory(mut self, reserve: usize) -> Self { self.reserve = Some(reserve); self } /// Configure the provided scale. pub fn with_scale>(self, scale: O) -> CLruCacheConfig { let Self { capacity, hash_builder, reserve, .. } = self; CLruCacheConfig { capacity, hash_builder, reserve, scale, _marker: PhantomData, } } }