crypto_bigint/limb/
from.rs

1//! `From`-like conversions for [`Limb`].
2
3use super::{Limb, WideWord, Word};
4
5impl Limb {
6    /// Create a [`Limb`] from a `u8` integer (const-friendly)
7    // TODO(tarcieri): replace with `const impl From<u8>` when stable
8    pub const fn from_u8(n: u8) -> Self {
9        Limb(n as Word)
10    }
11
12    /// Create a [`Limb`] from a `u16` integer (const-friendly)
13    // TODO(tarcieri): replace with `const impl From<u16>` when stable
14    pub const fn from_u16(n: u16) -> Self {
15        Limb(n as Word)
16    }
17
18    /// Create a [`Limb`] from a `u32` integer (const-friendly)
19    // TODO(tarcieri): replace with `const impl From<u32>` when stable
20    pub const fn from_u32(n: u32) -> Self {
21        #[allow(trivial_numeric_casts)]
22        Limb(n as Word)
23    }
24
25    /// Create a [`Limb`] from a `u64` integer (const-friendly)
26    // TODO(tarcieri): replace with `const impl From<u64>` when stable
27    #[cfg(target_pointer_width = "64")]
28    pub const fn from_u64(n: u64) -> Self {
29        Limb(n)
30    }
31}
32
33impl From<u8> for Limb {
34    #[inline]
35    fn from(n: u8) -> Limb {
36        Limb(n.into())
37    }
38}
39
40impl From<u16> for Limb {
41    #[inline]
42    fn from(n: u16) -> Limb {
43        Limb(n.into())
44    }
45}
46
47impl From<u32> for Limb {
48    #[inline]
49    fn from(n: u32) -> Limb {
50        Limb(n.into())
51    }
52}
53
54#[cfg(target_pointer_width = "64")]
55impl From<u64> for Limb {
56    #[inline]
57    fn from(n: u64) -> Limb {
58        Limb(n)
59    }
60}
61
62impl From<Limb> for Word {
63    #[inline]
64    fn from(limb: Limb) -> Word {
65        limb.0
66    }
67}
68
69impl From<Limb> for WideWord {
70    #[inline]
71    fn from(limb: Limb) -> WideWord {
72        limb.0.into()
73    }
74}