Skip to main content

fixed_bigint/heapless/
prim_int.rs

1//! `num_traits::PrimInt` for `HeaplessBigInt<T, CAP, Nct>`.
2//!
3//! A thin bridge: the bit-operation methods delegate to the already-implemented
4//! `const_num_traits::PrimBits`, and `pow` to the shared `pow_impl`. Nct-only
5//! (PrimInt supertrait-bundles `Num`, which is Nct on this carrier);
6//! `reverse_bits`/`leading_ones`/`trailing_ones` use the trait defaults, same
7//! as `FixedUInt`.
8
9use super::HeaplessBigInt;
10use super::pow::pow_impl;
11use crate::MachineWord;
12use const_num_traits::{CarryingMul, Nct, PrimBits};
13
14impl<T, const CAP: usize> num_traits::PrimInt for HeaplessBigInt<T, CAP, Nct>
15where
16    T: MachineWord + CarryingMul<Unsigned = T, Output = T>,
17{
18    fn count_ones(self) -> u32 {
19        PrimBits::count_ones(self)
20    }
21    fn count_zeros(self) -> u32 {
22        PrimBits::count_zeros(self)
23    }
24    fn leading_zeros(self) -> u32 {
25        PrimBits::leading_zeros(self)
26    }
27    fn trailing_zeros(self) -> u32 {
28        PrimBits::trailing_zeros(self)
29    }
30    fn rotate_left(self, n: u32) -> Self {
31        PrimBits::rotate_left(self, n)
32    }
33    fn rotate_right(self, n: u32) -> Self {
34        PrimBits::rotate_right(self, n)
35    }
36    fn signed_shl(self, n: u32) -> Self {
37        PrimBits::signed_shl(self, n)
38    }
39    fn signed_shr(self, n: u32) -> Self {
40        PrimBits::signed_shr(self, n)
41    }
42    fn unsigned_shl(self, n: u32) -> Self {
43        PrimBits::unsigned_shl(self, n)
44    }
45    fn unsigned_shr(self, n: u32) -> Self {
46        PrimBits::unsigned_shr(self, n)
47    }
48    fn swap_bytes(self) -> Self {
49        PrimBits::swap_bytes(self)
50    }
51    // Override the num_traits default: it reverses via shifts, and heapless's
52    // `Shr` narrows `len`, so the default collapses to zero. PrimBits does it
53    // limb-wise at the value width.
54    fn reverse_bits(self) -> Self {
55        PrimBits::reverse_bits(self)
56    }
57    fn from_be(x: Self) -> Self {
58        PrimBits::from_be(x)
59    }
60    fn from_le(x: Self) -> Self {
61        PrimBits::from_le(x)
62    }
63    fn to_be(self) -> Self {
64        PrimBits::to_be(self)
65    }
66    fn to_le(self) -> Self {
67        PrimBits::to_le(self)
68    }
69    fn pow(self, exp: u32) -> Self {
70        pow_impl(self, exp)
71    }
72}