crossbeam_skiplist/map.rs
1//! An ordered map based on a lock-free skip list. See [`SkipMap`].
2
3use std::borrow::Borrow;
4use std::fmt;
5use std::mem::ManuallyDrop;
6use std::ops::{Bound, RangeBounds};
7use std::ptr;
8
9use crate::base::{self, try_pin_loop};
10use crossbeam_epoch as epoch;
11
12/// An ordered map based on a lock-free skip list.
13///
14/// This is an alternative to [`BTreeMap`] which supports
15/// concurrent access across multiple threads.
16///
17/// [`BTreeMap`]: std::collections::BTreeMap
18pub struct SkipMap<K, V> {
19 inner: base::SkipList<K, V>,
20}
21
22impl<K, V> SkipMap<K, V> {
23 /// Returns a new, empty map.
24 ///
25 /// # Example
26 ///
27 /// ```
28 /// use crossbeam_skiplist::SkipMap;
29 ///
30 /// let map: SkipMap<i32, &str> = SkipMap::new();
31 /// ```
32 pub fn new() -> Self {
33 Self {
34 inner: base::SkipList::new(epoch::default_collector().clone()),
35 }
36 }
37
38 /// Returns `true` if the map is empty.
39 ///
40 /// # Example
41 /// ```
42 /// use crossbeam_skiplist::SkipMap;
43 ///
44 /// let map: SkipMap<&str, &str> = SkipMap::new();
45 /// assert!(map.is_empty());
46 ///
47 /// map.insert("key", "value");
48 /// assert!(!map.is_empty());
49 /// ```
50 pub fn is_empty(&self) -> bool {
51 self.inner.is_empty()
52 }
53
54 /// Returns the number of entries in the map.
55 ///
56 /// If the map is being concurrently modified, consider the returned number just an
57 /// approximation without any guarantees.
58 ///
59 /// # Example
60 /// ```
61 /// use crossbeam_skiplist::SkipMap;
62 ///
63 /// let map = SkipMap::new();
64 /// map.insert(0, 1);
65 /// assert_eq!(map.len(), 1);
66 ///
67 /// for x in 1..=5 {
68 /// map.insert(x, x + 1);
69 /// }
70 ///
71 /// assert_eq!(map.len(), 6);
72 /// ```
73 pub fn len(&self) -> usize {
74 self.inner.len()
75 }
76}
77
78impl<K, V> SkipMap<K, V>
79where
80 K: Ord,
81{
82 /// Returns the entry with the smallest key.
83 ///
84 /// This function returns an [`Entry`] which
85 /// can be used to access the key's associated value.
86 ///
87 /// # Example
88 /// ```
89 /// use crossbeam_skiplist::SkipMap;
90 ///
91 /// let numbers = SkipMap::new();
92 /// numbers.insert(5, "five");
93 /// assert_eq!(*numbers.front().unwrap().value(), "five");
94 /// numbers.insert(6, "six");
95 /// assert_eq!(*numbers.front().unwrap().value(), "five");
96 /// ```
97 pub fn front(&self) -> Option<Entry<'_, K, V>> {
98 let guard = &epoch::pin();
99 try_pin_loop(|| self.inner.front(guard)).map(Entry::new)
100 }
101
102 /// Returns the entry with the largest key.
103 ///
104 /// This function returns an [`Entry`] which
105 /// can be used to access the key's associated value.
106 ///
107 /// # Example
108 /// ```
109 /// use crossbeam_skiplist::SkipMap;
110 ///
111 /// let numbers = SkipMap::new();
112 /// numbers.insert(5, "five");
113 /// assert_eq!(*numbers.back().unwrap().value(), "five");
114 /// numbers.insert(6, "six");
115 /// assert_eq!(*numbers.back().unwrap().value(), "six");
116 /// ```
117 pub fn back(&self) -> Option<Entry<'_, K, V>> {
118 let guard = &epoch::pin();
119 try_pin_loop(|| self.inner.back(guard)).map(Entry::new)
120 }
121
122 /// Returns `true` if the map contains a value for the specified key.
123 ///
124 /// # Example
125 /// ```
126 /// use crossbeam_skiplist::SkipMap;
127 ///
128 /// let ages = SkipMap::new();
129 /// ages.insert("Bill Gates", 64);
130 ///
131 /// assert!(ages.contains_key(&"Bill Gates"));
132 /// assert!(!ages.contains_key(&"Steve Jobs"));
133 /// ```
134 pub fn contains_key<Q>(&self, key: &Q) -> bool
135 where
136 K: Borrow<Q>,
137 Q: Ord + ?Sized,
138 {
139 let guard = &epoch::pin();
140 self.inner.contains_key(key, guard)
141 }
142
143 /// Returns an entry with the specified `key`.
144 ///
145 /// This function returns an [`Entry`] which
146 /// can be used to access the key's associated value.
147 ///
148 /// # Example
149 /// ```
150 /// use crossbeam_skiplist::SkipMap;
151 ///
152 /// let numbers: SkipMap<&str, i32> = SkipMap::new();
153 /// assert!(numbers.get("six").is_none());
154 ///
155 /// numbers.insert("six", 6);
156 /// assert_eq!(*numbers.get("six").unwrap().value(), 6);
157 /// ```
158 pub fn get<Q>(&self, key: &Q) -> Option<Entry<'_, K, V>>
159 where
160 K: Borrow<Q>,
161 Q: Ord + ?Sized,
162 {
163 let guard = &epoch::pin();
164 try_pin_loop(|| self.inner.get(key, guard)).map(Entry::new)
165 }
166
167 /// Returns an `Entry` pointing to the lowest element whose key is above
168 /// the given bound. If no such element is found then `None` is
169 /// returned.
170 ///
171 /// This function returns an [`Entry`] which
172 /// can be used to access the key's associated value.
173 ///
174 /// # Example
175 /// ```
176 /// use crossbeam_skiplist::SkipMap;
177 /// use std::ops::Bound::*;
178 ///
179 /// let numbers = SkipMap::new();
180 /// numbers.insert(6, "six");
181 /// numbers.insert(7, "seven");
182 /// numbers.insert(12, "twelve");
183 ///
184 /// let greater_than_five = numbers.lower_bound(Excluded(&5)).unwrap();
185 /// assert_eq!(*greater_than_five.value(), "six");
186 ///
187 /// let greater_than_six = numbers.lower_bound(Excluded(&6)).unwrap();
188 /// assert_eq!(*greater_than_six.value(), "seven");
189 ///
190 /// let greater_than_thirteen = numbers.lower_bound(Excluded(&13));
191 /// assert!(greater_than_thirteen.is_none());
192 /// ```
193 pub fn lower_bound<'a, Q>(&'a self, bound: Bound<&Q>) -> Option<Entry<'a, K, V>>
194 where
195 K: Borrow<Q>,
196 Q: Ord + ?Sized,
197 {
198 let guard = &epoch::pin();
199 try_pin_loop(|| self.inner.lower_bound(bound, guard)).map(Entry::new)
200 }
201
202 /// Returns an `Entry` pointing to the highest element whose key is below
203 /// the given bound. If no such element is found then `None` is
204 /// returned.
205 ///
206 /// This function returns an [`Entry`] which
207 /// can be used to access the key's associated value.
208 ///
209 /// # Example
210 /// ```
211 /// use crossbeam_skiplist::SkipMap;
212 /// use std::ops::Bound::*;
213 ///
214 /// let numbers = SkipMap::new();
215 /// numbers.insert(6, "six");
216 /// numbers.insert(7, "seven");
217 /// numbers.insert(12, "twelve");
218 ///
219 /// let less_than_eight = numbers.upper_bound(Excluded(&8)).unwrap();
220 /// assert_eq!(*less_than_eight.value(), "seven");
221 ///
222 /// let less_than_six = numbers.upper_bound(Excluded(&6));
223 /// assert!(less_than_six.is_none());
224 /// ```
225 pub fn upper_bound<'a, Q>(&'a self, bound: Bound<&Q>) -> Option<Entry<'a, K, V>>
226 where
227 K: Borrow<Q>,
228 Q: Ord + ?Sized,
229 {
230 let guard = &epoch::pin();
231 try_pin_loop(|| self.inner.upper_bound(bound, guard)).map(Entry::new)
232 }
233
234 /// Finds an entry with the specified key, or inserts a new `key`-`value` pair if none exist.
235 ////
236 /// This function returns an [`Entry`] which
237 /// can be used to access the key's associated value.
238 ///
239 /// # Example
240 /// ```
241 /// use crossbeam_skiplist::SkipMap;
242 ///
243 /// let ages = SkipMap::new();
244 /// let gates_age = ages.get_or_insert("Bill Gates", 64);
245 /// assert_eq!(*gates_age.value(), 64);
246 ///
247 /// ages.insert("Steve Jobs", 65);
248 /// let jobs_age = ages.get_or_insert("Steve Jobs", -1);
249 /// assert_eq!(*jobs_age.value(), 65);
250 /// ```
251 pub fn get_or_insert(&self, key: K, value: V) -> Entry<'_, K, V> {
252 let guard = &epoch::pin();
253 Entry::new(self.inner.get_or_insert(key, value, guard))
254 }
255
256 /// Finds an entry with the specified key, or inserts a new `key`-`value` pair if none exist,
257 /// where value is calculated with a function.
258 ///
259 ///
260 /// <b>Note:</b> Another thread may write key value first, leading to the result of this closure
261 /// discarded. If closure is modifying some other state (such as shared counters or shared
262 /// objects), it may lead to <u>undesired behaviour</u> such as counters being changed without
263 /// result of closure inserted
264 ////
265 /// This function returns an [`Entry`] which
266 /// can be used to access the key's associated value.
267 ///
268 ///
269 /// # Example
270 /// ```
271 /// use crossbeam_skiplist::SkipMap;
272 ///
273 /// let ages = SkipMap::new();
274 /// let gates_age = ages.get_or_insert_with("Bill Gates", || 64);
275 /// assert_eq!(*gates_age.value(), 64);
276 ///
277 /// ages.insert("Steve Jobs", 65);
278 /// let jobs_age = ages.get_or_insert_with("Steve Jobs", || -1);
279 /// assert_eq!(*jobs_age.value(), 65);
280 /// ```
281 pub fn get_or_insert_with<F>(&self, key: K, value_fn: F) -> Entry<'_, K, V>
282 where
283 F: FnOnce() -> V,
284 {
285 let guard = &epoch::pin();
286 Entry::new(self.inner.get_or_insert_with(key, value_fn, guard))
287 }
288
289 /// Returns an iterator over all entries in the map,
290 /// sorted by key.
291 ///
292 /// This iterator returns [`Entry`]s which
293 /// can be used to access keys and their associated values.
294 ///
295 /// # Examples
296 /// ```
297 /// use crossbeam_skiplist::SkipMap;
298 ///
299 /// let numbers = SkipMap::new();
300 /// numbers.insert(6, "six");
301 /// numbers.insert(7, "seven");
302 /// numbers.insert(12, "twelve");
303 ///
304 /// // Print then numbers from least to greatest
305 /// for entry in numbers.iter() {
306 /// let number = entry.key();
307 /// let number_str = entry.value();
308 /// println!("{} is {}", number, number_str);
309 /// }
310 /// ```
311 pub fn iter(&self) -> Iter<'_, K, V> {
312 Iter {
313 inner: self.inner.ref_iter(),
314 }
315 }
316
317 /// Returns an iterator over a subset of entries in the map.
318 ///
319 /// This iterator returns [`Entry`]s which
320 /// can be used to access keys and their associated values.
321 ///
322 /// # Example
323 /// ```
324 /// use crossbeam_skiplist::SkipMap;
325 ///
326 /// let numbers = SkipMap::new();
327 /// numbers.insert(6, "six");
328 /// numbers.insert(7, "seven");
329 /// numbers.insert(12, "twelve");
330 ///
331 /// // Print all numbers in the map between 5 and 8.
332 /// for entry in numbers.range(5..=8) {
333 /// let number = entry.key();
334 /// let number_str = entry.value();
335 /// println!("{} is {}", number, number_str);
336 /// }
337 /// ```
338 pub fn range<Q, R>(&self, range: R) -> Range<'_, Q, R, K, V>
339 where
340 K: Borrow<Q>,
341 R: RangeBounds<Q>,
342 Q: Ord + ?Sized,
343 {
344 Range {
345 inner: self.inner.ref_range(range),
346 }
347 }
348}
349
350impl<K, V> SkipMap<K, V>
351where
352 K: Ord + Send + 'static,
353 V: Send + 'static,
354{
355 /// Inserts a `key`-`value` pair into the map and returns the new entry.
356 ///
357 /// If there is an existing entry with this key, it will be removed before inserting the new
358 /// one.
359 ///
360 /// This function returns an [`Entry`] which
361 /// can be used to access the inserted key's associated value.
362 ///
363 /// # Example
364 /// ```
365 /// use crossbeam_skiplist::SkipMap;
366 ///
367 /// let map = SkipMap::new();
368 /// map.insert("key", "value");
369 ///
370 /// assert_eq!(*map.get("key").unwrap().value(), "value");
371 /// ```
372 pub fn insert(&self, key: K, value: V) -> Entry<'_, K, V> {
373 let guard = &epoch::pin();
374 Entry::new(self.inner.insert(key, value, guard))
375 }
376
377 /// Inserts a `key`-`value` pair into the skip list and returns the new entry.
378 ///
379 /// If there is an existing entry with this key and compare(entry.value) returns true,
380 /// it will be removed before inserting the new one.
381 /// The closure will not be called if the key is not present.
382 ///
383 /// This function returns an [`Entry`] which
384 /// can be used to access the inserted key's associated value.
385 ///
386 /// # Example
387 /// ```
388 /// use crossbeam_skiplist::SkipMap;
389 ///
390 /// let map = SkipMap::new();
391 /// map.insert("key", 1);
392 /// map.compare_insert("key", 0, |x| x < &0);
393 /// assert_eq!(*map.get("key").unwrap().value(), 1);
394 /// map.compare_insert("key", 2, |x| x < &2);
395 /// assert_eq!(*map.get("key").unwrap().value(), 2);
396 /// map.compare_insert("absent_key", 0, |_| false);
397 /// assert_eq!(*map.get("absent_key").unwrap().value(), 0);
398 /// ```
399 pub fn compare_insert<F>(&self, key: K, value: V, compare_fn: F) -> Entry<'_, K, V>
400 where
401 F: Fn(&V) -> bool,
402 {
403 let guard = &epoch::pin();
404 Entry::new(self.inner.compare_insert(key, value, compare_fn, guard))
405 }
406
407 /// Removes an entry with the specified `key` from the map and returns it.
408 ///
409 /// The value will not actually be dropped until all references to it have gone
410 /// out of scope.
411 ///
412 /// This function returns an [`Entry`] which
413 /// can be used to access the removed key's associated value.
414 ///
415 /// # Example
416 /// ```
417 /// use crossbeam_skiplist::SkipMap;
418 ///
419 /// let map: SkipMap<&str, &str> = SkipMap::new();
420 /// assert!(map.remove("invalid key").is_none());
421 ///
422 /// map.insert("key", "value");
423 /// assert_eq!(*map.remove("key").unwrap().value(), "value");
424 /// ```
425 pub fn remove<Q>(&self, key: &Q) -> Option<Entry<'_, K, V>>
426 where
427 K: Borrow<Q>,
428 Q: Ord + ?Sized,
429 {
430 let guard = &epoch::pin();
431 self.inner.remove(key, guard).map(Entry::new)
432 }
433
434 /// Removes the entry with the lowest key
435 /// from the map. Returns the removed entry.
436 ///
437 /// The value will not actually be dropped until all references to it have gone
438 /// out of scope.
439 ///
440 /// # Example
441 /// ```
442 /// use crossbeam_skiplist::SkipMap;
443 ///
444 /// let numbers = SkipMap::new();
445 /// numbers.insert(6, "six");
446 /// numbers.insert(7, "seven");
447 /// numbers.insert(12, "twelve");
448 ///
449 /// assert_eq!(*numbers.pop_front().unwrap().value(), "six");
450 /// assert_eq!(*numbers.pop_front().unwrap().value(), "seven");
451 /// assert_eq!(*numbers.pop_front().unwrap().value(), "twelve");
452 ///
453 /// // All entries have been removed now.
454 /// assert!(numbers.is_empty());
455 /// ```
456 pub fn pop_front(&self) -> Option<Entry<'_, K, V>> {
457 let guard = &epoch::pin();
458 self.inner.pop_front(guard).map(Entry::new)
459 }
460
461 /// Removes the entry with the greatest key from the map.
462 /// Returns the removed entry.
463 ///
464 /// The value will not actually be dropped until all references to it have gone
465 /// out of scope.
466 ///
467 /// # Example
468 /// ```
469 /// use crossbeam_skiplist::SkipMap;
470 ///
471 /// let numbers = SkipMap::new();
472 /// numbers.insert(6, "six");
473 /// numbers.insert(7, "seven");
474 /// numbers.insert(12, "twelve");
475 ///
476 /// assert_eq!(*numbers.pop_back().unwrap().value(), "twelve");
477 /// assert_eq!(*numbers.pop_back().unwrap().value(), "seven");
478 /// assert_eq!(*numbers.pop_back().unwrap().value(), "six");
479 ///
480 /// // All entries have been removed now.
481 /// assert!(numbers.is_empty());
482 /// ```
483 pub fn pop_back(&self) -> Option<Entry<'_, K, V>> {
484 let guard = &epoch::pin();
485 self.inner.pop_back(guard).map(Entry::new)
486 }
487
488 /// Removes all entries from the map.
489 ///
490 /// # Example
491 /// ```
492 /// use crossbeam_skiplist::SkipMap;
493 ///
494 /// let people = SkipMap::new();
495 /// people.insert("Bill", "Gates");
496 /// people.insert("Steve", "Jobs");
497 ///
498 /// people.clear();
499 /// assert!(people.is_empty());
500 /// ```
501 pub fn clear(&self) {
502 let guard = &mut epoch::pin();
503 self.inner.clear(guard);
504 }
505}
506
507impl<K, V> Default for SkipMap<K, V> {
508 fn default() -> Self {
509 Self::new()
510 }
511}
512
513impl<K, V> fmt::Debug for SkipMap<K, V>
514where
515 K: Ord + fmt::Debug,
516 V: fmt::Debug,
517{
518 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
519 f.pad("SkipMap { .. }")
520 }
521}
522
523impl<K, V> IntoIterator for SkipMap<K, V> {
524 type Item = (K, V);
525 type IntoIter = IntoIter<K, V>;
526
527 fn into_iter(self) -> IntoIter<K, V> {
528 IntoIter {
529 inner: self.inner.into_iter(),
530 }
531 }
532}
533
534impl<'a, K, V> IntoIterator for &'a SkipMap<K, V>
535where
536 K: Ord,
537{
538 type Item = Entry<'a, K, V>;
539 type IntoIter = Iter<'a, K, V>;
540
541 fn into_iter(self) -> Iter<'a, K, V> {
542 self.iter()
543 }
544}
545
546impl<K, V> FromIterator<(K, V)> for SkipMap<K, V>
547where
548 K: Ord,
549{
550 fn from_iter<I>(iter: I) -> Self
551 where
552 I: IntoIterator<Item = (K, V)>,
553 {
554 let s = Self::new();
555 for (k, v) in iter {
556 s.get_or_insert(k, v);
557 }
558 s
559 }
560}
561
562/// A reference-counted entry in a map.
563pub struct Entry<'a, K, V> {
564 inner: ManuallyDrop<base::RefEntry<'a, K, V>>,
565}
566
567impl<'a, K, V> Entry<'a, K, V> {
568 fn new(inner: base::RefEntry<'a, K, V>) -> Self {
569 Self {
570 inner: ManuallyDrop::new(inner),
571 }
572 }
573
574 /// Returns a reference to the key.
575 pub fn key(&self) -> &K {
576 self.inner.key()
577 }
578
579 /// Returns a reference to the value.
580 pub fn value(&self) -> &V {
581 self.inner.value()
582 }
583
584 /// Returns `true` if the entry is removed from the map.
585 pub fn is_removed(&self) -> bool {
586 self.inner.is_removed()
587 }
588}
589
590impl<K, V> Drop for Entry<'_, K, V> {
591 fn drop(&mut self) {
592 unsafe {
593 ManuallyDrop::into_inner(ptr::read(&self.inner)).release_with_pin(epoch::pin);
594 }
595 }
596}
597
598impl<'a, K, V> Entry<'a, K, V>
599where
600 K: Ord,
601{
602 /// Moves to the next entry in the map.
603 pub fn move_next(&mut self) -> bool {
604 let guard = &epoch::pin();
605 self.inner.move_next(guard)
606 }
607
608 /// Moves to the previous entry in the map.
609 pub fn move_prev(&mut self) -> bool {
610 let guard = &epoch::pin();
611 self.inner.move_prev(guard)
612 }
613
614 /// Returns the next entry in the map.
615 pub fn next(&self) -> Option<Entry<'a, K, V>> {
616 let guard = &epoch::pin();
617 self.inner.next(guard).map(Entry::new)
618 }
619
620 /// Returns the previous entry in the map.
621 pub fn prev(&self) -> Option<Entry<'a, K, V>> {
622 let guard = &epoch::pin();
623 self.inner.prev(guard).map(Entry::new)
624 }
625}
626
627impl<K, V> Entry<'_, K, V>
628where
629 K: Ord + Send + 'static,
630 V: Send + 'static,
631{
632 /// Removes the entry from the map.
633 ///
634 /// Returns `true` if this call removed the entry and `false` if it was already removed.
635 pub fn remove(&self) -> bool {
636 let guard = &epoch::pin();
637 self.inner.remove(guard)
638 }
639}
640
641impl<K, V> Clone for Entry<'_, K, V> {
642 fn clone(&self) -> Self {
643 Self {
644 inner: self.inner.clone(),
645 }
646 }
647}
648
649impl<K, V> fmt::Debug for Entry<'_, K, V>
650where
651 K: fmt::Debug,
652 V: fmt::Debug,
653{
654 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
655 f.debug_tuple("Entry")
656 .field(self.key())
657 .field(self.value())
658 .finish()
659 }
660}
661
662/// An owning iterator over the entries of a `SkipMap`.
663pub struct IntoIter<K, V> {
664 inner: base::IntoIter<K, V>,
665}
666
667impl<K, V> Iterator for IntoIter<K, V> {
668 type Item = (K, V);
669
670 fn next(&mut self) -> Option<(K, V)> {
671 self.inner.next()
672 }
673}
674
675impl<K, V> fmt::Debug for IntoIter<K, V> {
676 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
677 f.pad("IntoIter { .. }")
678 }
679}
680
681/// An iterator over the entries of a `SkipMap`.
682pub struct Iter<'a, K, V> {
683 inner: base::RefIter<'a, K, V>,
684}
685
686impl<'a, K, V> Iterator for Iter<'a, K, V>
687where
688 K: Ord,
689{
690 type Item = Entry<'a, K, V>;
691
692 fn next(&mut self) -> Option<Entry<'a, K, V>> {
693 let guard = &epoch::pin();
694 self.inner.next(guard).map(Entry::new)
695 }
696}
697
698impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V>
699where
700 K: Ord,
701{
702 fn next_back(&mut self) -> Option<Entry<'a, K, V>> {
703 let guard = &epoch::pin();
704 self.inner.next_back(guard).map(Entry::new)
705 }
706}
707
708impl<K, V> fmt::Debug for Iter<'_, K, V> {
709 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
710 f.pad("Iter { .. }")
711 }
712}
713
714impl<K, V> Drop for Iter<'_, K, V> {
715 fn drop(&mut self) {
716 let guard = &epoch::pin();
717 self.inner.drop_impl(guard);
718 }
719}
720
721/// An iterator over a subset of entries of a `SkipMap`.
722pub struct Range<'a, Q, R, K, V>
723where
724 K: Ord + Borrow<Q>,
725 R: RangeBounds<Q>,
726 Q: Ord + ?Sized,
727{
728 pub(crate) inner: base::RefRange<'a, Q, R, K, V>,
729}
730
731impl<'a, Q, R, K, V> Iterator for Range<'a, Q, R, K, V>
732where
733 K: Ord + Borrow<Q>,
734 R: RangeBounds<Q>,
735 Q: Ord + ?Sized,
736{
737 type Item = Entry<'a, K, V>;
738
739 fn next(&mut self) -> Option<Entry<'a, K, V>> {
740 let guard = &epoch::pin();
741 self.inner.next(guard).map(Entry::new)
742 }
743}
744
745impl<'a, Q, R, K, V> DoubleEndedIterator for Range<'a, Q, R, K, V>
746where
747 K: Ord + Borrow<Q>,
748 R: RangeBounds<Q>,
749 Q: Ord + ?Sized,
750{
751 fn next_back(&mut self) -> Option<Entry<'a, K, V>> {
752 let guard = &epoch::pin();
753 self.inner.next_back(guard).map(Entry::new)
754 }
755}
756
757impl<Q, R, K, V> fmt::Debug for Range<'_, Q, R, K, V>
758where
759 K: Ord + Borrow<Q> + fmt::Debug,
760 V: fmt::Debug,
761 R: RangeBounds<Q> + fmt::Debug,
762 Q: Ord + ?Sized,
763{
764 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
765 f.debug_struct("Range")
766 .field("range", &self.inner.range)
767 .field("head", &self.inner.head)
768 .field("tail", &self.inner.tail)
769 .finish()
770 }
771}
772
773impl<Q, R, K, V> Drop for Range<'_, Q, R, K, V>
774where
775 K: Ord + Borrow<Q>,
776 R: RangeBounds<Q>,
777 Q: Ord + ?Sized,
778{
779 fn drop(&mut self) {
780 let guard = &epoch::pin();
781 self.inner.drop_impl(guard);
782 }
783}