working serialization gen

This commit is contained in:
Nikolay Volf
2016-04-20 19:01:53 +03:00
parent 3908ddf609
commit 8b1197b335
5 changed files with 84 additions and 20 deletions

View File

@@ -16,12 +16,50 @@
//! Binary representation of types
use util::bytes::*;
use std::mem;
pub struct BinaryConvertError;
pub trait BinaryConvertable : Sized {
fn size(&self) -> Result<usize, BinaryConvertError>;
fn size(&self) -> usize;
fn to_bytes(buffer: &mut [u8]) -> Result<(), BinaryConvertError>;
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError>;
fn from_bytes(buffer: &[u8]) -> Result<Self, BinaryConvertError>;
}
macro_rules! binary_fixed_size {
($target_ty: ident) => {
impl BinaryConvertable for $target_ty {
fn size(&self) -> usize {
mem::size_of::<$target_ty>()
}
fn from_bytes(bytes: &[u8]) -> Result<Self, BinaryConvertError> {
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
::std::cmp::Ordering::Less => return Err(BinaryConvertError),
::std::cmp::Ordering::Greater => return Err(BinaryConvertError),
::std::cmp::Ordering::Equal => ()
};
let mut res: Self = unsafe { ::std::mem::uninitialized() };
res.copy_raw(bytes);
Ok(res)
}
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError> {
let sz = ::std::mem::size_of::<$target_ty>();
let ip: *const $target_ty = self;
let ptr: *const u8 = ip as *const _;
unsafe {
::std::ptr::copy(ptr, buffer.as_mut_ptr(), sz);
}
Ok(())
}
}
}
}
binary_fixed_size!(u64);
binary_fixed_size!(u32);
binary_fixed_size!(bool);

View File

@@ -19,8 +19,9 @@
extern crate ethcore_devtools as devtools;
extern crate semver;
extern crate nanomsg;
extern crate ethcore_util as util;
pub mod interface;
pub mod binary;
pub use interface::{IpcInterface, IpcSocket, invoke, IpcConfig, Handshake, Error, WithSocket};
pub use binary::{BinaryConvertable};
pub use binary::{BinaryConvertable, BinaryConvertError};