|
@@ -23,84 +23,160 @@ impl<'a, T: Write> ser::Serializer for &'a Serializer<T> {
|
|
|
type SerializeStruct = Self;
|
|
|
type SerializeStructVariant = Self;
|
|
|
|
|
|
+ /// A bool is serialized by writing the byte 1 if true and 0 if false.
|
|
|
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
|
|
|
- self.output.write(&[if v { 1 } else { 0 }]).map_error()?;
|
|
|
+ self.output.write_all(&[if v { 1 } else { 0 }]).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The output format of a signed byte is two's complement, so we can just output
|
|
|
+ /// Rust's binary representation.
|
|
|
fn serialize_i8(self, v: i8) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The output format of a signed integer is two's complement, so we can just output
|
|
|
+ /// Rust's binary representation in little endian order.
|
|
|
fn serialize_i16(self, v: i16) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The output format of a signed integer is two's complement, so we can just output
|
|
|
+ /// Rust's binary representation in little endian order.
|
|
|
fn serialize_i32(self, v: i32) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The output format of a signed integer is two's complement, so we can just output
|
|
|
+ /// Rust's binary representation in little endian order.
|
|
|
fn serialize_i64(self, v: i64) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The output format of a signed integer is two's complement, so we can just output
|
|
|
+ /// Rust's binary representation in little endian order.
|
|
|
fn serialize_i128(self, v: i128) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The given byte is written directly to the output.
|
|
|
fn serialize_u8(self, v: u8) -> Result<Self::Ok> {
|
|
|
- self.output.write(&[v]).map_error()?;
|
|
|
+ self.output.write_all(&[v]).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The underlying bytes of the given unsigned integer are written to the output in little
|
|
|
+ /// endian order.
|
|
|
fn serialize_u16(self, v: u16) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The underlying bytes of the given unsigned integer are written to the output in little
|
|
|
+ /// endian order.
|
|
|
fn serialize_u32(self, v: u32) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The underlying bytes of the given unsigned integer are written to the output in little
|
|
|
+ /// endian order.
|
|
|
fn serialize_u64(self, v: u64) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The underlying bytes of the given unsigned integer are written to the output in little
|
|
|
+ /// endian order.
|
|
|
fn serialize_u128(self, v: u128) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// Since the output format is IEEE 754, we can just write the underlying bytes to the output
|
|
|
+ /// in little endian order.
|
|
|
fn serialize_f32(self, v: f32) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// Since the output format is IEEE 754, we can just write the underlying bytes to the output
|
|
|
+ /// in little endian order.
|
|
|
fn serialize_f64(self, v: f64) -> Result<Self::Ok> {
|
|
|
- self.output.write(&v.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(&v.to_le_bytes()).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// The given char is cast to a u8 then written to the output.
|
|
|
fn serialize_char(self, c: char) -> Result<Self::Ok> {
|
|
|
- self.output.write(&[c as u8]).map_error()?;
|
|
|
+ self.output.write_all(&[c as u8]).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// A slice of bytes is stored by first writing its length (in LE order) and then the slice.
|
|
|
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok> {
|
|
|
let len = v.len() as u32;
|
|
|
- self.output.write(&len.to_le_bytes()).map_error()?;
|
|
|
- self.output.write(v).map_error()?;
|
|
|
+ self.output.write_all(&len.to_le_bytes()).map_error()?;
|
|
|
+ self.output.write_all(v).map_error()?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+ /// A str is just serialized as a sequence of UTF8 bytes.
|
|
|
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
|
|
|
self.serialize_bytes(v.as_bytes())
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ /// The none variant is stored as a zero byte.
|
|
|
+ fn serialize_none(self) -> Result<Self::Ok> {
|
|
|
+ self.output.write_all(&[0]).map_error()?;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// A some variant is just stored using the representation of its value.
|
|
|
+ fn serialize_some<U: ?Sized + Serialize>(self, value: &U) -> Result<Self::Ok> {
|
|
|
+ value.serialize(self)?;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// The unit is a type which can be represented with zero bytes, so we faithfully represent it
|
|
|
+ /// as nothing.
|
|
|
+ fn serialize_unit(self) -> Result<()> {
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Forwards to serialize_unit.
|
|
|
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> {
|
|
|
+ self.serialize_unit()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// The index of the unit variant is written to the output.
|
|
|
+ fn serialize_unit_variant(
|
|
|
+ self, _name: &'static str, variant_index: u32, _variant: &'static str
|
|
|
+ ) -> Result<Self::Ok> {
|
|
|
+ self.serialize_u32(variant_index)?;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// The value of the newtype struct is serialized and its name is ignored.
|
|
|
+ fn serialize_newtype_struct<U: ?Sized + Serialize>(
|
|
|
+ self, _name: &'static str, value: &U
|
|
|
+ ) -> Result<Self::Ok> {
|
|
|
+ value.serialize(self)?;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// The index of the variant is serialized and written out, followed by the serialization of
|
|
|
+ /// its value.
|
|
|
+ fn serialize_newtype_variant<U: ?Sized + Serialize>(
|
|
|
+ self, _name: &'static str, variant_index: u32, _variant: &'static str, value: &U
|
|
|
+ ) -> Result<Self::Ok> {
|
|
|
+ self.serialize_u32(variant_index)?;
|
|
|
+ value.serialize(self)?;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+}
|