Skip to content

Commit aeeabd7

Browse files
authored
Implement FromBytes (#39)
1 parent 13a7fdb commit aeeabd7

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ default-features = false
2727
[dependencies.zeroize]
2828
version = "1.8.1"
2929
optional = true
30+
default-features = false
3031

3132
[profile.dev]
3233
opt-level = 0

src/fixeduint.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,14 @@ pub struct BytesHolder<T: MachineWord, const N: usize> {
14061406
array: [T; N],
14071407
}
14081408

1409+
impl<T: MachineWord, const N: usize> Default for BytesHolder<T, N> {
1410+
fn default() -> Self {
1411+
Self {
1412+
array: core::array::from_fn(|_| T::default()),
1413+
}
1414+
}
1415+
}
1416+
14091417
#[cfg(feature = "use-unsafe")]
14101418
impl<T: MachineWord, const N: usize> BytesHolder<T, N> {
14111419
// Converts internal storage to a mutable byte slice
@@ -1480,6 +1488,22 @@ where
14801488
}
14811489
}
14821490

1491+
#[cfg(feature = "use-unsafe")]
1492+
impl<T: MachineWord, const N: usize> num_traits::FromBytes for FixedUInt<T, N>
1493+
where
1494+
T: core::fmt::Debug,
1495+
{
1496+
type Bytes = BytesHolder<T, N>;
1497+
1498+
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
1499+
Self::from_be_bytes(bytes.as_ref())
1500+
}
1501+
1502+
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
1503+
Self::from_le_bytes(bytes.as_ref())
1504+
}
1505+
}
1506+
14831507
#[cfg(test)]
14841508
mod tests {
14851509
use super::FixedUInt as Bn;
@@ -1755,4 +1779,38 @@ mod tests {
17551779
&[0x12, 0x34, 0x56, 0x78],
17561780
);
17571781
}
1782+
1783+
fn from_helper<T>(input: &[u8], expected: T)
1784+
where
1785+
T: num_traits::FromBytes + core::fmt::Debug + core::cmp::PartialEq,
1786+
T::Bytes: num_traits::ops::bytes::NumBytes + Default + core::fmt::Debug,
1787+
{
1788+
let mut bytes = T::Bytes::default();
1789+
bytes.as_mut().copy_from_slice(input);
1790+
let result = T::from_be_bytes(&bytes);
1791+
assert_eq!(result, expected);
1792+
bytes.as_mut().reverse();
1793+
let result = T::from_le_bytes(&bytes);
1794+
assert_eq!(result, expected);
1795+
}
1796+
1797+
#[cfg(feature = "use-unsafe")]
1798+
#[test]
1799+
fn test_from_bytes() {
1800+
from_helper(&[0xAB_u8], 0xAB_u8);
1801+
from_helper(&[0xAB, 0xCD], 0xABCD_u16);
1802+
from_helper(&[0x12, 0x34, 0x56, 0x78], 0x12345678_u32);
1803+
from_helper(
1804+
&[0x12, 0x34, 0x56, 0x78],
1805+
FixedUInt::<u8, 4>::from_u32(0x12345678).unwrap(),
1806+
);
1807+
from_helper(
1808+
&[0x12, 0x34, 0x56, 0x78],
1809+
FixedUInt::<u16, 2>::from_u32(0x12345678).unwrap(),
1810+
);
1811+
from_helper(
1812+
&[0x12, 0x34, 0x56, 0x78],
1813+
FixedUInt::<u32, 1>::from_u32(0x12345678).unwrap(),
1814+
);
1815+
}
17581816
}

src/machineword.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub trait MachineWord:
2424
+ core::ops::BitXorAssign
2525
+ num_traits::FromBytes
2626
+ num_traits::ToBytes
27+
+ Default
2728
{
2829
type DoubleWord: num_traits::PrimInt;
2930
fn to_double(self) -> Self::DoubleWord;

0 commit comments

Comments
 (0)