vec serialization

This commit is contained in:
Nikolay Volf 2016-04-20 20:22:24 +03:00
parent e3c20e1c64
commit a61ab6d40f
2 changed files with 75 additions and 5 deletions

View File

@ -301,6 +301,7 @@ fn binary_expr_variant(
let type_name = ::syntax::print::pprust::ty_to_string(&ty); let type_name = ::syntax::print::pprust::ty_to_string(&ty);
let variant_ident = variant.node.name; let variant_ident = variant.node.name;
let variant_index_ident = builder.id(format!("{}", variant_index));
match variant.node.data { match variant.node.data {
ast::VariantData::Unit(_) => { ast::VariantData::Unit(_) => {
@ -310,7 +311,7 @@ fn binary_expr_variant(
Ok(BinaryArm { Ok(BinaryArm {
size: quote_arm!(cx, $pat => { 0usize } ), size: quote_arm!(cx, $pat => { 0usize } ),
write: quote_arm!(cx, $pat => { Ok(()) } ), write: quote_arm!(cx, $pat => { buffer[0] = $variant_index_ident; Ok(()) } ),
read: quote_arm!(cx, $pat => { } ), read: quote_arm!(cx, $pat => { } ),
}) })
}, },
@ -336,11 +337,14 @@ fn binary_expr_variant(
)); ));
let (size_expr, write_expr, read_expr) = (binary_expr.size, vec![binary_expr.write], binary_expr.read); let (size_expr, write_expr, read_expr) = (binary_expr.size, vec![binary_expr.write], binary_expr.read);
let variant_index_ident = builder.id(format!("{}", variant_index));
Ok(BinaryArm { Ok(BinaryArm {
size: quote_arm!(cx, $pat => { $size_expr } ), size: quote_arm!(cx, $pat => { $size_expr } ),
write: quote_arm!(cx, $pat => { buffer[0] = $variant_index_ident; let buffer = &mut buffer[1..]; $write_expr } ), write: quote_arm!(cx,
$pat => {
buffer[0] = $variant_index_ident;
let buffer = &mut buffer[1..];
$write_expr
}),
read: quote_arm!(cx, $pat => { $read_expr } ), read: quote_arm!(cx, $pat => { $read_expr } ),
}) })
} }
@ -369,7 +373,12 @@ fn binary_expr_variant(
let (size_expr, write_expr, read_expr) = (binary_expr.size, vec![binary_expr.write], binary_expr.read); let (size_expr, write_expr, read_expr) = (binary_expr.size, vec![binary_expr.write], binary_expr.read);
Ok(BinaryArm { Ok(BinaryArm {
size: quote_arm!(cx, $pat => { $size_expr } ), size: quote_arm!(cx, $pat => { $size_expr } ),
write: quote_arm!(cx, $pat => { $write_expr } ), write: quote_arm!(cx,
$pat => {
buffer[0] = $variant_index_ident;
let buffer = &mut buffer[1..];
$write_expr
}),
read: quote_arm!(cx, $pat => { $read_expr } ), read: quote_arm!(cx, $pat => { $read_expr } ),
}) })
}, },

View File

@ -27,6 +27,67 @@ pub trait BinaryConvertable : Sized {
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError>; fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError>;
fn from_bytes(buffer: &[u8]) -> Result<Self, BinaryConvertError>; fn from_bytes(buffer: &[u8]) -> Result<Self, BinaryConvertError>;
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
Err(BinaryConvertError)
}
fn len_params() -> usize {
0
}
}
impl<T> BinaryConvertable for Option<T> where T: BinaryConvertable {
fn size(&self) -> usize {
match * self { None => 0, Some(ref val) => val.size() }
}
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError> {
match *self { None => Err(BinaryConvertError), Some(ref val) => val.to_bytes(buffer) }
}
fn from_bytes(buffer: &[u8]) -> Result<Self, BinaryConvertError> {
Ok(Some(try!(T::from_bytes(buffer))))
}
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
Ok(None)
}
fn len_params() -> usize {
1
}
}
impl<T> BinaryConvertable for Vec<T> where T: BinaryConvertable {
fn size(&self) -> usize {
match T::len_params() {
0 => mem::size_of::<T>() * self.len(),
_ => self.iter().fold(0usize, |acc, t| acc + t.size()),
}
}
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError> {
let mut offset = 0usize;
for (index, item) in self.iter().enumerate() {
let item_end = offset + item.size();
try!(item.to_bytes(&mut buffer[offset..item_end]));
offset = item_end;
}
Ok(())
}
fn from_bytes(buffer: &[u8]) -> Result<Self, BinaryConvertError> {
Err(BinaryConvertError)
}
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
Ok(Self::new())
}
fn len_params() -> usize {
1
}
} }
macro_rules! binary_fixed_size { macro_rules! binary_fixed_size {