From 63c5c4def5e9631ce06e83ac1e790f18cd707629 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 14 Aug 2017 18:15:38 +0300 Subject: [PATCH] add little test --- src/uint.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/uint.rs b/src/uint.rs index c3eaae8..3b61899 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -1453,36 +1453,60 @@ impl<'a> From<&'a [u8; 32]> for U256 { } } -impl<'a> From<[u8; 32]> for U256 { +impl From<[u8; 32]> for U256 { fn from(bytes: [u8; 32]) -> Self { bytes[..].as_ref().into() } } +impl From for [u8; 32] { + fn from(number: U256) -> Self { + let mut arr = [0u8; 32]; + number.to_big_endian(&mut arr); + arr + } +} + impl<'a> From<&'a [u8; 16]> for U128 { fn from(bytes: &[u8; 16]) -> Self { bytes[..].into() } } -impl<'a> From<[u8; 16]> for U128 { +impl From<[u8; 16]> for U128 { fn from(bytes: [u8; 16]) -> Self { bytes[..].as_ref().into() } } +impl From for [u8; 16] { + fn from(number: U128) -> Self { + let mut arr = [0u8; 16]; + number.to_big_endian(&mut arr); + arr + } +} + impl<'a> From<&'a [u8; 64]> for U512 { fn from(bytes: &[u8; 64]) -> Self { bytes[..].into() } } -impl<'a> From<[u8; 64]> for U512 { +impl From<[u8; 64]> for U512 { fn from(bytes: [u8; 64]) -> Self { bytes[..].as_ref().into() } } +impl From for [u8; 64] { + fn from(number: U512) -> Self { + let mut arr = [0u8; 64]; + number.to_big_endian(&mut arr); + arr + } +} + #[cfg(feature="heapsizeof")] known_heap_size!(0, U128, U256); @@ -2377,7 +2401,6 @@ mod tests { assert_eq!(&raw, &new_raw); } - #[test] fn slice_roundtrip_le() { let raw = [ @@ -2395,4 +2418,13 @@ mod tests { assert_eq!(&raw, &new_raw); } -} + + #[test] + fn fixed_arrays_roundtrip() { + let raw: U256 = "7094875209347850239487502394881".into(); + let array: [u8; 32] = raw.into(); + let new_raw = array.into(); + + assert_eq!(raw, new_raw); + } + }