zstd-complete 0.1.0

A pure Rust Zstandard compressor and decompressor with bounded streaming I/O
Documentation
use alloc::boxed::Box;
use core::ops::Index;

const ZSTD_OPT_NUM: usize = 1 << 12;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::encoding::levels::c_port) struct OptMatch {
    pub(in crate::encoding::levels::c_port) off_base: u32,
    pub(in crate::encoding::levels::c_port) len: u32,
}

impl OptMatch {
    const EMPTY: Self = Self {
        off_base: 0,
        len: 0,
    };
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::encoding::levels::c_port) struct OptMatchTable {
    entries: Box<[OptMatch; ZSTD_OPT_NUM]>,
    len: usize,
}

impl OptMatchTable {
    pub(in crate::encoding::levels::c_port) fn new() -> Self {
        Self {
            entries: Box::new([OptMatch::EMPTY; ZSTD_OPT_NUM]),
            len: 0,
        }
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn clear(&mut self) {
        self.len = 0;
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn len(&self) -> usize {
        self.len
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn is_empty(&self) -> bool {
        self.len == 0
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn push(&mut self, value: OptMatch) {
        debug_assert!(self.len < ZSTD_OPT_NUM);
        self.entries[self.len] = value;
        self.len += 1;
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn last(&self) -> Option<&OptMatch> {
        if self.len == 0 {
            None
        } else {
            Some(&self.entries[self.len - 1])
        }
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn as_slice(&self) -> &[OptMatch] {
        &self.entries[..self.len]
    }

    #[inline(always)]
    pub(in crate::encoding::levels::c_port) fn get(&self, index: usize) -> OptMatch {
        debug_assert!(index < self.len);
        self.entries[index]
    }
}

impl Index<usize> for OptMatchTable {
    type Output = OptMatch;

    #[inline(always)]
    fn index(&self, index: usize) -> &Self::Output {
        &self.as_slice()[index]
    }
}