|
@@ -22,7 +22,7 @@ pub struct Serializer<T: Write> {
|
|
|
output: T,
|
|
|
}
|
|
|
|
|
|
-pub fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>> {
|
|
|
+pub fn to_vec<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>> {
|
|
|
let mut serializer = Serializer { output: Vec::new()};
|
|
|
value.serialize(&mut serializer)?;
|
|
|
Ok(serializer.output)
|
|
@@ -242,12 +242,14 @@ impl<'a, T: Write> SerializeSeq for &'a mut Serializer<T> {
|
|
|
type Ok = Ok;
|
|
|
type Error = Error;
|
|
|
|
|
|
- fn serialize_element<U: ?Sized>(&mut self, value: &U) -> Result<()> {
|
|
|
- unimplemented!();
|
|
|
+ fn serialize_element<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<()> {
|
|
|
+ value.serialize(&mut **self)?;
|
|
|
+ Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// We don't need to add a marker to the end of the sequence because we know its length.
|
|
|
fn end(self) -> Result<Self::Ok> {
|
|
|
- unimplemented!();
|
|
|
+ Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -376,4 +378,115 @@ mod test {
|
|
|
}
|
|
|
Ok(())
|
|
|
}
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_i32() -> Result<()> {
|
|
|
+ {
|
|
|
+ let buffer = super::to_vec(&1i32)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00, 0x00, 0x00], buffer);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ let value: i32 = -2;
|
|
|
+ let buffer = super::to_vec(&value)?;
|
|
|
+ assert_eq!(vec![0xFE, 0xFF, 0xFF, 0xFF], buffer);
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_i64() -> Result<()> {
|
|
|
+ {
|
|
|
+ let buffer = super::to_vec(&1i64)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buffer);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ let value: i64 = -2;
|
|
|
+ let buffer = super::to_vec(&value)?;
|
|
|
+ assert_eq!(vec![0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], buffer);
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_i128() -> Result<()> {
|
|
|
+ {
|
|
|
+ let buffer = super::to_vec(&1i128)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buffer);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ let value: i128 = -2;
|
|
|
+ let buffer = super::to_vec(&value)?;
|
|
|
+ assert_eq!(vec![0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], buffer);
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_u8() -> Result<()> {
|
|
|
+ let value: u8 = 42;
|
|
|
+ let buffer = super::to_vec(&value)?;
|
|
|
+ assert_eq!(vec![value], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_u16() -> Result<()> {
|
|
|
+ let buffer = super::to_vec(&1u16)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_u32() -> Result<()> {
|
|
|
+ let buffer = super::to_vec(&1u32)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00, 0x00, 0x00], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_u64() -> Result<()> {
|
|
|
+ let buffer = super::to_vec(&1u64)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_u128() -> Result<()> {
|
|
|
+ let buffer = super::to_vec(&1u128)?;
|
|
|
+ assert_eq!(vec![0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_f32() -> Result<()> {
|
|
|
+ let buffer = super::to_vec(&0.15625f32)?;
|
|
|
+ assert_eq!(vec![0x00, 0x00, 0x20, 0x3E], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_f64() -> Result<()> {
|
|
|
+ let buffer = super::to_vec(&1f64)?;
|
|
|
+ assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_char() -> Result<()> {
|
|
|
+ let c: char = '*';
|
|
|
+ let buffer = super::to_vec(&c)?;
|
|
|
+ assert_eq!(vec![42], buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_bytes() -> Result<()> {
|
|
|
+ let mut bytes: Vec<u8> = vec![41, 23, 72, 61];
|
|
|
+ let buffer = super::to_vec(bytes.as_slice())?;
|
|
|
+ let length = bytes.len() as u32;
|
|
|
+ let mut expected = length.to_le_bytes().to_vec();
|
|
|
+ expected.append(&mut bytes);
|
|
|
+ assert_eq!(expected, buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
}
|