relp-num 0.2.1

Number types for the relp crate
Documentation
use std::mem;

use smallvec::SmallVec;

/// Unsigned big integer.
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct Ubig<const S: usize>(SmallVec<[usize; S]>);
/// Non zero unsigned big integer.
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct NonZeroUbig<const S: usize>(SmallVec<[usize; S]>);

pub mod io;
pub mod ops;
pub mod properties;

impl<const S: usize> Ubig<S> {
    pub(crate) unsafe fn is_well_formed(&self) -> bool {
        match self.0.last() {
            None => true,
            Some(&value) => value != 0,
        }
    }
    pub(crate) fn inner(&self) -> &SmallVec<[usize; S]> {
        &self.0
    }
    pub(crate) unsafe fn inner_mut(&mut self) -> &mut SmallVec<[usize; S]> {
        &mut self.0
    }
    pub(crate) fn into_inner(self) -> SmallVec<[usize; S]> {
        self.0
    }
}

impl<const S: usize> NonZeroUbig<S> {
    pub(crate) unsafe fn is_well_formed(&self) -> bool {
        match self.0.last() {
            None => false,
            Some(&value) => value != 0,
        }
    }
    pub(crate) fn inner(&self) -> &SmallVec<[usize; S]> {
        &self.0
    }
    pub(crate) unsafe fn inner_mut(&mut self) -> &mut SmallVec<[usize; S]> {
        &mut self.0
    }
    pub(crate) fn first(&self) -> &usize {
        // SAFETY: A `NonZeroUbig` is never empty, so index zero is always in bounds.
        unsafe {
            self.0.get_unchecked(0)
        }
    }
    pub(crate) unsafe fn first_mut(&mut self) -> &mut usize {
        // SAFETY: A `NonZeroUbig` is never empty, so index zero is always in bounds. This is
        // `unsafe` for what the caller may write through the reference, not for the read: setting
        // the only word to zero would break that invariant.
        unsafe { self.0.get_unchecked_mut(0) }
    }
    pub(crate) fn into_inner(self) -> SmallVec<[usize; S]> {
        self.0
    }
}

pub const BITS_PER_WORD: u32 = (mem::size_of::<usize>() * 8) as u32;