Merge pull request #395 from pandoracore/fix-construct_uint
Improving `construct_uint` macro
This commit is contained in:
commit
a2bfcb5a89
|
@ -16,23 +16,22 @@
|
||||||
//! to avoid mixing data of the same hash format (like SHA256d) but of different meaning
|
//! to avoid mixing data of the same hash format (like SHA256d) but of different meaning
|
||||||
//! (transaction id, block hash etc).
|
//! (transaction id, block hash etc).
|
||||||
|
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use consensus::encode::{Encodable, Decodable, Error};
|
use consensus::encode::{Encodable, Decodable, Error};
|
||||||
use hashes::{sha256, sha256d, hash160, Hash};
|
use hashes::{Hash, sha256, sha256d, ripemd160, hash160};
|
||||||
use hashes::hex::{ToHex, FromHex};
|
use hashes::hex::{FromHex, ToHex};
|
||||||
|
|
||||||
macro_rules! impl_hashencode {
|
macro_rules! impl_hashencode {
|
||||||
($hashtype:ident) => {
|
($hashtype:ident) => {
|
||||||
impl Encodable for $hashtype {
|
impl $crate::consensus::Encodable for $hashtype {
|
||||||
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
|
fn consensus_encode<S: ::std::io::Write>(&self, s: S) -> Result<usize, $crate::consensus::encode::Error> {
|
||||||
self.0.consensus_encode(s)
|
self.0.consensus_encode(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for $hashtype {
|
impl $crate::consensus::Decodable for $hashtype {
|
||||||
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
|
fn consensus_decode<D: ::std::io::Read>(d: D) -> Result<Self, $crate::consensus::encode::Error> {
|
||||||
Ok(Self::from_inner(<<$hashtype as Hash>::Inner>::consensus_decode(d)?))
|
use $crate::hashes::Hash;
|
||||||
|
Ok(Self::from_inner(<<$hashtype as $crate::hashes::Hash>::Inner>::consensus_decode(d)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)?;
|
||||||
|
|
Loading…
Reference in New Issue