Skip to main content

dope_runtime/executor/raw/
cursor.rs

1/// Retained remainder selection for one statically sized sibling group.
2/// Empty and sufficiently funded visits leave the cursor unchanged.
3#[derive(Clone, Copy)]
4pub struct Cursor<const N: usize> {
5    next: usize,
6}
7
8impl<const N: usize> Cursor<N> {
9    pub const INITIAL: Self = {
10        assert!(N != 0);
11        Self { next: 0 }
12    };
13
14    pub(super) fn start(&mut self, left: usize) -> Self {
15        if left == 0 || left >= N {
16            return Self::INITIAL;
17        }
18        let selected = *self;
19        self.next = if self.next + 1 == N { 0 } else { self.next + 1 };
20        selected
21    }
22
23    /// Priority of this declaration position among the unvisited siblings.
24    pub fn rank<const INDEX: usize>(self) -> usize {
25        const { assert!(INDEX < N) };
26        if self.next <= INDEX { 0 } else { N - self.next }
27    }
28}