seekable_iterator/
seekable.rs

1use core::borrow::Borrow;
2
3use crate::comparator::Comparator;
4
5
6/// A trait adding seek functionality to one of the cursor iterator traits.
7///
8/// See [`CursorIterator`], [`CursorLendingIterator`], or [`CursorPooledIterator`] for more.
9///
10/// [`CursorIterator`]: crate::cursor::CursorIterator
11/// [`CursorLendingIterator`]: crate::cursor::CursorLendingIterator
12/// [`CursorPooledIterator`]: crate::cursor::CursorPooledIterator
13pub trait Seekable<Key: ?Sized, Cmp: ?Sized + Comparator<Key>> {
14    /// Reset the iterator to its initial position, before the first entry and after the last
15    /// entry (if there are any entries in the collection).
16    ///
17    /// The iterator will then not be `!valid()`.
18    fn reset(&mut self);
19
20    /// Move the iterator to the smallest key which is greater or equal than the provided
21    /// `min_bound`.
22    ///
23    /// If there is no such key, the iterator becomes `!valid()`, and is conceptually
24    /// one position before the first entry and one position after the last entry (if there are
25    /// any entries in the collection).
26    fn seek<K>(&mut self, min_bound: K) where Key: Borrow<K>;
27
28    /// Move the iterator to the greatest key which is strictly less than the provided
29    /// `strict_upper_bound`.
30    ///
31    /// If there is no such key, the iterator becomes `!valid()`, and is conceptually
32    /// one position before the first entry and one position after the last entry (if there are
33    /// any entries in the collection).
34    fn seek_before<K>(&mut self, strict_upper_bound: K) where Key: Borrow<K>;
35
36    /// Move the iterator to the smallest key in the collection.
37    ///
38    /// If the collection is empty, the iterator is `!valid()`.
39    fn seek_to_first(&mut self);
40
41    /// Move the iterator to the greatest key in the collection.
42    ///
43    /// If the collection is empty, the iterator is `!valid()`.
44    fn seek_to_last(&mut self);
45}
46
47#[cfg(any(feature = "lender", feature = "lending-iterator"))]
48macro_rules! delegate_seekable {
49    ($struct_name:ident.$field:tt $($extra_i_bounds:tt)*) => {
50        impl<Key, Cmp, I> Seekable<Key, Cmp> for $struct_name<I>
51        where
52            Key: ?Sized,
53            Cmp: ?Sized + Comparator<Key>,
54            I:   Seekable<Key, Cmp> + $($extra_i_bounds)*,
55        {
56            #[inline]
57            fn reset(&mut self) {
58                self.$field.reset();
59            }
60
61            #[inline]
62            fn seek<K>(&mut self, min_bound: K) where Key: Borrow<K> {
63                self.$field.seek(min_bound);
64            }
65
66            #[inline]
67            fn seek_before<K>(&mut self, strict_upper_bound: K) where Key: Borrow<K> {
68                self.$field.seek_before(strict_upper_bound);
69            }
70
71            #[inline]
72            fn seek_to_first(&mut self) {
73                self.$field.seek_to_first();
74            }
75
76            #[inline]
77            fn seek_to_last(&mut self) {
78                self.$field.seek_to_last();
79            }
80        }
81    };
82}
83
84#[cfg(any(feature = "lender", feature = "lending-iterator"))]
85pub(crate) use delegate_seekable;