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
//! Conversion from `u32` into [`U256`].
use super::U256;
/// Creates a [`U256`] from a `u32` value, placing it in the least
/// significant limb with the upper three limbs set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let v = U256::from(42u32);
/// assert_eq!(v, U256::from_be_limbs([0, 0, 0, 42]));
/// ```
impl From<u32> for U256 {
#[inline]
fn from(value: u32) -> Self {
Self::from_be_limbs([0, 0, 0, value as u64])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Converting zero produces U256::ZERO.
#[test]
fn zero() {
assert_eq!(U256::from(0u32), U256::ZERO);
}
/// Converting one produces U256::ONE.
#[test]
fn one() {
assert_eq!(U256::from(1u32), U256::ONE);
}
/// Converting u32::MAX fills only the low 32 bits of the lowest limb.
#[test]
fn max_u32() {
assert_eq!(
U256::from(u32::MAX),
U256::from_be_limbs([0, 0, 0, u32::MAX as u64]),
);
}
/// Matches equivalent u64 conversion.
#[test]
fn matches_u64() {
assert_eq!(U256::from(12345u32), U256::from(12345u64));
}
}