fixed_bigint/
machineword.rs1pub use crate::const_numtraits::ConstPrimInt;
19use crate::const_numtraits::{
20 ConstOverflowingAdd, ConstOverflowingSub, ConstToBytes, ConstWideningMul,
21};
22
23c0nst::c0nst! {
24 pub c0nst trait ConstMachineWord:
27 [c0nst] ConstPrimInt +
28 [c0nst] ConstOverflowingAdd +
29 [c0nst] ConstOverflowingSub +
30 [c0nst] ConstToBytes +
31 [c0nst] ConstWideningMul
32 {
33 type ConstDoubleWord: [c0nst] ConstPrimInt;
34 fn to_double(self) -> Self::ConstDoubleWord;
35 fn from_double(word: Self::ConstDoubleWord) -> Self;
36 }
37
38 impl c0nst ConstMachineWord for u8 {
39 type ConstDoubleWord = u16;
40 fn to_double(self) -> u16 { self as u16 }
41 fn from_double(word: u16) -> u8 { word as u8 }
42 }
43 impl c0nst ConstMachineWord for u16 {
44 type ConstDoubleWord = u32;
45 fn to_double(self) -> u32 { self as u32 }
46 fn from_double(word: u32) -> u16 { word as u16 }
47 }
48 impl c0nst ConstMachineWord for u32 {
49 type ConstDoubleWord = u64;
50 fn to_double(self) -> u64 { self as u64 }
51 fn from_double(word: u64) -> u32 { word as u32 }
52 }
53 impl c0nst ConstMachineWord for u64 {
54 type ConstDoubleWord = u128;
55 fn to_double(self) -> u128 { self as u128 }
56 fn from_double(word: u128) -> u64 { word as u64 }
57 }
58}
59
60pub trait MachineWord:
66 ConstMachineWord<ConstDoubleWord = Self::DoubleWord> + core::hash::Hash + num_traits::ToPrimitive
67{
68 type DoubleWord: ConstPrimInt;
69}
70
71impl MachineWord for u8 {
72 type DoubleWord = u16;
73}
74impl MachineWord for u16 {
75 type DoubleWord = u32;
76}
77impl MachineWord for u32 {
78 type DoubleWord = u64;
79}
80impl MachineWord for u64 {
81 type DoubleWord = u128;
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 c0nst::c0nst! {
89 pub c0nst fn to_double<T: [c0nst] ConstMachineWord>(a: T) -> T::ConstDoubleWord {
90 a.to_double()
91 }
92 }
93
94 #[test]
95 fn test_constmachineword_ops() {
96 assert_eq!(to_double(200u8), 200u16);
97
98 #[cfg(feature = "nightly")]
99 {
100 const DOUBLE_RES: u16 = to_double(200u8);
101 assert_eq!(DOUBLE_RES, 200u16);
102 }
103 }
104}