Skip to main content

fixed_bigint/heapless/
from_prim.rs

1//! `From<u8>` / `From<u16>` / `From<u32>` for `HeaplessBigInt`.
2//!
3//! Small-value constructors. Output `len` is fixed by the source
4//! primitive width (`ceil(size_of::<uN>() / size_of::<T>())`), not by
5//! the value — so the shape is public. Under-sized capacity (`CAP` too
6//! small to hold the source primitive) triggers `from_le_bytes`'s
7//! runtime assertion; matches `FixedUInt`'s implicit contract.
8//!
9//! This is the source-int width, which is likely narrower than the width
10//! a downstream computation needs — see the construction-width table in
11//! the [module docs](super). To carry the value at a chosen width, pin it
12//! with [`WithPrecision`](const_num_traits::WithPrecision) (e.g.
13//! `From::from(v).widen_to_precision_of(&modulus)`).
14
15use super::HeaplessBigInt;
16use crate::MachineWord;
17use const_num_traits::Personality;
18
19impl<T: MachineWord, const CAP: usize, P: Personality> From<u8> for HeaplessBigInt<T, CAP, P> {
20    fn from(v: u8) -> Self {
21        Self::from_le_bytes(&v.to_le_bytes())
22    }
23}
24
25impl<T: MachineWord, const CAP: usize, P: Personality> From<u16> for HeaplessBigInt<T, CAP, P> {
26    fn from(v: u16) -> Self {
27        Self::from_le_bytes(&v.to_le_bytes())
28    }
29}
30
31impl<T: MachineWord, const CAP: usize, P: Personality> From<u32> for HeaplessBigInt<T, CAP, P> {
32    fn from(v: u32) -> Self {
33        Self::from_le_bytes(&v.to_le_bytes())
34    }
35}