reth_trie/
cursor_position.rs1use std::{fmt::Debug, ops::Bound};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub(crate) enum CursorPosition<K> {
8 Unseeked,
10 Positioned { key: K, lower_bound: Bound<K> },
12 NoCurrent { lower_bound: Bound<K> },
14}
15
16impl<K: Copy + Ord + Debug> CursorPosition<K> {
17 pub(crate) const fn key(&self) -> Option<K> {
19 match self {
20 Self::Positioned { key, .. } => Some(*key),
21 Self::Unseeked | Self::NoCurrent { .. } => None,
22 }
23 }
24
25 const fn lower_bound(&self) -> Bound<K> {
27 match self {
28 Self::Unseeked => Bound::Unbounded,
29 Self::Positioned { lower_bound, .. } | Self::NoCurrent { lower_bound } => *lower_bound,
30 }
31 }
32
33 pub(crate) const fn set_lower_bound(&mut self, lower_bound: Bound<K>) {
35 *self = match self.key() {
36 Some(key) => Self::Positioned { key, lower_bound },
37 None => Self::NoCurrent { lower_bound },
38 };
39 }
40
41 pub(crate) fn set_key(&mut self, key: Option<K>) {
43 debug_assert!(
44 self.key().is_none_or(|last| key.is_none_or(|next| next >= last)),
45 "Cannot return entry {:?} previous to the last returned entry at {:?}",
46 key,
47 self.key(),
48 );
49 let lower_bound = self.lower_bound();
50 *self = match key {
51 Some(key) => Self::Positioned { key, lower_bound },
52 None => Self::NoCurrent { lower_bound },
53 };
54 }
55
56 pub(crate) fn must_rewind(&self, key: K) -> bool {
58 match self.lower_bound() {
59 Bound::Included(lower) => key < lower,
60 Bound::Excluded(lower) => key <= lower,
61 Bound::Unbounded => false,
62 }
63 }
64}