@@ -131,10 +131,12 @@ where
131131 ///
132132 /// If necessary, will remove the value at the front of the LRU list to make room.
133133 #[ inline]
134- pub fn insert ( & mut self , k : K , v : V ) -> Option < V > {
134+ pub fn insert < F : FnOnce ( K , V ) > ( & mut self , k : K , v : V , remove_lru_callback : F ) -> Option < V > {
135135 let old_val = self . map . insert ( k, v) ;
136136 if self . len ( ) > self . capacity ( ) {
137- self . remove_lru ( ) ;
137+ if let Some ( x) = self . remove_lru ( ) {
138+ remove_lru_callback ( x. 0 , x. 1 )
139+ }
138140 }
139141 old_val
140142 }
@@ -196,9 +198,11 @@ where
196198 /// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
197199 /// the LRU list.
198200 #[ inline]
199- pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V , S > {
201+ pub fn entry < F : FnOnce ( K , V ) > ( & mut self , key : K , remove_lru_callback : F ) -> Entry < ' _ , K , V , S > {
200202 if self . len ( ) > self . capacity ( ) {
201- self . remove_lru ( ) ;
203+ if let Some ( x) = self . remove_lru ( ) {
204+ remove_lru_callback ( x. 0 , x. 1 )
205+ }
202206 }
203207 self . map . entry ( key)
204208 }
@@ -218,9 +222,14 @@ where
218222 /// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
219223 /// entry in the LRU list.
220224 #[ inline]
221- pub fn raw_entry_mut ( & mut self ) -> RawEntryBuilderMut < ' _ , K , V , S > {
225+ pub fn raw_entry_mut < F : FnOnce ( K , V ) > (
226+ & mut self ,
227+ remove_lru_callback : F ,
228+ ) -> RawEntryBuilderMut < ' _ , K , V , S > {
222229 if self . len ( ) > self . capacity ( ) {
223- self . remove_lru ( ) ;
230+ if let Some ( x) = self . remove_lru ( ) {
231+ remove_lru_callback ( x. 0 , x. 1 )
232+ }
224233 }
225234 self . map . raw_entry_mut ( )
226235 }
@@ -248,9 +257,11 @@ where
248257 /// If there are more entries in the `LruCache` than the new capacity will allow, they are
249258 /// removed.
250259 #[ inline]
251- pub fn set_capacity ( & mut self , capacity : usize ) {
260+ pub fn set_capacity < F : Fn ( K , V ) > ( & mut self , capacity : usize , remove_lru_callback : F ) {
252261 for _ in capacity..self . len ( ) {
253- self . remove_lru ( ) ;
262+ if let Some ( x) = self . remove_lru ( ) {
263+ remove_lru_callback ( x. 0 , x. 1 )
264+ }
254265 }
255266 self . max_size = capacity;
256267 }
@@ -286,7 +297,11 @@ impl<K: Eq + Hash, V, S: BuildHasher + Default> Extend<(K, V)> for LruCache<K, V
286297 #[ inline]
287298 fn extend < I : IntoIterator < Item = ( K , V ) > > ( & mut self , iter : I ) {
288299 for ( k, v) in iter {
289- self . insert ( k, v) ;
300+ //self.insert(k, v);
301+ self . map . insert ( k, v) ;
302+ if self . len ( ) > self . capacity ( ) {
303+ self . remove_lru ( ) ;
304+ }
290305 }
291306 }
292307}
0 commit comments