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    #[cfg_attr(docsrs, doc(cfg(target_pointer_width = "64")))]
29    pub const fn from_u64(n: u64) -> Self {
30        Limb(n)
31    }
32}
33
34impl From<u8> for Limb {
35    #[inline]
36    fn from(n: u8) -> Limb {
37        Limb(n.into())
38    }
39}
40
41impl From<u16> for Limb {
42    #[inline]
43    fn from(n: u16) -> Limb {
44        Limb(n.into())
45    }
46}
47
48impl From<u32> for Limb {
49    #[inline]
50    fn from(n: u32) -> Limb {
51        Limb(n.into())
52    }
53}
54
55#[cfg(target_pointer_width = "64")]
56#[cfg_attr(docsrs, doc(cfg(target_pointer_width = "64")))]
57impl From<u64> for Limb {
58    #[inline]
59    fn from(n: u64) -> Limb {
60        Limb(n)
61    }
62}
63
64impl From<Limb> for Word {
65    #[inline]
66    fn from(limb: Limb) -> Word {
67        limb.0
68    }
69}
70
71impl From<Limb> for WideWord {
72    #[inline]
73    fn from(limb: Limb) -> WideWord {
74        limb.0.into()
75    }
76}