use std::num::NonZero;
use crate::{
buffer,
buffer::segments::{self, Chain, Handle},
depot::arena::linked::slots,
};
#[derive(Clone, Copy, Debug)]
pub struct Position {
pub(in crate::buffer) index: slots::Index,
pub(in crate::buffer) start: usize,
}
impl Position {
pub const fn new() -> Self {
Self {
index: slots::Index::NONE,
start: 0,
}
}
pub unsafe fn resume<'a, T: buffer::raw::Segment, H: Handle<T>>(
&mut self,
source: &'a Chain<T, H>,
offset: usize,
) -> segments::Cursor<'a, T> {
unsafe { segments::Cursor::resume(source, self, offset) }
}
pub unsafe fn rebase_prefix(&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>());