arithmetic_coding_core_adder_dep/
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    + Sized
9    + From<u32>
10    + Sub<Output = Self>
11    + Add<Output = Self>
12    + Mul<Output = Self>
13    + Div<Output = Self>
14    + AddAssign
15    + PartialOrd
16    + Copy
17    + std::fmt::Debug
18{
19    /// the number of bits needed to represent this type
20    const BITS: u32;
21
22    /// the additive identity
23    const ZERO: Self;
24
25    /// the multiplicative identity
26    const ONE: Self;
27
28    /// integer natural logarithm, rounded down
29    fn log2(self) -> u32;
30}
31
32macro_rules! impl_bitstore {
33    ($t:ty) => {
34        impl BitStore for $t {
35            const BITS: u32 = Self::BITS;
36            const ONE: Self = 1;
37            const ZERO: Self = 0;
38
39            fn log2(self) -> u32 {
40                Self::ilog2(self)
41            }
42        }
43    };
44}
45
46impl_bitstore! {u32}
47impl_bitstore! {u64}
48impl_bitstore! {u128}