From 7464ba72725417bfdaa0cb737daf0a55b0e98509 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Mon, 20 Nov 2017 18:55:34 +0100 Subject: [PATCH] Allow serialized types to use a more compact representation ... (#217) ... by utilizing that bincode is not human readable. Uses the changes in https://github.com/serde-rs/serde/pull/1044 which allows data formats to report that they are not human readable. This lets certain types serialize themselves into a more compact form as they know that the serialized form does not need to be readable. Closes #215 BREAKING CHANGE This changes how types serialize themselves if they detect the `is_human_readable` state. --- Cargo.toml | 5 +++-- src/de/mod.rs | 4 ++++ src/ser/mod.rs | 8 ++++++++ tests/test.rs | 9 +++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0c7ea9a..dbad94d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,8 +18,9 @@ description = "A binary serialization / deserialization strategy that uses Serde [dependencies] byteorder = "1.0.0" -serde = "1.*.*" +serde = "1.0.16" [dev-dependencies] serde_bytes = "0.10.*" -serde_derive = "1.*.*" +serde_derive = "1.0.16" + diff --git a/src/de/mod.rs b/src/de/mod.rs index 335995f..284d5fd 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -407,6 +407,10 @@ where let message = "Bincode does not support Deserializer::deserialize_ignored_any"; Err(Error::custom(message)) } + + fn is_human_readable(&self) -> bool { + false + } } impl<'de, 'a, R, S, E> serde::de::VariantAccess<'de> for &'a mut Deserializer diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 34e5112..b08067a 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -203,6 +203,10 @@ impl<'a, W: Write, E: ByteOrder> serde::Serializer for &'a mut Serializer ) -> Result<()> { self.serialize_u32(variant_index) } + + fn is_human_readable(&self) -> bool { + false + } } pub struct SizeChecker { @@ -392,6 +396,10 @@ impl<'a, S: SizeLimit> serde::Serializer for &'a mut SizeChecker { try!(self.add_value(variant_index)); value.serialize(self) } + + fn is_human_readable(&self) -> bool { + false + } } #[doc(hidden)] diff --git a/tests/test.rs b/tests/test.rs index 4f5eee2..79d8ca6 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -457,3 +457,12 @@ fn test_zero_copy_parse() { assert_eq!(out, f); } } + +#[test] +fn not_human_readable() { + use std::net::Ipv4Addr; + let ip = Ipv4Addr::new(1, 2, 3, 4); + the_same(ip); + assert_eq!(&ip.octets()[..], &serialize_little(&ip, Infinite).unwrap()[..]); + assert_eq!(::std::mem::size_of::() as u64, serialized_size(&ip)); +}