pub const SPAN_CLASS_COUNT: u32 = 256;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Span {
    pub offset: u32,
    pub length: u32,
}

impl Span {
    #[inline(always)]
    pub const fn new(offset: u32, length: u32) -> Self {
        Self { offset, length }
    }

    #[inline(always)]
    pub const fn empty(self) -> bool {
        self.length == 0
    }

    #[inline(always)]
    pub fn split_after(&mut self, count: u32) -> Self {
        assert!(count <= self.length);
        let rest = Self {
            offset: self.offset + count,
            length: self.length - count,
        };
        self.length = count;
        rest
    }

    #[inline(always)]
    pub const fn span_class(self) -> u32 {
        let length = if self.length > SPAN_CLASS_COUNT {
            SPAN_CLASS_COUNT
        } else {
            self.length
        };
        length - 1
    }

    #[inline(always)]
    pub fn byte_length_for_page_size(self, page_size: usize) -> usize {
        self.length as usize * page_size
    }
}
