arithmetic_coding_core/
bitstore.rs

1use std::ops::{Add, AddAssign, Div, Mul, Shl, ShlAssign, Sub};
2
3/// A trait for a type that can be used for the internal integer representation
4/// of an encoder or decoder
5pub trait BitStore:
6    Shl<u32, Output = Self>
7    + ShlAssign<u32>
8    + Sub<Output = Self>
9    + Add<Output = Self>
10    + Mul<Output = Self>
11    + Div<Output = Self>
12    + AddAssign
13    + PartialOrd
14    + Copy
15    + std::fmt::Debug
16{
17    /// the number of bits needed to represent this type
18    const BITS: u32;
19
20    /// the additive identity
21    const ZERO: Self;
22
23    /// the multiplicative identity
24    const ONE: Self;
25
26    /// integer natural logarithm, rounded down
27    fn log2(self) -> u32;
28}
29
30macro_rules! impl_bitstore {
31    ($t:ty) => {
32        impl BitStore for $t {
33            const BITS: u32 = Self::BITS;
34            const ONE: Self = 1;
35            const ZERO: Self = 0;
36
37            fn log2(self) -> u32 {
38                Self::ilog2(self)
39            }
40        }
41    };
42}
43
44impl_bitstore! {u32}
45impl_bitstore! {u64}
46impl_bitstore! {u128}
47impl_bitstore! {usize}