@@ -131,7 +131,25 @@ 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 < F : FnOnce ( K , V ) > ( & mut self , k : K , v : V , remove_lru_callback : F ) -> Option < V > {
134+ pub fn insert ( & mut self , k : K , v : V ) -> Option < V > {
135+ let old_val = self . map . insert ( k, v) ;
136+ if self . len ( ) > self . capacity ( ) {
137+ self . remove_lru ( ) ;
138+ }
139+ old_val
140+ }
141+
142+ /// Insert a new value into the `LruCache` with LRU callback.
143+ ///
144+ /// If necessary, will remove the value at the front of the LRU list to make room.
145+ /// Calls a callback if there was an LRU removed value
146+ #[ inline]
147+ pub fn insert_with_callback < F : FnOnce ( K , V ) > (
148+ & mut self ,
149+ k : K ,
150+ v : V ,
151+ remove_lru_callback : F ,
152+ ) -> Option < V > {
135153 let old_val = self . map . insert ( k, v) ;
136154 if self . len ( ) > self . capacity ( ) {
137155 if let Some ( x) = self . remove_lru ( ) {
@@ -198,7 +216,28 @@ where
198216 /// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
199217 /// the LRU list.
200218 #[ inline]
201- pub fn entry < F : FnOnce ( K , V ) > ( & mut self , key : K , remove_lru_callback : F ) -> Entry < ' _ , K , V , S > {
219+ pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V , S > {
220+ if self . len ( ) > self . capacity ( ) {
221+ self . remove_lru ( ) ;
222+ }
223+ self . map . entry ( key)
224+ }
225+
226+ /// Like `entry` but with LRU callback.
227+ /// If the returned entry is vacant, it will always have room to insert a single value. By
228+ /// using the entry API, you can exceed the configured capacity by 1.
229+ ///
230+ /// The returned entry is not automatically moved to the back of the LRU list. By calling
231+ /// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
232+ /// the LRU list.
233+ /// Calls a callback if there was an LRU removed value
234+
235+ #[ inline]
236+ pub fn entry_with_callback < F : FnOnce ( K , V ) > (
237+ & mut self ,
238+ key : K ,
239+ remove_lru_callback : F ,
240+ ) -> Entry < ' _ , K , V , S > {
202241 if self . len ( ) > self . capacity ( ) {
203242 if let Some ( x) = self . remove_lru ( ) {
204243 remove_lru_callback ( x. 0 , x. 1 )
@@ -221,8 +260,25 @@ where
221260 /// The constructed raw entry is never automatically moved to the back of the LRU list. By
222261 /// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
223262 /// entry in the LRU list.
263+ /// Calls a callback if there was an LRU removed value
264+
224265 #[ inline]
225- pub fn raw_entry_mut < F : FnOnce ( K , V ) > (
266+ pub fn raw_entry_mut ( & mut self ) -> RawEntryBuilderMut < ' _ , K , V , S > {
267+ if self . len ( ) > self . capacity ( ) {
268+ self . remove_lru ( ) ;
269+ }
270+ self . map . raw_entry_mut ( )
271+ }
272+
273+ /// Like `raw_entry` but with LRU callback.
274+ /// If the constructed raw entry is vacant, it will always have room to insert a single value.
275+ /// By using the raw entry API, you can exceed the configured capacity by 1.
276+ ///
277+ /// The constructed raw entry is never automatically moved to the back of the LRU list. By
278+ /// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
279+ /// entry in the LRU list.
280+ #[ inline]
281+ pub fn raw_entry_mut_with_callback < F : FnOnce ( K , V ) > (
226282 & mut self ,
227283 remove_lru_callback : F ,
228284 ) -> RawEntryBuilderMut < ' _ , K , V , S > {
@@ -257,7 +313,25 @@ where
257313 /// If there are more entries in the `LruCache` than the new capacity will allow, they are
258314 /// removed.
259315 #[ inline]
260- pub fn set_capacity < F : Fn ( K , V ) > ( & mut self , capacity : usize , remove_lru_callback : F ) {
316+ pub fn set_capacity ( & mut self , capacity : usize ) {
317+ for _ in capacity..self . len ( ) {
318+ self . remove_lru ( ) ;
319+ }
320+ self . max_size = capacity;
321+ }
322+
323+ /// Like `set_capacity` but with LRU callback.
324+ /// Set the new cache capacity for the `LruCache` with an LRU callback.
325+ ///
326+ /// If there are more entries in the `LruCache` than the new capacity will allow, they are
327+ /// removed.
328+ /// Calls a callback if there was an LRU removed value
329+ #[ inline]
330+ pub fn set_capacity_with_callback < F : Fn ( K , V ) > (
331+ & mut self ,
332+ capacity : usize ,
333+ remove_lru_callback : F ,
334+ ) {
261335 for _ in capacity..self . len ( ) {
262336 if let Some ( x) = self . remove_lru ( ) {
263337 remove_lru_callback ( x. 0 , x. 1 )
0 commit comments