1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Conversion from `u64` into [`U256`].
use super::U256;
/// Creates a [`U256`] from a `u64` value, placing it in the least
/// significant limb with the upper three limbs set to zero.
///
/// This provides ergonomic construction of small values without
/// needing the full `from_be_limbs([0, 0, 0, n])` syntax.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let v = U256::from(42u64);
/// assert_eq!(v, U256::from_be_limbs([0, 0, 0, 42]));
/// ```
impl From<u64> for U256 {
#[inline]
fn from(value: u64) -> Self {
Self::from_be_limbs([0, 0, 0, value])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Converting zero produces U256::ZERO.
#[test]
fn zero() {
assert_eq!(U256::from(0u64), U256::ZERO);
}
/// Converting one produces U256::ONE.
#[test]
fn one() {
assert_eq!(U256::from(1u64), U256::ONE);
}
/// Converting u64::MAX fills only the lowest limb.
#[test]
fn max_u64() {
assert_eq!(
U256::from(u64::MAX),
U256::from_be_limbs([0, 0, 0, u64::MAX]),
);
}
/// Arbitrary value round-trips through to_be_limbs.
#[test]
fn round_trip() {
let v = U256::from(0xDEADBEEFu64);
assert_eq!(v.to_be_limbs(), [0, 0, 0, 0xDEADBEEF]);
}
}