1#![no_std]
61
62#[cfg(feature = "hashbrown")]
63extern crate hashbrown;
64
65#[cfg(test)]
66extern crate scoped_threadpool;
67
68use alloc::borrow::Borrow;
69use alloc::boxed::Box;
70use core::fmt;
71use core::hash::{BuildHasher, Hash, Hasher};
72use core::iter::FusedIterator;
73use core::marker::PhantomData;
74use core::mem;
75use core::num::NonZeroUsize;
76use core::ptr::{self, NonNull};
77
78#[cfg(any(test, not(feature = "hashbrown")))]
79extern crate std;
80
81#[cfg(feature = "hashbrown")]
82use hashbrown::HashMap;
83#[cfg(not(feature = "hashbrown"))]
84use std::collections::HashMap;
85
86extern crate alloc;
87
88struct KeyRef<K> {
90 k: *const K,
91}
92
93impl<K: Hash> Hash for KeyRef<K> {
94 fn hash<H: Hasher>(&self, state: &mut H) {
95 unsafe { (*self.k).hash(state) }
96 }
97}
98
99impl<K: PartialEq> PartialEq for KeyRef<K> {
100 #![allow(unknown_lints)]
103 #[allow(clippy::unconditional_recursion)]
104 fn eq(&self, other: &KeyRef<K>) -> bool {
105 unsafe { (*self.k).eq(&*other.k) }
106 }
107}
108
109impl<K: Eq> Eq for KeyRef<K> {}
110
111#[repr(transparent)]
114struct KeyWrapper<K: ?Sized>(K);
115
116impl<K: ?Sized> KeyWrapper<K> {
117 fn from_ref(key: &K) -> &Self {
118 unsafe { &*(key as *const K as *const KeyWrapper<K>) }
120 }
121}
122
123impl<K: ?Sized + Hash> Hash for KeyWrapper<K> {
124 fn hash<H: Hasher>(&self, state: &mut H) {
125 self.0.hash(state)
126 }
127}
128
129impl<K: ?Sized + PartialEq> PartialEq for KeyWrapper<K> {
130 #![allow(unknown_lints)]
133 #[allow(clippy::unconditional_recursion)]
134 fn eq(&self, other: &Self) -> bool {
135 self.0.eq(&other.0)
136 }
137}
138
139impl<K: ?Sized + Eq> Eq for KeyWrapper<K> {}
140
141impl<K, Q> Borrow<KeyWrapper<Q>> for KeyRef<K>
142where
143 K: Borrow<Q>,
144 Q: ?Sized,
145{
146 fn borrow(&self) -> &KeyWrapper<Q> {
147 let key = unsafe { &*self.k }.borrow();
148 KeyWrapper::from_ref(key)
149 }
150}
151
152struct LruEntry<K, V> {
155 key: mem::MaybeUninit<K>,
156 val: mem::MaybeUninit<V>,
157 prev: *mut LruEntry<K, V>,
158 next: *mut LruEntry<K, V>,
159}
160
161impl<K, V> LruEntry<K, V> {
162 fn new(key: K, val: V) -> Self {
163 LruEntry {
164 key: mem::MaybeUninit::new(key),
165 val: mem::MaybeUninit::new(val),
166 prev: ptr::null_mut(),
167 next: ptr::null_mut(),
168 }
169 }
170
171 fn new_sigil() -> Self {
172 LruEntry {
173 key: mem::MaybeUninit::uninit(),
174 val: mem::MaybeUninit::uninit(),
175 prev: ptr::null_mut(),
176 next: ptr::null_mut(),
177 }
178 }
179}
180
181#[cfg(feature = "hashbrown")]
182pub type DefaultHasher = hashbrown::DefaultHashBuilder;
183#[cfg(not(feature = "hashbrown"))]
184pub type DefaultHasher = std::collections::hash_map::RandomState;
185
186pub struct LruCache<K, V, S = DefaultHasher> {
188 map: HashMap<KeyRef<K>, NonNull<LruEntry<K, V>>, S>,
189 cap: NonZeroUsize,
190
191 head: *mut LruEntry<K, V>,
193 tail: *mut LruEntry<K, V>,
194}
195
196impl<K, V, S> Clone for LruCache<K, V, S>
197where
198 K: Hash + PartialEq + Eq + Clone,
199 V: Clone,
200 S: BuildHasher + Clone,
201{
202 fn clone(&self) -> Self {
203 let map_cap = if self.is_unbounded() {
204 self.len()
205 } else {
206 self.cap().get()
207 };
208 let mut new_lru = LruCache::construct(
209 self.cap(),
210 HashMap::with_capacity_and_hasher(map_cap, self.map.hasher().clone()),
211 );
212
213 for (key, value) in self.iter().rev() {
214 new_lru.push(key.clone(), value.clone());
215 }
216
217 new_lru
218 }
219}
220
221impl<K: Hash + Eq, V> LruCache<K, V> {
222 pub fn new(cap: NonZeroUsize) -> LruCache<K, V> {
232 LruCache::construct(cap, HashMap::with_capacity(cap.get()))
233 }
234
235 pub fn unbounded() -> LruCache<K, V> {
245 LruCache::construct(NonZeroUsize::MAX, HashMap::default())
246 }
247}
248
249impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
250 pub fn with_hasher(cap: NonZeroUsize, hash_builder: S) -> LruCache<K, V, S> {
263 LruCache::construct(
264 cap,
265 HashMap::with_capacity_and_hasher(cap.into(), hash_builder),
266 )
267 }
268
269 pub fn unbounded_with_hasher(hash_builder: S) -> LruCache<K, V, S> {
281 LruCache::construct(NonZeroUsize::MAX, HashMap::with_hasher(hash_builder))
282 }
283
284 fn construct(
286 cap: NonZeroUsize,
287 map: HashMap<KeyRef<K>, NonNull<LruEntry<K, V>>, S>,
288 ) -> LruCache<K, V, S> {
289 let cache = LruCache {
292 map,
293 cap,
294 head: Box::into_raw(Box::new(LruEntry::new_sigil())),
295 tail: Box::into_raw(Box::new(LruEntry::new_sigil())),
296 };
297
298 unsafe {
299 (*cache.head).next = cache.tail;
300 (*cache.tail).prev = cache.head;
301 }
302
303 cache
304 }
305
306 fn is_unbounded(&self) -> bool {
308 self.cap() == NonZeroUsize::MAX
309 }
310
311 pub fn put(&mut self, k: K, v: V) -> Option<V> {
329 self.capturing_put(k, v, false).map(|(_, v)| v)
330 }
331
332 pub fn push(&mut self, k: K, v: V) -> Option<(K, V)> {
357 self.capturing_put(k, v, true)
358 }
359
360 fn capturing_put(&mut self, k: K, mut v: V, capture: bool) -> Option<(K, V)> {
364 let node_ref = self.map.get_mut(&KeyRef { k: &k });
365
366 match node_ref {
367 Some(node_ref) => {
368 let node_ptr: *mut LruEntry<K, V> = node_ref.as_ptr();
371
372 let node_ref = unsafe { &mut (*(*node_ptr).val.as_mut_ptr()) };
374 mem::swap(&mut v, node_ref);
375 let _ = node_ref;
376
377 self.detach(node_ptr);
378 self.attach(node_ptr);
379 Some((k, v))
380 }
381 None => {
382 let (replaced, node) = self.replace_or_create_node(k, v);
383 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
384
385 self.attach(node_ptr);
386
387 let keyref = unsafe { (*node_ptr).key.as_ptr() };
388 self.map.insert(KeyRef { k: keyref }, node);
389
390 replaced.filter(|_| capture)
391 }
392 }
393 }
394
395 #[allow(clippy::type_complexity)]
398 fn replace_or_create_node(&mut self, k: K, v: V) -> (Option<(K, V)>, NonNull<LruEntry<K, V>>) {
399 if self.len() == self.cap().get() {
400 let old_key = KeyRef {
402 k: unsafe { &(*(*(*self.tail).prev).key.as_ptr()) },
403 };
404 let old_node = self.map.remove(&old_key).unwrap();
405 let node_ptr: *mut LruEntry<K, V> = old_node.as_ptr();
406
407 let replaced = unsafe {
409 (
410 mem::replace(&mut (*node_ptr).key, mem::MaybeUninit::new(k)).assume_init(),
411 mem::replace(&mut (*node_ptr).val, mem::MaybeUninit::new(v)).assume_init(),
412 )
413 };
414
415 self.detach(node_ptr);
416
417 (Some(replaced), old_node)
418 } else {
419 (None, unsafe {
422 NonNull::new_unchecked(Box::into_raw(Box::new(LruEntry::new(k, v))))
423 })
424 }
425 }
426
427 pub fn get<'a, Q>(&'a mut self, k: &Q) -> Option<&'a V>
447 where
448 K: Borrow<Q>,
449 Q: Hash + Eq + ?Sized,
450 {
451 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
452 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
453
454 self.detach(node_ptr);
455 self.attach(node_ptr);
456
457 Some(unsafe { &*(*node_ptr).val.as_ptr() })
458 } else {
459 None
460 }
461 }
462
463 pub fn get_mut<'a, Q>(&'a mut self, k: &Q) -> Option<&'a mut V>
483 where
484 K: Borrow<Q>,
485 Q: Hash + Eq + ?Sized,
486 {
487 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
488 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
489
490 self.detach(node_ptr);
491 self.attach(node_ptr);
492
493 Some(unsafe { &mut *(*node_ptr).val.as_mut_ptr() })
494 } else {
495 None
496 }
497 }
498
499 pub fn get_key_value<'a, Q>(&'a mut self, k: &Q) -> Option<(&'a K, &'a V)>
519 where
520 K: Borrow<Q>,
521 Q: Hash + Eq + ?Sized,
522 {
523 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
524 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
525
526 self.detach(node_ptr);
527 self.attach(node_ptr);
528
529 Some(unsafe { (&*(*node_ptr).key.as_ptr(), &*(*node_ptr).val.as_ptr()) })
530 } else {
531 None
532 }
533 }
534
535 pub fn get_key_value_mut<'a, Q>(&'a mut self, k: &Q) -> Option<(&'a K, &'a mut V)>
558 where
559 K: Borrow<Q>,
560 Q: Hash + Eq + ?Sized,
561 {
562 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
563 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
564
565 self.detach(node_ptr);
566 self.attach(node_ptr);
567
568 Some(unsafe {
569 (
570 &*(*node_ptr).key.as_ptr(),
571 &mut *(*node_ptr).val.as_mut_ptr(),
572 )
573 })
574 } else {
575 None
576 }
577 }
578
579 pub fn get_or_insert<F>(&mut self, k: K, f: F) -> &V
602 where
603 F: FnOnce() -> V,
604 {
605 if let Some(node) = self.map.get_mut(&KeyRef { k: &k }) {
606 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
607
608 self.detach(node_ptr);
609 self.attach(node_ptr);
610
611 unsafe { &*(*node_ptr).val.as_ptr() }
612 } else {
613 let v = f();
614 let (_, node) = self.replace_or_create_node(k, v);
615 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
616
617 self.attach(node_ptr);
618
619 let keyref = unsafe { (*node_ptr).key.as_ptr() };
620 self.map.insert(KeyRef { k: keyref }, node);
621 unsafe { &*(*node_ptr).val.as_ptr() }
622 }
623 }
624
625 pub fn get_or_insert_ref<'a, Q, F>(&'a mut self, k: &'_ Q, f: F) -> &'a V
651 where
652 K: Borrow<Q>,
653 Q: Hash + Eq + ?Sized + alloc::borrow::ToOwned<Owned = K>,
654 F: FnOnce() -> V,
655 {
656 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
657 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
658
659 self.detach(node_ptr);
660 self.attach(node_ptr);
661
662 unsafe { &*(*node_ptr).val.as_ptr() }
663 } else {
664 let v = f();
665 let (_, node) = self.replace_or_create_node(k.to_owned(), v);
666 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
667
668 self.attach(node_ptr);
669
670 let keyref = unsafe { (*node_ptr).key.as_ptr() };
671 self.map.insert(KeyRef { k: keyref }, node);
672 unsafe { &*(*node_ptr).val.as_ptr() }
673 }
674 }
675
676 pub fn try_get_or_insert<F, E>(&mut self, k: K, f: F) -> Result<&V, E>
704 where
705 F: FnOnce() -> Result<V, E>,
706 {
707 if let Some(node) = self.map.get_mut(&KeyRef { k: &k }) {
708 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
709
710 self.detach(node_ptr);
711 self.attach(node_ptr);
712
713 unsafe { Ok(&*(*node_ptr).val.as_ptr()) }
714 } else {
715 let v = f()?;
716 let (_, node) = self.replace_or_create_node(k, v);
717 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
718
719 self.attach(node_ptr);
720
721 let keyref = unsafe { (*node_ptr).key.as_ptr() };
722 self.map.insert(KeyRef { k: keyref }, node);
723 Ok(unsafe { &*(*node_ptr).val.as_ptr() })
724 }
725 }
726
727 pub fn try_get_or_insert_ref<'a, Q, F, E>(&'a mut self, k: &'_ Q, f: F) -> Result<&'a V, E>
757 where
758 K: Borrow<Q>,
759 Q: Hash + Eq + ?Sized + alloc::borrow::ToOwned<Owned = K>,
760 F: FnOnce() -> Result<V, E>,
761 {
762 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
763 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
764
765 self.detach(node_ptr);
766 self.attach(node_ptr);
767
768 unsafe { Ok(&*(*node_ptr).val.as_ptr()) }
769 } else {
770 let v = f()?;
771 let (_, node) = self.replace_or_create_node(k.to_owned(), v);
772 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
773
774 self.attach(node_ptr);
775
776 let keyref = unsafe { (*node_ptr).key.as_ptr() };
777 self.map.insert(KeyRef { k: keyref }, node);
778 Ok(unsafe { &*(*node_ptr).val.as_ptr() })
779 }
780 }
781
782 pub fn get_or_insert_mut<F>(&mut self, k: K, f: F) -> &mut V
805 where
806 F: FnOnce() -> V,
807 {
808 if let Some(node) = self.map.get_mut(&KeyRef { k: &k }) {
809 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
810
811 self.detach(node_ptr);
812 self.attach(node_ptr);
813
814 unsafe { &mut *(*node_ptr).val.as_mut_ptr() }
815 } else {
816 let v = f();
817 let (_, node) = self.replace_or_create_node(k, v);
818 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
819
820 self.attach(node_ptr);
821
822 let keyref = unsafe { (*node_ptr).key.as_ptr() };
823 self.map.insert(KeyRef { k: keyref }, node);
824 unsafe { &mut *(*node_ptr).val.as_mut_ptr() }
825 }
826 }
827
828 pub fn get_or_insert_mut_ref<'a, Q, F>(&mut self, k: &'_ Q, f: F) -> &'a mut V
853 where
854 K: Borrow<Q>,
855 Q: Hash + Eq + ?Sized + alloc::borrow::ToOwned<Owned = K>,
856 F: FnOnce() -> V,
857 {
858 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
859 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
860
861 self.detach(node_ptr);
862 self.attach(node_ptr);
863
864 unsafe { &mut *(*node_ptr).val.as_mut_ptr() }
865 } else {
866 let v = f();
867 let (_, node) = self.replace_or_create_node(k.to_owned(), v);
868 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
869
870 self.attach(node_ptr);
871
872 let keyref = unsafe { (*node_ptr).key.as_ptr() };
873 self.map.insert(KeyRef { k: keyref }, node);
874 unsafe { &mut *(*node_ptr).val.as_mut_ptr() }
875 }
876 }
877
878 pub fn try_get_or_insert_mut<F, E>(&mut self, k: K, f: F) -> Result<&mut V, E>
907 where
908 F: FnOnce() -> Result<V, E>,
909 {
910 if let Some(node) = self.map.get_mut(&KeyRef { k: &k }) {
911 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
912
913 self.detach(node_ptr);
914 self.attach(node_ptr);
915
916 unsafe { Ok(&mut *(*node_ptr).val.as_mut_ptr()) }
917 } else {
918 let v = f()?;
919 let (_, node) = self.replace_or_create_node(k, v);
920 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
921
922 self.attach(node_ptr);
923
924 let keyref = unsafe { (*node_ptr).key.as_ptr() };
925 self.map.insert(KeyRef { k: keyref }, node);
926 unsafe { Ok(&mut *(*node_ptr).val.as_mut_ptr()) }
927 }
928 }
929
930 pub fn try_get_or_insert_mut_ref<'a, Q, F, E>(
962 &'a mut self,
963 k: &'_ Q,
964 f: F,
965 ) -> Result<&'a mut V, E>
966 where
967 K: Borrow<Q>,
968 Q: Hash + Eq + ?Sized + alloc::borrow::ToOwned<Owned = K>,
969 F: FnOnce() -> Result<V, E>,
970 {
971 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
972 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
973
974 self.detach(node_ptr);
975 self.attach(node_ptr);
976
977 unsafe { Ok(&mut *(*node_ptr).val.as_mut_ptr()) }
978 } else {
979 let v = f()?;
980 let (_, node) = self.replace_or_create_node(k.to_owned(), v);
981 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
982
983 self.attach(node_ptr);
984
985 let keyref = unsafe { (*node_ptr).key.as_ptr() };
986 self.map.insert(KeyRef { k: keyref }, node);
987 unsafe { Ok(&mut *(*node_ptr).val.as_mut_ptr()) }
988 }
989 }
990
991 pub fn peek<'a, Q>(&'a self, k: &Q) -> Option<&'a V>
1009 where
1010 K: Borrow<Q>,
1011 Q: Hash + Eq + ?Sized,
1012 {
1013 self.map
1014 .get(KeyWrapper::from_ref(k))
1015 .map(|node| unsafe { &*node.as_ref().val.as_ptr() })
1016 }
1017
1018 pub fn peek_mut<'a, Q>(&'a mut self, k: &Q) -> Option<&'a mut V>
1036 where
1037 K: Borrow<Q>,
1038 Q: Hash + Eq + ?Sized,
1039 {
1040 match self.map.get_mut(KeyWrapper::from_ref(k)) {
1041 None => None,
1042 Some(node) => Some(unsafe { &mut *(*node.as_ptr()).val.as_mut_ptr() }),
1043 }
1044 }
1045
1046 pub fn peek_lru(&self) -> Option<(&K, &V)> {
1063 if self.is_empty() {
1064 return None;
1065 }
1066
1067 let (key, val);
1068 unsafe {
1069 let node = (*self.tail).prev;
1070 key = &(*(*node).key.as_ptr()) as &K;
1071 val = &(*(*node).val.as_ptr()) as &V;
1072 }
1073
1074 Some((key, val))
1075 }
1076
1077 pub fn peek_mru(&self) -> Option<(&K, &V)> {
1094 if self.is_empty() {
1095 return None;
1096 }
1097
1098 let (key, val);
1099 unsafe {
1100 let node: *mut LruEntry<K, V> = (*self.head).next;
1101 key = &(*(*node).key.as_ptr()) as &K;
1102 val = &(*(*node).val.as_ptr()) as &V;
1103 }
1104
1105 Some((key, val))
1106 }
1107
1108 pub fn contains<Q>(&self, k: &Q) -> bool
1127 where
1128 K: Borrow<Q>,
1129 Q: Hash + Eq + ?Sized,
1130 {
1131 self.map.contains_key(KeyWrapper::from_ref(k))
1132 }
1133
1134 pub fn pop<Q>(&mut self, k: &Q) -> Option<V>
1152 where
1153 K: Borrow<Q>,
1154 Q: Hash + Eq + ?Sized,
1155 {
1156 match self.map.remove(KeyWrapper::from_ref(k)) {
1157 None => None,
1158 Some(old_node) => {
1159 let mut old_node = unsafe {
1160 let mut old_node = *Box::from_raw(old_node.as_ptr());
1161 ptr::drop_in_place(old_node.key.as_mut_ptr());
1162
1163 old_node
1164 };
1165
1166 self.detach(&mut old_node);
1167
1168 let LruEntry { key: _, val, .. } = old_node;
1169 unsafe { Some(val.assume_init()) }
1170 }
1171 }
1172 }
1173
1174 pub fn pop_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
1194 where
1195 K: Borrow<Q>,
1196 Q: Hash + Eq + ?Sized,
1197 {
1198 match self.map.remove(KeyWrapper::from_ref(k)) {
1199 None => None,
1200 Some(old_node) => {
1201 let mut old_node = unsafe { *Box::from_raw(old_node.as_ptr()) };
1202
1203 self.detach(&mut old_node);
1204
1205 let LruEntry { key, val, .. } = old_node;
1206 unsafe { Some((key.assume_init(), val.assume_init())) }
1207 }
1208 }
1209 }
1210
1211 pub fn pop_lru(&mut self) -> Option<(K, V)> {
1232 let node = self.remove_last()?;
1233 let node = *node;
1235 let LruEntry { key, val, .. } = node;
1236 unsafe { Some((key.assume_init(), val.assume_init())) }
1237 }
1238
1239 pub fn pop_mru(&mut self) -> Option<(K, V)> {
1260 let node = self.remove_first()?;
1261 let node = *node;
1263 let LruEntry { key, val, .. } = node;
1264 unsafe { Some((key.assume_init(), val.assume_init())) }
1265 }
1266
1267 pub fn promote<Q>(&mut self, k: &Q) -> bool
1294 where
1295 K: Borrow<Q>,
1296 Q: Hash + Eq + ?Sized,
1297 {
1298 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
1299 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
1300 self.detach(node_ptr);
1301 self.attach(node_ptr);
1302 true
1303 } else {
1304 false
1305 }
1306 }
1307
1308 pub fn demote<Q>(&mut self, k: &Q) -> bool
1337 where
1338 K: Borrow<Q>,
1339 Q: Hash + Eq + ?Sized,
1340 {
1341 if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
1342 let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
1343 self.detach(node_ptr);
1344 self.attach_last(node_ptr);
1345 true
1346 } else {
1347 false
1348 }
1349 }
1350
1351 pub fn len(&self) -> usize {
1371 self.map.len()
1372 }
1373
1374 pub fn is_empty(&self) -> bool {
1388 self.map.len() == 0
1389 }
1390
1391 pub fn cap(&self) -> NonZeroUsize {
1402 self.cap
1403 }
1404
1405 pub fn resize(&mut self, cap: NonZeroUsize) {
1428 if cap == self.cap {
1430 return;
1431 }
1432
1433 while self.map.len() > cap.get() {
1434 self.pop_lru();
1435 }
1436 self.map.shrink_to_fit();
1437
1438 self.cap = cap;
1439 }
1440
1441 pub fn clear(&mut self) {
1461 while self.pop_lru().is_some() {}
1462 }
1463
1464 pub fn iter(&self) -> Iter<'_, K, V> {
1483 Iter {
1484 len: self.len(),
1485 ptr: unsafe { (*self.head).next },
1486 end: unsafe { (*self.tail).prev },
1487 phantom: PhantomData,
1488 }
1489 }
1490
1491 pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
1519 IterMut {
1520 len: self.len(),
1521 ptr: unsafe { (*self.head).next },
1522 end: unsafe { (*self.tail).prev },
1523 phantom: PhantomData,
1524 }
1525 }
1526
1527 fn remove_first(&mut self) -> Option<Box<LruEntry<K, V>>> {
1528 let next;
1529 unsafe { next = (*self.head).next }
1530 if !core::ptr::eq(next, self.tail) {
1531 let old_key = KeyRef {
1532 k: unsafe { &(*(*(*self.head).next).key.as_ptr()) },
1533 };
1534 let old_node = self.map.remove(&old_key).unwrap();
1535 let node_ptr: *mut LruEntry<K, V> = old_node.as_ptr();
1536 self.detach(node_ptr);
1537 unsafe { Some(Box::from_raw(node_ptr)) }
1538 } else {
1539 None
1540 }
1541 }
1542
1543 fn remove_last(&mut self) -> Option<Box<LruEntry<K, V>>> {
1544 let prev;
1545 unsafe { prev = (*self.tail).prev }
1546 if !core::ptr::eq(prev, self.head) {
1547 let old_key = KeyRef {
1548 k: unsafe { &(*(*(*self.tail).prev).key.as_ptr()) },
1549 };
1550 let old_node = self.map.remove(&old_key).unwrap();
1551 let node_ptr: *mut LruEntry<K, V> = old_node.as_ptr();
1552 self.detach(node_ptr);
1553 unsafe { Some(Box::from_raw(node_ptr)) }
1554 } else {
1555 None
1556 }
1557 }
1558
1559 fn detach(&mut self, node: *mut LruEntry<K, V>) {
1560 unsafe {
1561 (*(*node).prev).next = (*node).next;
1562 (*(*node).next).prev = (*node).prev;
1563 }
1564 }
1565
1566 fn attach(&mut self, node: *mut LruEntry<K, V>) {
1568 unsafe {
1569 (*node).next = (*self.head).next;
1570 (*node).prev = self.head;
1571 (*self.head).next = node;
1572 (*(*node).next).prev = node;
1573 }
1574 }
1575
1576 fn attach_last(&mut self, node: *mut LruEntry<K, V>) {
1578 unsafe {
1579 (*node).next = self.tail;
1580 (*node).prev = (*self.tail).prev;
1581 (*self.tail).prev = node;
1582 (*(*node).prev).next = node;
1583 }
1584 }
1585}
1586
1587impl<K, V, S> Drop for LruCache<K, V, S> {
1588 fn drop(&mut self) {
1589 self.map.drain().for_each(|(_, node)| unsafe {
1590 let mut node = *Box::from_raw(node.as_ptr());
1591 ptr::drop_in_place((node).key.as_mut_ptr());
1592 ptr::drop_in_place((node).val.as_mut_ptr());
1593 });
1594 let _head = unsafe { *Box::from_raw(self.head) };
1598 let _tail = unsafe { *Box::from_raw(self.tail) };
1599 }
1600}
1601
1602impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a LruCache<K, V, S> {
1603 type Item = (&'a K, &'a V);
1604 type IntoIter = Iter<'a, K, V>;
1605
1606 fn into_iter(self) -> Iter<'a, K, V> {
1607 self.iter()
1608 }
1609}
1610
1611impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a mut LruCache<K, V, S> {
1612 type Item = (&'a K, &'a mut V);
1613 type IntoIter = IterMut<'a, K, V>;
1614
1615 fn into_iter(self) -> IterMut<'a, K, V> {
1616 self.iter_mut()
1617 }
1618}
1619
1620unsafe impl<K: Send, V: Send, S: Send> Send for LruCache<K, V, S> {}
1624unsafe impl<K: Sync, V: Sync, S: Sync> Sync for LruCache<K, V, S> {}
1625
1626impl<K: Hash + Eq, V, S: BuildHasher> fmt::Debug for LruCache<K, V, S> {
1627 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1628 f.debug_struct("LruCache")
1629 .field("len", &self.len())
1630 .field("cap", &self.cap())
1631 .finish()
1632 }
1633}
1634
1635pub struct Iter<'a, K: 'a, V: 'a> {
1643 len: usize,
1644
1645 ptr: *const LruEntry<K, V>,
1646 end: *const LruEntry<K, V>,
1647
1648 phantom: PhantomData<&'a K>,
1649}
1650
1651impl<'a, K, V> Iterator for Iter<'a, K, V> {
1652 type Item = (&'a K, &'a V);
1653
1654 fn next(&mut self) -> Option<(&'a K, &'a V)> {
1655 if self.len == 0 {
1656 return None;
1657 }
1658
1659 let key = unsafe { &(*(*self.ptr).key.as_ptr()) as &K };
1660 let val = unsafe { &(*(*self.ptr).val.as_ptr()) as &V };
1661
1662 self.len -= 1;
1663 self.ptr = unsafe { (*self.ptr).next };
1664
1665 Some((key, val))
1666 }
1667
1668 fn size_hint(&self) -> (usize, Option<usize>) {
1669 (self.len, Some(self.len))
1670 }
1671
1672 fn count(self) -> usize {
1673 self.len
1674 }
1675}
1676
1677impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
1678 fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
1679 if self.len == 0 {
1680 return None;
1681 }
1682
1683 let key = unsafe { &(*(*self.end).key.as_ptr()) as &K };
1684 let val = unsafe { &(*(*self.end).val.as_ptr()) as &V };
1685
1686 self.len -= 1;
1687 self.end = unsafe { (*self.end).prev };
1688
1689 Some((key, val))
1690 }
1691}
1692
1693impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {}
1694impl<'a, K, V> FusedIterator for Iter<'a, K, V> {}
1695
1696impl<'a, K, V> Clone for Iter<'a, K, V> {
1697 fn clone(&self) -> Iter<'a, K, V> {
1698 Iter {
1699 len: self.len,
1700 ptr: self.ptr,
1701 end: self.end,
1702 phantom: PhantomData,
1703 }
1704 }
1705}
1706
1707unsafe impl<'a, K: Send, V: Send> Send for Iter<'a, K, V> {}
1710unsafe impl<'a, K: Sync, V: Sync> Sync for Iter<'a, K, V> {}
1711
1712pub struct IterMut<'a, K: 'a, V: 'a> {
1720 len: usize,
1721
1722 ptr: *mut LruEntry<K, V>,
1723 end: *mut LruEntry<K, V>,
1724
1725 phantom: PhantomData<&'a K>,
1726}
1727
1728impl<'a, K, V> Iterator for IterMut<'a, K, V> {
1729 type Item = (&'a K, &'a mut V);
1730
1731 fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
1732 if self.len == 0 {
1733 return None;
1734 }
1735
1736 let key = unsafe { &(*(*self.ptr).key.as_ptr()) as &K };
1737 let val = unsafe { &mut (*(*self.ptr).val.as_mut_ptr()) as &mut V };
1738
1739 self.len -= 1;
1740 self.ptr = unsafe { (*self.ptr).next };
1741
1742 Some((key, val))
1743 }
1744
1745 fn size_hint(&self) -> (usize, Option<usize>) {
1746 (self.len, Some(self.len))
1747 }
1748
1749 fn count(self) -> usize {
1750 self.len
1751 }
1752}
1753
1754impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
1755 fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> {
1756 if self.len == 0 {
1757 return None;
1758 }
1759
1760 let key = unsafe { &(*(*self.end).key.as_ptr()) as &K };
1761 let val = unsafe { &mut (*(*self.end).val.as_mut_ptr()) as &mut V };
1762
1763 self.len -= 1;
1764 self.end = unsafe { (*self.end).prev };
1765
1766 Some((key, val))
1767 }
1768}
1769
1770impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {}
1771impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
1772
1773unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}
1776unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {}
1777
1778pub struct IntoIter<K, V>
1786where
1787 K: Hash + Eq,
1788{
1789 cache: LruCache<K, V>,
1790}
1791
1792impl<K, V> Iterator for IntoIter<K, V>
1793where
1794 K: Hash + Eq,
1795{
1796 type Item = (K, V);
1797
1798 fn next(&mut self) -> Option<(K, V)> {
1799 self.cache.pop_lru()
1800 }
1801
1802 fn size_hint(&self) -> (usize, Option<usize>) {
1803 let len = self.cache.len();
1804 (len, Some(len))
1805 }
1806
1807 fn count(self) -> usize {
1808 self.cache.len()
1809 }
1810}
1811
1812impl<K, V> ExactSizeIterator for IntoIter<K, V> where K: Hash + Eq {}
1813impl<K, V> FusedIterator for IntoIter<K, V> where K: Hash + Eq {}
1814
1815impl<K: Hash + Eq, V> IntoIterator for LruCache<K, V> {
1816 type Item = (K, V);
1817 type IntoIter = IntoIter<K, V>;
1818
1819 fn into_iter(self) -> IntoIter<K, V> {
1820 IntoIter { cache: self }
1821 }
1822}
1823
1824#[cfg(test)]
1825mod tests {
1826 use super::LruCache;
1827 use core::{fmt::Debug, num::NonZeroUsize};
1828 use scoped_threadpool::Pool;
1829 use std::rc::Rc;
1830 use std::sync::atomic::{AtomicUsize, Ordering};
1831
1832 fn assert_opt_eq<V: PartialEq + Debug>(opt: Option<&V>, v: V) {
1833 assert!(opt.is_some());
1834 assert_eq!(opt.unwrap(), &v);
1835 }
1836
1837 fn assert_opt_eq_mut<V: PartialEq + Debug>(opt: Option<&mut V>, v: V) {
1838 assert!(opt.is_some());
1839 assert_eq!(opt.unwrap(), &v);
1840 }
1841
1842 fn assert_opt_eq_tuple<K: PartialEq + Debug, V: PartialEq + Debug>(
1843 opt: Option<(&K, &V)>,
1844 kv: (K, V),
1845 ) {
1846 assert!(opt.is_some());
1847 let res = opt.unwrap();
1848 assert_eq!(res.0, &kv.0);
1849 assert_eq!(res.1, &kv.1);
1850 }
1851
1852 fn assert_opt_eq_mut_tuple<K: PartialEq + Debug, V: PartialEq + Debug>(
1853 opt: Option<(&K, &mut V)>,
1854 kv: (K, V),
1855 ) {
1856 assert!(opt.is_some());
1857 let res = opt.unwrap();
1858 assert_eq!(res.0, &kv.0);
1859 assert_eq!(res.1, &kv.1);
1860 }
1861
1862 #[test]
1863 fn test_unbounded() {
1864 let mut cache = LruCache::unbounded();
1865 for i in 0..13370 {
1866 cache.put(i, ());
1867 }
1868 assert_eq!(cache.len(), 13370);
1869 }
1870
1871 #[test]
1872 #[cfg(feature = "hashbrown")]
1873 fn test_with_hasher() {
1874 use core::num::NonZeroUsize;
1875
1876 use hashbrown::DefaultHashBuilder;
1877
1878 let s = DefaultHashBuilder::default();
1879 let mut cache = LruCache::with_hasher(NonZeroUsize::new(16).unwrap(), s);
1880
1881 for i in 0..13370 {
1882 cache.put(i, ());
1883 }
1884 assert_eq!(cache.len(), 16);
1885 }
1886
1887 #[test]
1888 fn test_put_and_get() {
1889 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
1890 assert!(cache.is_empty());
1891
1892 assert_eq!(cache.put("apple", "red"), None);
1893 assert_eq!(cache.put("banana", "yellow"), None);
1894
1895 assert_eq!(cache.cap().get(), 2);
1896 assert_eq!(cache.len(), 2);
1897 assert!(!cache.is_empty());
1898 assert_opt_eq(cache.get(&"apple"), "red");
1899 assert_opt_eq(cache.get(&"banana"), "yellow");
1900 }
1901
1902 #[test]
1903 fn test_put_and_get_or_insert() {
1904 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
1905 assert!(cache.is_empty());
1906
1907 assert_eq!(cache.put("apple", "red"), None);
1908 assert_eq!(cache.put("banana", "yellow"), None);
1909
1910 assert_eq!(cache.cap().get(), 2);
1911 assert_eq!(cache.len(), 2);
1912 assert!(!cache.is_empty());
1913 assert_eq!(cache.get_or_insert("apple", || "orange"), &"red");
1914 assert_eq!(cache.get_or_insert("banana", || "orange"), &"yellow");
1915 assert_eq!(cache.get_or_insert("lemon", || "orange"), &"orange");
1916 assert_eq!(cache.get_or_insert("lemon", || "red"), &"orange");
1917 }
1918
1919 #[test]
1920 fn test_get_or_insert_ref() {
1921 use alloc::borrow::ToOwned;
1922 use alloc::string::String;
1923
1924 let key1 = Rc::new("1".to_owned());
1925 let key2 = Rc::new("2".to_owned());
1926 let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
1927 assert!(cache.is_empty());
1928 assert_eq!(cache.get_or_insert_ref(&key1, || "One".to_owned()), "One");
1929 assert_eq!(cache.get_or_insert_ref(&key2, || "Two".to_owned()), "Two");
1930 assert_eq!(cache.len(), 2);
1931 assert!(!cache.is_empty());
1932 assert_eq!(
1933 cache.get_or_insert_ref(&key2, || "Not two".to_owned()),
1934 "Two"
1935 );
1936 assert_eq!(
1937 cache.get_or_insert_ref(&key2, || "Again not two".to_owned()),
1938 "Two"
1939 );
1940 assert_eq!(Rc::strong_count(&key1), 2);
1941 assert_eq!(Rc::strong_count(&key2), 2);
1942 }
1943
1944 #[test]
1945 fn test_try_get_or_insert() {
1946 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
1947
1948 assert_eq!(
1949 cache.try_get_or_insert::<_, &str>("apple", || Ok("red")),
1950 Ok(&"red")
1951 );
1952 assert_eq!(
1953 cache.try_get_or_insert::<_, &str>("apple", || Err("failed")),
1954 Ok(&"red")
1955 );
1956 assert_eq!(
1957 cache.try_get_or_insert::<_, &str>("banana", || Ok("orange")),
1958 Ok(&"orange")
1959 );
1960 assert_eq!(
1961 cache.try_get_or_insert::<_, &str>("lemon", || Err("failed")),
1962 Err("failed")
1963 );
1964 assert_eq!(
1965 cache.try_get_or_insert::<_, &str>("banana", || Err("failed")),
1966 Ok(&"orange")
1967 );
1968 }
1969
1970 #[test]
1971 fn test_try_get_or_insert_ref() {
1972 use alloc::borrow::ToOwned;
1973 use alloc::string::String;
1974
1975 let key1 = Rc::new("1".to_owned());
1976 let key2 = Rc::new("2".to_owned());
1977 let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
1978 let f = || -> Result<String, ()> { Err(()) };
1979 let a = || -> Result<String, ()> { Ok("One".to_owned()) };
1980 let b = || -> Result<String, ()> { Ok("Two".to_owned()) };
1981 assert_eq!(cache.try_get_or_insert_ref(&key1, a), Ok(&"One".to_owned()));
1982 assert_eq!(cache.try_get_or_insert_ref(&key2, f), Err(()));
1983 assert_eq!(cache.try_get_or_insert_ref(&key2, b), Ok(&"Two".to_owned()));
1984 assert_eq!(cache.try_get_or_insert_ref(&key2, a), Ok(&"Two".to_owned()));
1985 assert_eq!(cache.len(), 2);
1986 assert_eq!(Rc::strong_count(&key1), 2);
1987 assert_eq!(Rc::strong_count(&key2), 2);
1988 }
1989
1990 #[test]
1991 fn test_put_and_get_or_insert_mut() {
1992 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
1993 assert!(cache.is_empty());
1994
1995 assert_eq!(cache.put("apple", "red"), None);
1996 assert_eq!(cache.put("banana", "yellow"), None);
1997
1998 assert_eq!(cache.cap().get(), 2);
1999 assert_eq!(cache.len(), 2);
2000
2001 let v = cache.get_or_insert_mut("apple", || "orange");
2002 assert_eq!(v, &"red");
2003 *v = "blue";
2004
2005 assert_eq!(cache.get_or_insert_mut("apple", || "orange"), &"blue");
2006 assert_eq!(cache.get_or_insert_mut("banana", || "orange"), &"yellow");
2007 assert_eq!(cache.get_or_insert_mut("lemon", || "orange"), &"orange");
2008 assert_eq!(cache.get_or_insert_mut("lemon", || "red"), &"orange");
2009 }
2010
2011 #[test]
2012 fn test_get_or_insert_mut_ref() {
2013 use alloc::borrow::ToOwned;
2014 use alloc::string::String;
2015
2016 let key1 = Rc::new("1".to_owned());
2017 let key2 = Rc::new("2".to_owned());
2018 let mut cache = LruCache::<Rc<String>, &'static str>::new(NonZeroUsize::new(2).unwrap());
2019 assert_eq!(cache.get_or_insert_mut_ref(&key1, || "One"), &mut "One");
2020 let v = cache.get_or_insert_mut_ref(&key2, || "Two");
2021 *v = "New two";
2022 assert_eq!(cache.get_or_insert_mut_ref(&key2, || "Two"), &mut "New two");
2023 assert_eq!(Rc::strong_count(&key1), 2);
2024 assert_eq!(Rc::strong_count(&key2), 2);
2025 }
2026
2027 #[test]
2028 fn test_try_get_or_insert_mut() {
2029 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2030
2031 cache.put(1, "a");
2032 cache.put(2, "b");
2033 cache.put(2, "c");
2034
2035 let f = || -> Result<&str, &str> { Err("failed") };
2036 let a = || -> Result<&str, &str> { Ok("a") };
2037 let b = || -> Result<&str, &str> { Ok("b") };
2038 if let Ok(v) = cache.try_get_or_insert_mut(2, a) {
2039 *v = "d";
2040 }
2041 assert_eq!(cache.try_get_or_insert_mut(2, a), Ok(&mut "d"));
2042 assert_eq!(cache.try_get_or_insert_mut(3, f), Err("failed"));
2043 assert_eq!(cache.try_get_or_insert_mut(4, b), Ok(&mut "b"));
2044 assert_eq!(cache.try_get_or_insert_mut(4, a), Ok(&mut "b"));
2045 }
2046
2047 #[test]
2048 fn test_try_get_or_insert_mut_ref() {
2049 use alloc::borrow::ToOwned;
2050 use alloc::string::String;
2051
2052 let key1 = Rc::new("1".to_owned());
2053 let key2 = Rc::new("2".to_owned());
2054 let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
2055 let f = || -> Result<String, ()> { Err(()) };
2056 let a = || -> Result<String, ()> { Ok("One".to_owned()) };
2057 let b = || -> Result<String, ()> { Ok("Two".to_owned()) };
2058 assert_eq!(
2059 cache.try_get_or_insert_mut_ref(&key1, a),
2060 Ok(&mut "One".to_owned())
2061 );
2062 assert_eq!(cache.try_get_or_insert_mut_ref(&key2, f), Err(()));
2063 if let Ok(v) = cache.try_get_or_insert_mut_ref(&key2, b) {
2064 assert_eq!(v, &mut "Two");
2065 *v = "New two".to_owned();
2066 }
2067 assert_eq!(
2068 cache.try_get_or_insert_mut_ref(&key2, a),
2069 Ok(&mut "New two".to_owned())
2070 );
2071 assert_eq!(Rc::strong_count(&key1), 2);
2072 assert_eq!(Rc::strong_count(&key2), 2);
2073 }
2074
2075 #[test]
2076 fn test_put_and_get_mut() {
2077 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2078
2079 cache.put("apple", "red");
2080 cache.put("banana", "yellow");
2081
2082 assert_eq!(cache.cap().get(), 2);
2083 assert_eq!(cache.len(), 2);
2084 assert_opt_eq_mut(cache.get_mut(&"apple"), "red");
2085 assert_opt_eq_mut(cache.get_mut(&"banana"), "yellow");
2086 }
2087
2088 #[test]
2089 fn test_get_mut_and_update() {
2090 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2091
2092 cache.put("apple", 1);
2093 cache.put("banana", 3);
2094
2095 {
2096 let v = cache.get_mut(&"apple").unwrap();
2097 *v = 4;
2098 }
2099
2100 assert_eq!(cache.cap().get(), 2);
2101 assert_eq!(cache.len(), 2);
2102 assert_opt_eq_mut(cache.get_mut(&"apple"), 4);
2103 assert_opt_eq_mut(cache.get_mut(&"banana"), 3);
2104 }
2105
2106 #[test]
2107 fn test_put_update() {
2108 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2109
2110 assert_eq!(cache.put("apple", "red"), None);
2111 assert_eq!(cache.put("apple", "green"), Some("red"));
2112
2113 assert_eq!(cache.len(), 1);
2114 assert_opt_eq(cache.get(&"apple"), "green");
2115 }
2116
2117 #[test]
2118 fn test_put_removes_oldest() {
2119 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2120
2121 assert_eq!(cache.put("apple", "red"), None);
2122 assert_eq!(cache.put("banana", "yellow"), None);
2123 assert_eq!(cache.put("pear", "green"), None);
2124
2125 assert!(cache.get(&"apple").is_none());
2126 assert_opt_eq(cache.get(&"banana"), "yellow");
2127 assert_opt_eq(cache.get(&"pear"), "green");
2128
2129 assert_eq!(cache.put("apple", "green"), None);
2132 assert_eq!(cache.put("tomato", "red"), None);
2133
2134 assert!(cache.get(&"pear").is_none());
2135 assert_opt_eq(cache.get(&"apple"), "green");
2136 assert_opt_eq(cache.get(&"tomato"), "red");
2137 }
2138
2139 #[test]
2140 fn test_peek() {
2141 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2142
2143 cache.put("apple", "red");
2144 cache.put("banana", "yellow");
2145
2146 assert_opt_eq(cache.peek(&"banana"), "yellow");
2147 assert_opt_eq(cache.peek(&"apple"), "red");
2148
2149 cache.put("pear", "green");
2150
2151 assert!(cache.peek(&"apple").is_none());
2152 assert_opt_eq(cache.peek(&"banana"), "yellow");
2153 assert_opt_eq(cache.peek(&"pear"), "green");
2154 }
2155
2156 #[test]
2157 fn test_peek_mut() {
2158 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2159
2160 cache.put("apple", "red");
2161 cache.put("banana", "yellow");
2162
2163 assert_opt_eq_mut(cache.peek_mut(&"banana"), "yellow");
2164 assert_opt_eq_mut(cache.peek_mut(&"apple"), "red");
2165 assert!(cache.peek_mut(&"pear").is_none());
2166
2167 cache.put("pear", "green");
2168
2169 assert!(cache.peek_mut(&"apple").is_none());
2170 assert_opt_eq_mut(cache.peek_mut(&"banana"), "yellow");
2171 assert_opt_eq_mut(cache.peek_mut(&"pear"), "green");
2172
2173 {
2174 let v = cache.peek_mut(&"banana").unwrap();
2175 *v = "green";
2176 }
2177
2178 assert_opt_eq_mut(cache.peek_mut(&"banana"), "green");
2179 }
2180
2181 #[test]
2182 fn test_peek_lru() {
2183 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2184
2185 assert!(cache.peek_lru().is_none());
2186
2187 cache.put("apple", "red");
2188 cache.put("banana", "yellow");
2189 assert_opt_eq_tuple(cache.peek_lru(), ("apple", "red"));
2190
2191 cache.get(&"apple");
2192 assert_opt_eq_tuple(cache.peek_lru(), ("banana", "yellow"));
2193
2194 cache.clear();
2195 assert!(cache.peek_lru().is_none());
2196 }
2197
2198 #[test]
2199 fn test_peek_mru() {
2200 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2201
2202 assert!(cache.peek_mru().is_none());
2203
2204 cache.put("apple", "red");
2205 cache.put("banana", "yellow");
2206 assert_opt_eq_tuple(cache.peek_mru(), ("banana", "yellow"));
2207
2208 cache.get(&"apple");
2209 assert_opt_eq_tuple(cache.peek_mru(), ("apple", "red"));
2210
2211 cache.clear();
2212 assert!(cache.peek_mru().is_none());
2213 }
2214
2215 #[test]
2216 fn test_contains() {
2217 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2218
2219 cache.put("apple", "red");
2220 cache.put("banana", "yellow");
2221 cache.put("pear", "green");
2222
2223 assert!(!cache.contains(&"apple"));
2224 assert!(cache.contains(&"banana"));
2225 assert!(cache.contains(&"pear"));
2226 }
2227
2228 #[test]
2229 fn test_pop() {
2230 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2231
2232 cache.put("apple", "red");
2233 cache.put("banana", "yellow");
2234
2235 assert_eq!(cache.len(), 2);
2236 assert_opt_eq(cache.get(&"apple"), "red");
2237 assert_opt_eq(cache.get(&"banana"), "yellow");
2238
2239 let popped = cache.pop(&"apple");
2240 assert!(popped.is_some());
2241 assert_eq!(popped.unwrap(), "red");
2242 assert_eq!(cache.len(), 1);
2243 assert!(cache.get(&"apple").is_none());
2244 assert_opt_eq(cache.get(&"banana"), "yellow");
2245 }
2246
2247 #[test]
2248 fn test_pop_entry() {
2249 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2250 cache.put("apple", "red");
2251 cache.put("banana", "yellow");
2252
2253 assert_eq!(cache.len(), 2);
2254 assert_opt_eq(cache.get(&"apple"), "red");
2255 assert_opt_eq(cache.get(&"banana"), "yellow");
2256
2257 let popped = cache.pop_entry(&"apple");
2258 assert!(popped.is_some());
2259 assert_eq!(popped.unwrap(), ("apple", "red"));
2260 assert_eq!(cache.len(), 1);
2261 assert!(cache.get(&"apple").is_none());
2262 assert_opt_eq(cache.get(&"banana"), "yellow");
2263 }
2264
2265 #[test]
2266 fn test_pop_lru() {
2267 let mut cache = LruCache::new(NonZeroUsize::new(200).unwrap());
2268
2269 for i in 0..75 {
2270 cache.put(i, "A");
2271 }
2272 for i in 0..75 {
2273 cache.put(i + 100, "B");
2274 }
2275 for i in 0..75 {
2276 cache.put(i + 200, "C");
2277 }
2278 assert_eq!(cache.len(), 200);
2279
2280 for i in 0..75 {
2281 assert_opt_eq(cache.get(&(74 - i + 100)), "B");
2282 }
2283 assert_opt_eq(cache.get(&25), "A");
2284
2285 for i in 26..75 {
2286 assert_eq!(cache.pop_lru(), Some((i, "A")));
2287 }
2288 for i in 0..75 {
2289 assert_eq!(cache.pop_lru(), Some((i + 200, "C")));
2290 }
2291 for i in 0..75 {
2292 assert_eq!(cache.pop_lru(), Some((74 - i + 100, "B")));
2293 }
2294 assert_eq!(cache.pop_lru(), Some((25, "A")));
2295 for _ in 0..50 {
2296 assert_eq!(cache.pop_lru(), None);
2297 }
2298 }
2299
2300 #[test]
2301 fn test_pop_mru() {
2302 let mut cache = LruCache::new(NonZeroUsize::new(200).unwrap());
2303
2304 for i in 0..75 {
2305 cache.put(i, "A");
2306 }
2307 for i in 0..75 {
2308 cache.put(i + 100, "B");
2309 }
2310 for i in 0..75 {
2311 cache.put(i + 200, "C");
2312 }
2313 assert_eq!(cache.len(), 200);
2314
2315 for i in 0..75 {
2316 assert_opt_eq(cache.get(&(74 - i + 100)), "B");
2317 }
2318 assert_opt_eq(cache.get(&25), "A");
2319
2320 assert_eq!(cache.pop_mru(), Some((25, "A")));
2321 for i in 0..75 {
2322 assert_eq!(cache.pop_mru(), Some((i + 100, "B")));
2323 }
2324 for i in 0..75 {
2325 assert_eq!(cache.pop_mru(), Some((74 - i + 200, "C")));
2326 }
2327 for i in (26..75).into_iter().rev() {
2328 assert_eq!(cache.pop_mru(), Some((i, "A")));
2329 }
2330 for _ in 0..50 {
2331 assert_eq!(cache.pop_mru(), None);
2332 }
2333 }
2334
2335 #[test]
2336 fn test_clear() {
2337 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2338
2339 cache.put("apple", "red");
2340 cache.put("banana", "yellow");
2341
2342 assert_eq!(cache.len(), 2);
2343 assert_opt_eq(cache.get(&"apple"), "red");
2344 assert_opt_eq(cache.get(&"banana"), "yellow");
2345
2346 cache.clear();
2347 assert_eq!(cache.len(), 0);
2348 }
2349
2350 #[test]
2351 fn test_resize_larger() {
2352 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2353
2354 cache.put(1, "a");
2355 cache.put(2, "b");
2356 cache.resize(NonZeroUsize::new(4).unwrap());
2357 cache.put(3, "c");
2358 cache.put(4, "d");
2359
2360 assert_eq!(cache.len(), 4);
2361 assert_eq!(cache.get(&1), Some(&"a"));
2362 assert_eq!(cache.get(&2), Some(&"b"));
2363 assert_eq!(cache.get(&3), Some(&"c"));
2364 assert_eq!(cache.get(&4), Some(&"d"));
2365 }
2366
2367 #[test]
2368 fn test_resize_smaller() {
2369 let mut cache = LruCache::new(NonZeroUsize::new(4).unwrap());
2370
2371 cache.put(1, "a");
2372 cache.put(2, "b");
2373 cache.put(3, "c");
2374 cache.put(4, "d");
2375
2376 cache.resize(NonZeroUsize::new(2).unwrap());
2377
2378 assert_eq!(cache.len(), 2);
2379 assert!(cache.get(&1).is_none());
2380 assert!(cache.get(&2).is_none());
2381 assert_eq!(cache.get(&3), Some(&"c"));
2382 assert_eq!(cache.get(&4), Some(&"d"));
2383 }
2384
2385 #[test]
2386 fn test_send() {
2387 use std::thread;
2388
2389 let mut cache = LruCache::new(NonZeroUsize::new(4).unwrap());
2390 cache.put(1, "a");
2391
2392 let handle = thread::spawn(move || {
2393 assert_eq!(cache.get(&1), Some(&"a"));
2394 });
2395
2396 assert!(handle.join().is_ok());
2397 }
2398
2399 #[test]
2400 fn test_multiple_threads() {
2401 let mut pool = Pool::new(1);
2402 let mut cache = LruCache::new(NonZeroUsize::new(4).unwrap());
2403 cache.put(1, "a");
2404
2405 let cache_ref = &cache;
2406 pool.scoped(|scoped| {
2407 scoped.execute(move || {
2408 assert_eq!(cache_ref.peek(&1), Some(&"a"));
2409 });
2410 });
2411
2412 assert_eq!((cache_ref).peek(&1), Some(&"a"));
2413 }
2414
2415 #[test]
2416 fn test_iter_forwards() {
2417 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2418 cache.put("a", 1);
2419 cache.put("b", 2);
2420 cache.put("c", 3);
2421
2422 {
2423 let mut iter = cache.iter();
2425 assert_eq!(iter.len(), 3);
2426 assert_opt_eq_tuple(iter.next(), ("c", 3));
2427
2428 assert_eq!(iter.len(), 2);
2429 assert_opt_eq_tuple(iter.next(), ("b", 2));
2430
2431 assert_eq!(iter.len(), 1);
2432 assert_opt_eq_tuple(iter.next(), ("a", 1));
2433
2434 assert_eq!(iter.len(), 0);
2435 assert_eq!(iter.next(), None);
2436 }
2437 {
2438 let mut iter = cache.iter_mut();
2440 assert_eq!(iter.len(), 3);
2441 assert_opt_eq_mut_tuple(iter.next(), ("c", 3));
2442
2443 assert_eq!(iter.len(), 2);
2444 assert_opt_eq_mut_tuple(iter.next(), ("b", 2));
2445
2446 assert_eq!(iter.len(), 1);
2447 assert_opt_eq_mut_tuple(iter.next(), ("a", 1));
2448
2449 assert_eq!(iter.len(), 0);
2450 assert_eq!(iter.next(), None);
2451 }
2452 }
2453
2454 #[test]
2455 fn test_iter_backwards() {
2456 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2457 cache.put("a", 1);
2458 cache.put("b", 2);
2459 cache.put("c", 3);
2460
2461 {
2462 let mut iter = cache.iter();
2464 assert_eq!(iter.len(), 3);
2465 assert_opt_eq_tuple(iter.next_back(), ("a", 1));
2466
2467 assert_eq!(iter.len(), 2);
2468 assert_opt_eq_tuple(iter.next_back(), ("b", 2));
2469
2470 assert_eq!(iter.len(), 1);
2471 assert_opt_eq_tuple(iter.next_back(), ("c", 3));
2472
2473 assert_eq!(iter.len(), 0);
2474 assert_eq!(iter.next_back(), None);
2475 }
2476
2477 {
2478 let mut iter = cache.iter_mut();
2480 assert_eq!(iter.len(), 3);
2481 assert_opt_eq_mut_tuple(iter.next_back(), ("a", 1));
2482
2483 assert_eq!(iter.len(), 2);
2484 assert_opt_eq_mut_tuple(iter.next_back(), ("b", 2));
2485
2486 assert_eq!(iter.len(), 1);
2487 assert_opt_eq_mut_tuple(iter.next_back(), ("c", 3));
2488
2489 assert_eq!(iter.len(), 0);
2490 assert_eq!(iter.next_back(), None);
2491 }
2492 }
2493
2494 #[test]
2495 fn test_iter_forwards_and_backwards() {
2496 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2497 cache.put("a", 1);
2498 cache.put("b", 2);
2499 cache.put("c", 3);
2500
2501 {
2502 let mut iter = cache.iter();
2504 assert_eq!(iter.len(), 3);
2505 assert_opt_eq_tuple(iter.next(), ("c", 3));
2506
2507 assert_eq!(iter.len(), 2);
2508 assert_opt_eq_tuple(iter.next_back(), ("a", 1));
2509
2510 assert_eq!(iter.len(), 1);
2511 assert_opt_eq_tuple(iter.next(), ("b", 2));
2512
2513 assert_eq!(iter.len(), 0);
2514 assert_eq!(iter.next_back(), None);
2515 }
2516 {
2517 let mut iter = cache.iter_mut();
2519 assert_eq!(iter.len(), 3);
2520 assert_opt_eq_mut_tuple(iter.next(), ("c", 3));
2521
2522 assert_eq!(iter.len(), 2);
2523 assert_opt_eq_mut_tuple(iter.next_back(), ("a", 1));
2524
2525 assert_eq!(iter.len(), 1);
2526 assert_opt_eq_mut_tuple(iter.next(), ("b", 2));
2527
2528 assert_eq!(iter.len(), 0);
2529 assert_eq!(iter.next_back(), None);
2530 }
2531 }
2532
2533 #[test]
2534 fn test_iter_multiple_threads() {
2535 let mut pool = Pool::new(1);
2536 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2537 cache.put("a", 1);
2538 cache.put("b", 2);
2539 cache.put("c", 3);
2540
2541 let mut iter = cache.iter();
2542 assert_eq!(iter.len(), 3);
2543 assert_opt_eq_tuple(iter.next(), ("c", 3));
2544
2545 {
2546 let iter_ref = &mut iter;
2547 pool.scoped(|scoped| {
2548 scoped.execute(move || {
2549 assert_eq!(iter_ref.len(), 2);
2550 assert_opt_eq_tuple(iter_ref.next(), ("b", 2));
2551 });
2552 });
2553 }
2554
2555 assert_eq!(iter.len(), 1);
2556 assert_opt_eq_tuple(iter.next(), ("a", 1));
2557
2558 assert_eq!(iter.len(), 0);
2559 assert_eq!(iter.next(), None);
2560 }
2561
2562 #[test]
2563 fn test_iter_clone() {
2564 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2565 cache.put("a", 1);
2566 cache.put("b", 2);
2567
2568 let mut iter = cache.iter();
2569 let mut iter_clone = iter.clone();
2570
2571 assert_eq!(iter.len(), 2);
2572 assert_opt_eq_tuple(iter.next(), ("b", 2));
2573 assert_eq!(iter_clone.len(), 2);
2574 assert_opt_eq_tuple(iter_clone.next(), ("b", 2));
2575
2576 assert_eq!(iter.len(), 1);
2577 assert_opt_eq_tuple(iter.next(), ("a", 1));
2578 assert_eq!(iter_clone.len(), 1);
2579 assert_opt_eq_tuple(iter_clone.next(), ("a", 1));
2580
2581 assert_eq!(iter.len(), 0);
2582 assert_eq!(iter.next(), None);
2583 assert_eq!(iter_clone.len(), 0);
2584 assert_eq!(iter_clone.next(), None);
2585 }
2586
2587 #[test]
2588 fn test_into_iter() {
2589 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2590 cache.put("a", 1);
2591 cache.put("b", 2);
2592 cache.put("c", 3);
2593
2594 let mut iter = cache.into_iter();
2595 assert_eq!(iter.len(), 3);
2596 assert_eq!(iter.next(), Some(("a", 1)));
2597
2598 assert_eq!(iter.len(), 2);
2599 assert_eq!(iter.next(), Some(("b", 2)));
2600
2601 assert_eq!(iter.len(), 1);
2602 assert_eq!(iter.next(), Some(("c", 3)));
2603
2604 assert_eq!(iter.len(), 0);
2605 assert_eq!(iter.next(), None);
2606 }
2607
2608 #[test]
2609 fn test_that_pop_actually_detaches_node() {
2610 let mut cache = LruCache::new(NonZeroUsize::new(5).unwrap());
2611
2612 cache.put("a", 1);
2613 cache.put("b", 2);
2614 cache.put("c", 3);
2615 cache.put("d", 4);
2616 cache.put("e", 5);
2617
2618 assert_eq!(cache.pop(&"c"), Some(3));
2619
2620 cache.put("f", 6);
2621
2622 let mut iter = cache.iter();
2623 assert_opt_eq_tuple(iter.next(), ("f", 6));
2624 assert_opt_eq_tuple(iter.next(), ("e", 5));
2625 assert_opt_eq_tuple(iter.next(), ("d", 4));
2626 assert_opt_eq_tuple(iter.next(), ("b", 2));
2627 assert_opt_eq_tuple(iter.next(), ("a", 1));
2628 assert!(iter.next().is_none());
2629 }
2630
2631 #[test]
2632 fn test_get_with_borrow() {
2633 use alloc::string::String;
2634
2635 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2636
2637 let key = String::from("apple");
2638 cache.put(key, "red");
2639
2640 assert_opt_eq(cache.get("apple"), "red");
2641 }
2642
2643 #[test]
2644 fn test_get_mut_with_borrow() {
2645 use alloc::string::String;
2646
2647 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2648
2649 let key = String::from("apple");
2650 cache.put(key, "red");
2651
2652 assert_opt_eq_mut(cache.get_mut("apple"), "red");
2653 }
2654
2655 #[test]
2656 fn test_no_memory_leaks() {
2657 static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
2658
2659 struct DropCounter;
2660
2661 impl Drop for DropCounter {
2662 fn drop(&mut self) {
2663 DROP_COUNT.fetch_add(1, Ordering::SeqCst);
2664 }
2665 }
2666
2667 let n = 100;
2668 for _ in 0..n {
2669 let mut cache = LruCache::new(NonZeroUsize::new(1).unwrap());
2670 for i in 0..n {
2671 cache.put(i, DropCounter {});
2672 }
2673 }
2674 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), n * n);
2675 }
2676
2677 #[test]
2678 fn test_no_memory_leaks_with_clear() {
2679 static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
2680
2681 struct DropCounter;
2682
2683 impl Drop for DropCounter {
2684 fn drop(&mut self) {
2685 DROP_COUNT.fetch_add(1, Ordering::SeqCst);
2686 }
2687 }
2688
2689 let n = 100;
2690 for _ in 0..n {
2691 let mut cache = LruCache::new(NonZeroUsize::new(1).unwrap());
2692 for i in 0..n {
2693 cache.put(i, DropCounter {});
2694 }
2695 cache.clear();
2696 }
2697 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), n * n);
2698 }
2699
2700 #[test]
2701 fn test_no_memory_leaks_with_resize() {
2702 static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
2703
2704 struct DropCounter;
2705
2706 impl Drop for DropCounter {
2707 fn drop(&mut self) {
2708 DROP_COUNT.fetch_add(1, Ordering::SeqCst);
2709 }
2710 }
2711
2712 let n = 100;
2713 for _ in 0..n {
2714 let mut cache = LruCache::new(NonZeroUsize::new(1).unwrap());
2715 for i in 0..n {
2716 cache.put(i, DropCounter {});
2717 }
2718 cache.clear();
2719 }
2720 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), n * n);
2721 }
2722
2723 #[test]
2724 fn test_no_memory_leaks_with_pop() {
2725 static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
2726
2727 #[derive(Hash, Eq)]
2728 struct KeyDropCounter(usize);
2729
2730 impl PartialEq for KeyDropCounter {
2731 fn eq(&self, other: &Self) -> bool {
2732 self.0.eq(&other.0)
2733 }
2734 }
2735
2736 impl Drop for KeyDropCounter {
2737 fn drop(&mut self) {
2738 DROP_COUNT.fetch_add(1, Ordering::SeqCst);
2739 }
2740 }
2741
2742 let n = 100;
2743 for _ in 0..n {
2744 let mut cache = LruCache::new(NonZeroUsize::new(1).unwrap());
2745
2746 for i in 0..100 {
2747 cache.put(KeyDropCounter(i), i);
2748 cache.pop(&KeyDropCounter(i));
2749 }
2750 }
2751
2752 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), n * n * 2);
2753 }
2754
2755 #[test]
2756 fn test_promote_and_demote() {
2757 let mut cache = LruCache::new(NonZeroUsize::new(5).unwrap());
2758 for i in 0..5 {
2759 cache.push(i, i);
2760 }
2761 cache.promote(&1);
2762 cache.promote(&0);
2763 cache.demote(&3);
2764 cache.demote(&4);
2765 assert_eq!(cache.pop_lru(), Some((4, 4)));
2766 assert_eq!(cache.pop_lru(), Some((3, 3)));
2767 assert_eq!(cache.pop_lru(), Some((2, 2)));
2768 assert_eq!(cache.pop_lru(), Some((1, 1)));
2769 assert_eq!(cache.pop_lru(), Some((0, 0)));
2770 assert_eq!(cache.pop_lru(), None);
2771 }
2772
2773 #[test]
2774 fn test_get_key_value() {
2775 use alloc::string::String;
2776
2777 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2778
2779 let key = String::from("apple");
2780 cache.put(key, "red");
2781
2782 assert_eq!(
2783 cache.get_key_value("apple"),
2784 Some((&String::from("apple"), &"red"))
2785 );
2786 assert_eq!(cache.get_key_value("banana"), None);
2787 }
2788
2789 #[test]
2790 fn test_get_key_value_mut() {
2791 use alloc::string::String;
2792
2793 let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
2794
2795 let key = String::from("apple");
2796 cache.put(key, "red");
2797
2798 let (k, v) = cache.get_key_value_mut("apple").unwrap();
2799 assert_eq!(k, &String::from("apple"));
2800 assert_eq!(v, &mut "red");
2801 *v = "green";
2802
2803 assert_eq!(
2804 cache.get_key_value("apple"),
2805 Some((&String::from("apple"), &"green"))
2806 );
2807 assert_eq!(cache.get_key_value("banana"), None);
2808 }
2809
2810 #[test]
2811 fn test_clone() {
2812 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap());
2813 cache.put("a", 1);
2814 cache.put("b", 2);
2815 cache.put("c", 3);
2816
2817 let mut cloned = cache.clone();
2818
2819 assert_eq!(cache.pop_lru(), Some(("a", 1)));
2820 assert_eq!(cloned.pop_lru(), Some(("a", 1)));
2821
2822 assert_eq!(cache.pop_lru(), Some(("b", 2)));
2823 assert_eq!(cloned.pop_lru(), Some(("b", 2)));
2824
2825 assert_eq!(cache.pop_lru(), Some(("c", 3)));
2826 assert_eq!(cloned.pop_lru(), Some(("c", 3)));
2827
2828 assert_eq!(cache.pop_lru(), None);
2829 assert_eq!(cloned.pop_lru(), None);
2830 }
2831
2832 #[test]
2833 fn test_clone_unbounded() {
2834 let mut cache = LruCache::unbounded();
2835 cache.put("a", 1);
2836 cache.put("b", 2);
2837 cache.put("c", 3);
2838
2839 let mut cloned = cache.clone();
2840
2841 assert_eq!(cache.pop_lru(), Some(("a", 1)));
2842 assert_eq!(cloned.pop_lru(), Some(("a", 1)));
2843
2844 assert_eq!(cache.pop_lru(), Some(("b", 2)));
2845 assert_eq!(cloned.pop_lru(), Some(("b", 2)));
2846
2847 assert_eq!(cache.pop_lru(), Some(("c", 3)));
2848 assert_eq!(cloned.pop_lru(), Some(("c", 3)));
2849
2850 assert_eq!(cache.pop_lru(), None);
2851 assert_eq!(cloned.pop_lru(), None);
2852 }
2853
2854 #[test]
2855 fn iter_mut_stacked_borrows_violation() {
2856 let mut cache: LruCache<i32, i32> = LruCache::new(NonZeroUsize::new(3).unwrap());
2857 cache.put(1, 10);
2858 cache.put(2, 20);
2859 cache.put(3, 30);
2860
2861 for (_k, v) in cache.iter_mut() {
2862 *v *= 2;
2863 }
2864
2865 assert_eq!(cache.get(&1), Some(&20));
2866 assert_eq!(cache.get(&2), Some(&40));
2867 assert_eq!(cache.get(&3), Some(&60));
2868 }
2869}
2870
2871fn _test_lifetimes() {}