o3 0.62.0

shared-nothing primitives
Documentation
use std::num::NonZero;

use crate::depot::arena::linked;

/// A movable traversal checkpoint containing no borrowed addresses.
/// Prefix removal requires rebasing before the same source is resumed.
#[derive(Clone, Copy, Debug)]
pub struct Position {
    pub(in crate::buffer) index: linked::raw::Index,
    pub(in crate::buffer) range: u32,
    pub(in crate::buffer) start: usize,
}

impl Position {
    pub const fn new() -> Self {
        Self {
            index: linked::raw::Index::NONE,
            range: u32::MAX,
            start: 0,
        }
    }

    /// # Safety
    /// This checkpoint follows exactly the removed whole prefix of the same live source.
    /// Surviving node, link, range and byte identities remain unchanged.
    pub unsafe fn rebase(&mut self, removed: NonZero<usize>) {
        match self.start.checked_sub(removed.get()) {
            Some(start) => self.start = start,
            None => *self = Self::new(),
        }
    }
}

impl Default for Position {
    fn default() -> Self {
        Self::new()
    }
}

const _: () = assert!(size_of::<Position>() == 2 * size_of::<usize>());