-
-
Notifications
You must be signed in to change notification settings - Fork 849
Closed
Labels
Description
Currently I'm trying to implement serializing newtype structs. So, we have the next code:
#[inline]
fn serialize_newtype_struct<T>(
&mut self, _name: &'static str, value: T
) -> Result<()> where T: ser::Serialize {
let header = vec![BertTag::SmallTuple as u8];
try!(self.writer.write_all(header.as_slice()));
let structure_name_atom = self.get_atom(_name);
try!(self.writer.write_all(structure_name_atom.as_slice()));
value.serialize(self)
}
And simple test for this stuff:
#[test]
fn serialize_newtype_struct() {
struct Point(i32, i32);
let point = Point(1, 2);
assert_eq!(
term_to_binary(&point).unwrap(),
// ... some result there
)
}
term_to_binary
function it's no more than a little wrapper, which let so use BERT serializer more easier way. It has the next signature:
/// Encode the passed value into a `[u8]` writer.
#[inline]
pub fn to_writer<W, T>(
writer: &mut W, value: &T
) -> Result<()> where W: io::Write, T: ser::Serialize {
let mut ser = Serializer::new(writer);
try!(value.serialize(&mut ser));
Ok(())
}
/// Encode the specified struct into a `[u8]` buffer.
#[inline]
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>> where T: ser::Serialize {
let mut writer = Vec::with_capacity(128);
try!(to_writer(&mut writer, value));
Ok(writer)
}
/// Convert passed value to a BERT representation.
#[inline]
pub fn term_to_binary<T> (
value: &T
) -> Result<Vec<u8>> where T: ser::Serialize {
let mut binary = vec![EXT_VERSION];
let data = try!(to_vec(value));
binary.extend(data.iter().clone());
Ok(binary)
}
So, how can I invoke the serialize_newtype_struct
method with passed into term_to_binary newtype struct from test?