Add conversion from [u8; *]

Conversions I skipped when porting over stuff from bigint but that turned out to be useful.
This commit is contained in:
David Palm 2018-08-21 16:05:41 +02:00
parent abe18ea03c
commit 6d47b02689
3 changed files with 50 additions and 2 deletions

View File

@ -4,7 +4,7 @@ homepage = "http://parity.io"
repository = "https://github.com/paritytech/parity-common"
license = "MIT/Apache-2.0"
name = "uint"
version = "0.3.0"
version = "0.3.1"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]

View File

@ -813,6 +813,19 @@ macro_rules! construct_uint {
arr
}
}
impl From<[u8; $n_words * 8]> for $name {
fn from(bytes: [u8; $n_words * 8]) -> Self {
bytes[..].as_ref().into()
}
}
impl<'a> From<&'a [u8; $n_words * 8]> for $name {
fn from(bytes: &[u8; $n_words * 8]) -> Self {
bytes[..].into()
}
}
impl Default for $name {
fn default() -> Self {
$name::zero()

View File

@ -1001,7 +1001,7 @@ fn from_big_endian() {
}
#[test]
fn from_fixed_array() {
fn into_fixed_array() {
let expected: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
@ -1012,6 +1012,41 @@ fn from_fixed_array() {
assert_eq!(ary, expected);
}
#[test]
fn test_u256_from_fixed_array() {
let ary = [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 123
];
let num : U256 = ary.into();
assert_eq!( num, U256::from(123) );
let a_ref : &U256 = &ary.into();
assert_eq!( a_ref, &U256::from(123) );
}
#[test]
fn test_u512_from_fixed_array() {
let ary = [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 123
];
let num : U512 = ary.into();
assert_eq!( num, U512::from(123) );
let a_ref : &U512 = &ary.into();
assert_eq!( a_ref, &U512::from(123) );
}
#[test]
fn leading_zeros() {
assert_eq!(U256::from("000000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1").leading_zeros(), 95);