Skip to content

Commit

Permalink
Implement Uint::from_be_slice()
Browse files Browse the repository at this point in the history
Needed because Rust 1.29 does not easily allow converting from a slice
into an array.
  • Loading branch information
shesek committed Dec 30, 2020
1 parent fc626a0 commit 6d089b2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/util/uint.rs
Expand Up @@ -90,9 +90,15 @@ macro_rules! construct_uint {
}
}

/// Creates big integer value from a byte slice array using
/// Creates big integer value from a byte array using
/// big-endian encoding
pub fn from_be_bytes(bytes: [u8; $n_words * 8]) -> $name {
Self::from_be_slice(&bytes)
}

/// Creates big integer value from a byte slice using
/// big-endian encoding
pub fn from_be_slice(bytes: &[u8]) -> $name {
use super::endian::slice_to_u64_be;
let mut slice = [0u64; $n_words];
slice.iter_mut()
Expand Down Expand Up @@ -431,7 +437,6 @@ macro_rules! construct_uint {
fn deserialize<D: $crate::serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
use ::std::convert::TryInto;
use ::std::fmt;
use $crate::hashes::hex::FromHex;
use $crate::serde::de;
Expand All @@ -452,7 +457,7 @@ macro_rules! construct_uint {
if bytes.len() != $n_words * 8 {
Err(de::Error::invalid_length(bytes.len() * 2, &self))
} else {
Ok($name::from_be_bytes(bytes[..].try_into().unwrap()))
Ok($name::from_be_slice(&bytes[..]))
}
}
}
Expand Down

0 comments on commit 6d089b2

Please sign in to comment.