generalized fixedsized structs with macro
This commit is contained in:
parent
17f26ad588
commit
3138584320
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
use util::hash::H256;
|
use util::hash::H256;
|
||||||
use header::BlockNumber;
|
use header::BlockNumber;
|
||||||
|
use util::bytes::{FromRawBytes, FromBytesError, ToBytesWithMap, Populatable};
|
||||||
|
|
||||||
/// Uniquely identifies block.
|
/// Uniquely identifies block.
|
||||||
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
|
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
|
||||||
@ -50,3 +51,7 @@ pub struct UncleId (
|
|||||||
/// Position in block.
|
/// Position in block.
|
||||||
pub usize
|
pub usize
|
||||||
);
|
);
|
||||||
|
|
||||||
|
sized_binary_map!(TransactionId);
|
||||||
|
sized_binary_map!(UncleId);
|
||||||
|
sized_binary_map!(BlockId);
|
||||||
|
@ -265,19 +265,42 @@ impl<T> FromRawBytes for T where T: FixedHash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRawBytes for u16 {
|
#[macro_export]
|
||||||
fn from_bytes(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
macro_rules! sized_binary_map {
|
||||||
match bytes.len().cmp(&2) {
|
($target_ty: ident) => {
|
||||||
Ordering::Less => return Err(FromBytesError::NotLongEnough),
|
impl FromRawBytes for $target_ty {
|
||||||
Ordering::Greater => return Err(FromBytesError::TooLong),
|
fn from_bytes(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||||
Ordering::Equal => ()
|
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
|
||||||
};
|
::std::cmp::Ordering::Less => return Err(FromBytesError::NotLongEnough),
|
||||||
let mut res: Self = unsafe { mem::uninitialized() };
|
::std::cmp::Ordering::Greater => return Err(FromBytesError::TooLong),
|
||||||
res.copy_raw(bytes);
|
::std::cmp::Ordering::Equal => ()
|
||||||
Ok(res)
|
};
|
||||||
|
let mut res: Self = unsafe { ::std::mem::uninitialized() };
|
||||||
|
res.copy_raw(bytes);
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ToBytesWithMap for $target_ty {
|
||||||
|
fn to_bytes_map(&self) -> Vec<u8> {
|
||||||
|
let sz = ::std::mem::size_of::<$target_ty>();
|
||||||
|
let mut res = Vec::<u8>::with_capacity(sz);
|
||||||
|
|
||||||
|
let ip: *const $target_ty = self;
|
||||||
|
let ptr: *const u8 = ip as *const _;
|
||||||
|
unsafe {
|
||||||
|
res.set_len(sz);
|
||||||
|
::std::ptr::copy(ptr, res.as_mut_ptr(), sz);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sized_binary_map!(u16);
|
||||||
|
sized_binary_map!(u32);
|
||||||
|
sized_binary_map!(u64);
|
||||||
|
|
||||||
/// Value that can be serialized from variable-length byte array
|
/// Value that can be serialized from variable-length byte array
|
||||||
pub trait FromRawBytesVariable : Sized {
|
pub trait FromRawBytesVariable : Sized {
|
||||||
/// Create value from slice
|
/// Create value from slice
|
||||||
@ -406,7 +429,9 @@ impl FromRawBytesVariable for Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Value that serializes directly to variable-sized byte array and stores map
|
||||||
pub trait ToBytesWithMap {
|
pub trait ToBytesWithMap {
|
||||||
|
/// serialize to variable-sized byte array and store map
|
||||||
fn to_bytes_map(&self) -> Vec<u8>;
|
fn to_bytes_map(&self) -> Vec<u8>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -416,36 +441,6 @@ impl<T> ToBytesWithMap for T where T: FixedHash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToBytesWithMap for u16 {
|
|
||||||
fn to_bytes_map(&self) -> Vec<u8> {
|
|
||||||
let sz = mem::size_of::<u16>();
|
|
||||||
let mut res = Vec::<u8>::with_capacity(sz);
|
|
||||||
|
|
||||||
let ip: *const u16 = self;
|
|
||||||
let ptr: *const u8 = ip as *const _;
|
|
||||||
unsafe {
|
|
||||||
res.set_len(sz);
|
|
||||||
::std::ptr::copy(ptr, res.as_mut_ptr(), sz);
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ToBytesWithMap for u64 {
|
|
||||||
fn to_bytes_map(&self) -> Vec<u8> {
|
|
||||||
let sz = mem::size_of::<u64>();
|
|
||||||
let mut res = Vec::<u8>::with_capacity(sz);
|
|
||||||
|
|
||||||
let ip: *const u64 = self;
|
|
||||||
let ptr: *const u8 = ip as *const _;
|
|
||||||
unsafe {
|
|
||||||
res.set_len(sz);
|
|
||||||
::std::ptr::copy(ptr, res.as_mut_ptr(), sz);
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fax_raw() {
|
fn fax_raw() {
|
||||||
let mut x = [255u8; 4];
|
let mut x = [255u8; 4];
|
||||||
|
Loading…
Reference in New Issue
Block a user