seekable_iterator/
seekable.rs

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