minor fixes
This commit is contained in:
parent
416c2ec3e4
commit
fa1b74fa53
19
src/bytes.rs
19
src/bytes.rs
@ -166,6 +166,14 @@ impl ToBytes for u64 {
|
|||||||
fn to_bytes_len(&self) -> usize { 8 - self.leading_zeros() as usize / 8 }
|
fn to_bytes_len(&self) -> usize { 8 - self.leading_zeros() as usize / 8 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ToBytes for bool {
|
||||||
|
fn to_bytes(&self) -> Vec<u8> {
|
||||||
|
vec![ if *self { 1u8 } else { 0u8 } ]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_bytes_len(&self) -> usize { 1 }
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! impl_map_to_bytes {
|
macro_rules! impl_map_to_bytes {
|
||||||
($from: ident, $to: ty) => {
|
($from: ident, $to: ty) => {
|
||||||
impl ToBytes for $from {
|
impl ToBytes for $from {
|
||||||
@ -264,6 +272,17 @@ impl FromBytes for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromBytes for bool {
|
||||||
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<bool> {
|
||||||
|
match bytes.len() {
|
||||||
|
0 => Ok(false),
|
||||||
|
1 => Ok(bytes[0] != 0),
|
||||||
|
_ => Err(FromBytesError::DataIsTooLong),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
macro_rules! impl_map_from_bytes {
|
macro_rules! impl_map_from_bytes {
|
||||||
($from: ident, $to: ident) => {
|
($from: ident, $to: ident) => {
|
||||||
impl FromBytes for $from {
|
impl FromBytes for $from {
|
||||||
|
@ -33,7 +33,7 @@ pub trait FixedHash: Sized + BytesConvertable {
|
|||||||
|
|
||||||
macro_rules! impl_hash {
|
macro_rules! impl_hash {
|
||||||
($from: ident, $size: expr) => {
|
($from: ident, $size: expr) => {
|
||||||
#[derive(Eq)]
|
#[derive(Eq, Copy)]
|
||||||
pub struct $from (pub [u8; $size]);
|
pub struct $from (pub [u8; $size]);
|
||||||
|
|
||||||
impl BytesConvertable for $from {
|
impl BytesConvertable for $from {
|
||||||
|
@ -47,7 +47,9 @@ pub use self::rlpstream::{RlpStream};
|
|||||||
use super::hash::H256;
|
use super::hash::H256;
|
||||||
|
|
||||||
pub const NULL_RLP: [u8; 1] = [0x80; 1];
|
pub const NULL_RLP: [u8; 1] = [0x80; 1];
|
||||||
|
pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1];
|
||||||
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
|
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
|
||||||
|
pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] );
|
||||||
|
|
||||||
/// Shortcut function to decode trusted rlp
|
/// Shortcut function to decode trusted rlp
|
||||||
///
|
///
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use rlp::DecoderError;
|
use rlp::{DecoderError, UntrustedRlp};
|
||||||
|
|
||||||
pub trait Decoder: Sized {
|
pub trait Decoder: Sized {
|
||||||
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
|
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||||
where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
|
where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
|
||||||
|
|
||||||
fn as_list(&self) -> Result<Vec<Self>, DecoderError>;
|
fn as_list(&self) -> Result<Vec<Self>, DecoderError>;
|
||||||
|
fn as_rlp<'a>(&'a self) -> &'a UntrustedRlp<'a>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Decodable: Sized {
|
pub trait Decodable: Sized {
|
||||||
|
@ -321,6 +321,10 @@ impl<'a> Decoder for BasicDecoder<'a> {
|
|||||||
.collect();
|
.collect();
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn as_rlp<'s>(&'s self) -> &'s UntrustedRlp<'s> {
|
||||||
|
&self.rlp
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Decodable for T where T: FromBytes {
|
impl<T> Decodable for T where T: FromBytes {
|
||||||
|
@ -19,7 +19,9 @@ use hash::{FixedHash, H256};
|
|||||||
/// ```
|
/// ```
|
||||||
pub trait Hashable {
|
pub trait Hashable {
|
||||||
fn sha3(&self) -> H256;
|
fn sha3(&self) -> H256;
|
||||||
fn sha3_into(&self, dest: &mut [u8]);
|
fn sha3_into(&self, dest: &mut [u8]) {
|
||||||
|
self.sha3().copy_to(dest);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Hashable for T where T: BytesConvertable {
|
impl<T> Hashable for T where T: BytesConvertable {
|
||||||
|
20
src/uint.rs
20
src/uint.rs
@ -52,11 +52,17 @@ macro_rules! construct_uint {
|
|||||||
impl $name {
|
impl $name {
|
||||||
/// Conversion to u32
|
/// Conversion to u32
|
||||||
#[inline]
|
#[inline]
|
||||||
fn low_u32(&self) -> u32 {
|
pub fn low_u32(&self) -> u32 {
|
||||||
let &$name(ref arr) = self;
|
let &$name(ref arr) = self;
|
||||||
arr[0] as u32
|
arr[0] as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Conversion to u64
|
||||||
|
#[inline]
|
||||||
|
pub fn low_u64(&self) -> u64 {
|
||||||
|
let &$name(ref arr) = self;
|
||||||
|
arr[0]
|
||||||
|
}
|
||||||
/// Return the least number of bits needed to represent the number
|
/// Return the least number of bits needed to represent the number
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bits(&self) -> usize {
|
pub fn bits(&self) -> usize {
|
||||||
@ -434,6 +440,18 @@ impl From<U128> for U256 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<U256> for u64 {
|
||||||
|
fn from(value: U256) -> u64 {
|
||||||
|
value.low_u64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<U256> for u32 {
|
||||||
|
fn from(value: U256) -> u32 {
|
||||||
|
value.low_u32()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub const ZERO_U256: U256 = U256([0x00u64; 4]);
|
pub const ZERO_U256: U256 = U256([0x00u64; 4]);
|
||||||
pub const ONE_U256: U256 = U256([0x01u64, 0x00u64, 0x00u64, 0x00u64]);
|
pub const ONE_U256: U256 = U256([0x01u64, 0x00u64, 0x00u64, 0x00u64]);
|
||||||
pub const BAD_U256: U256 = U256([0xffffffffffffffffu64; 4]);
|
pub const BAD_U256: U256 = U256([0xffffffffffffffffu64; 4]);
|
||||||
|
Loading…
Reference in New Issue
Block a user