Improving `construct_uint` macro

This commit is contained in:
Dr Maxim Orlovsky 2020-01-21 13:11:48 +01:00
parent 5452260884
commit f3e762b555
1 changed files with 14 additions and 18 deletions

View File

@ -18,11 +18,6 @@
//! The functions here are designed to be fast. //! The functions here are designed to be fast.
//! //!
use std::fmt;
use consensus::encode;
use util::BitArray;
macro_rules! construct_uint { macro_rules! construct_uint {
($name:ident, $n_words:expr) => ( ($name:ident, $n_words:expr) => (
/// Little-endian large integer type /// Little-endian large integer type
@ -116,7 +111,7 @@ macro_rules! construct_uint {
#[inline] #[inline]
fn sub(self, other: $name) -> $name { fn sub(self, other: $name) -> $name {
self + !other + BitArray::one() self + !other + $crate::util::BitArray::one()
} }
} }
@ -124,6 +119,7 @@ macro_rules! construct_uint {
type Output = $name; type Output = $name;
fn mul(self, other: $name) -> $name { fn mul(self, other: $name) -> $name {
use $crate::util::BitArray;
let mut me = $name::zero(); let mut me = $name::zero();
// TODO: be more efficient about this // TODO: be more efficient about this
for i in 0..(2 * $n_words) { for i in 0..(2 * $n_words) {
@ -170,7 +166,7 @@ macro_rules! construct_uint {
} }
} }
impl BitArray for $name { impl $crate::util::BitArray for $name {
#[inline] #[inline]
fn bit(&self, index: usize) -> bool { fn bit(&self, index: usize) -> bool {
let &$name(ref arr) = self; let &$name(ref arr) = self;
@ -214,7 +210,7 @@ macro_rules! construct_uint {
impl ::std::default::Default for $name { impl ::std::default::Default for $name {
fn default() -> $name { fn default() -> $name {
BitArray::zero() $crate::util::BitArray::zero()
} }
} }
@ -319,8 +315,8 @@ macro_rules! construct_uint {
} }
} }
impl fmt::Debug for $name { impl ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let &$name(ref data) = self; let &$name(ref data) = self;
write!(f, "0x")?; write!(f, "0x")?;
for ch in data.iter().rev() { for ch in data.iter().rev() {
@ -330,18 +326,18 @@ macro_rules! construct_uint {
} }
} }
impl fmt::Display for $name { impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
<fmt::Debug>::fmt(self, f) <::std::fmt::Debug>::fmt(self, f)
} }
} }
impl ::consensus::Encodable for $name { impl $crate::consensus::Encodable for $name {
#[inline] #[inline]
fn consensus_encode<S: ::std::io::Write>( fn consensus_encode<S: ::std::io::Write>(
&self, &self,
mut s: S, mut s: S,
) -> Result<usize, encode::Error> { ) -> Result<usize, $crate::consensus::encode::Error> {
let &$name(ref data) = self; let &$name(ref data) = self;
let mut len = 0; let mut len = 0;
for word in data.iter() { for word in data.iter() {
@ -351,11 +347,11 @@ macro_rules! construct_uint {
} }
} }
impl ::consensus::Decodable for $name { impl $crate::consensus::Decodable for $name {
fn consensus_decode<D: ::std::io::Read>( fn consensus_decode<D: ::std::io::Read>(
mut d: D, mut d: D,
) -> Result<$name, encode::Error> { ) -> Result<$name, $crate::consensus::encode::Error> {
use consensus::Decodable; use $crate::consensus::Decodable;
let mut ret: [u64; $n_words] = [0; $n_words]; let mut ret: [u64; $n_words] = [0; $n_words];
for i in 0..$n_words { for i in 0..$n_words {
ret[i] = Decodable::consensus_decode(&mut d)?; ret[i] = Decodable::consensus_decode(&mut d)?;