Repository: pyfisch/cbor
Pull request #116 (2019-06-09 13:53:30)

No canonical order
See #115 


Patch:
diff --git a/src/ser.rs b/src/ser.rs
index 3e4e2e5..6d62843 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -233,29 +233,6 @@ where
         }
     }
 
-    #[cfg(feature = "std")]
-    fn serialize_with_same_settings<V: Serialize>(&self, v: V) -> Result<Vec<u8>> {
-        let buf: Vec<u8> = vec![];
-        let mut s = Serializer {
-            writer: buf,
-            packed: self.packed,
-            enum_as_map: self.enum_as_map,
-        };
-        v.serialize(&mut s)?;
-        Ok(s.writer)
-    }
-
-    #[cfg(not(feature = "std"))]
-    fn serialize_with_same_settings<V: Serialize>(&mut self, v: V) -> Result<()> {
-        let mut s = Serializer {
-            writer: &mut self.writer,
-            packed: self.packed,
-            enum_as_map: self.enum_as_map,
-        };
-        v.serialize(&mut s)?;
-        Ok(())
-    }
-
     /// Writes a CBOR self-describe tag to the stream.
     ///
     /// Tagging allows a decoder to distinguish different file formats based on their content
@@ -609,50 +586,6 @@ where
         self.serialize_collection(5, len)
     }
 
-    #[cfg(feature = "std")]
-    fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok>
-    where
-        K: Serialize,
-        V: Serialize,
-        I: IntoIterator<Item = (K, V)>,
-    {
-        use serde::ser::SerializeMap;
-
-        let entry_results = iter
-            .into_iter()
-            .map(|(k, v)| {
-                (
-                    self.serialize_with_same_settings(k),
-                    self.serialize_with_same_settings(v),
-                )
-            })
-            .collect::<Vec<_>>();
-
-        let mut entries = vec![];
-        for (k, v) in entry_results {
-            let (k, v) = (k?, v?);
-            entries.push((k, v));
-        }
-
-        entries.sort_by(|a, b| a.0.cmp(&b.0));
-
-        let serializer = self.serialize_map(Some(entries.len()))?;
-
-        for (key, value) in entries {
-            serializer
-                .ser
-                .writer
-                .write_all(&key)
-                .map_err(|e| e.into())?;
-            serializer
-                .ser
-                .writer
-                .write_all(&value)
-                .map_err(|e| e.into())?;
-        }
-        serializer.end()
-    }
-
     #[cfg(not(feature = "std"))]
     fn collect_str<T: ?Sized>(self, value: &T) -> Result<()>
     where
@@ -669,12 +602,7 @@ where
     #[inline]
     fn serialize_struct(self, _name: &'static str, len: usize) -> Result<StructSerializer<'a, W>> {
         self.write_u64(5, len as u64)?;
-        Ok(StructSerializer {
-            ser: self,
-            idx: 0,
-            #[cfg(feature = "std")]
-            entries: vec![],
-        })
+        Ok(StructSerializer { ser: self, idx: 0 })
     }
 
     #[inline]
@@ -763,62 +691,12 @@ where
     }
 }
 
-#[cfg(feature = "std")]
 #[doc(hidden)]
 pub struct StructSerializer<'a, W> {
     ser: &'a mut Serializer<W>,
     idx: u32,
-    entries: Vec<(Vec<u8>, Vec<u8>)>,
-}
-
-#[cfg(not(feature = "std"))]
-#[doc(hidden)]
-pub struct StructSerializer<'a, W> {
-    ser: &'a mut Serializer<W>,
-    idx: u32,
-}
-
-#[cfg(feature = "std")]
-impl<'a, W> StructSerializer<'a, W>
-where
-    W: Write,
-{
-    #[inline]
-    fn serialize_field_inner<T>(&mut self, key: &'static str, value: &T) -> Result<()>
-    where
-        T: ?Sized + ser::Serialize,
-    {
-        let key_bytes = if self.ser.packed {
-            self.ser.serialize_with_same_settings(self.idx)?
-        } else {
-            self.ser.serialize_with_same_settings(key)?
-        };
-        self.idx += 1;
-        self.entries
-            .push((key_bytes, self.ser.serialize_with_same_settings(value)?));
-        Ok(())
-    }
-
-    #[inline]
-    fn skip_field_inner(&mut self, _: &'static str) -> Result<()> {
-        self.idx += 1;
-        Ok(())
-    }
-
-    #[inline]
-    fn end_inner(mut self) -> Result<()> {
-        self.entries.sort_by(|a, b| a.0.cmp(&b.0));
-        for (k, v) in self.entries {
-            self.ser.writer.write_all(&k).map_err(|e| e.into())?;
-            self.ser.writer.write_all(&v).map_err(|e| e.into())?;
-        }
-        Ok(())
-    }
 }
 
-// Version of `StructSerializer` that does not canonicalize its output, suitable for embedded
-// platforms.
-#[cfg(not(feature = "std"))]
 impl<'a, W> StructSerializer<'a, W>
 where
     W: Write,
@@ -829,11 +707,11 @@ where
         T: ?Sized + ser::Serialize,
     {
         if self.ser.packed {
-            self.ser.serialize_with_same_settings(self.idx)?;
+            self.idx.serialize(&mut *self.ser)?;
         } else {
-            self.ser.serialize_with_same_settings(key)?;
+            key.serialize(&mut *self.ser)?;
         }
-        self.ser.serialize_with_same_settings(value)?;
+        value.serialize(&mut *self.ser)?;
         self.idx += 1;
         Ok(())
     }


Test patch:
diff --git a/tests/bennofs.rs b/tests/bennofs.rs
index 3ada587..44212b5 100644
--- a/tests/bennofs.rs
+++ b/tests/bennofs.rs
@@ -38,8 +38,9 @@ mod std_tests {
 
     #[test]
     fn test() {
-        let serialized = serde_cbor::ser::to_vec_packed(&EXAMPLE).unwrap();
-        let deserialized: Example = serde_cbor::from_slice(&serialized).unwrap();
+        let serialized = serde_cbor::ser::to_vec_packed(&EXAMPLE).expect("bennofs 1");
+        println!("{:?}", serialized);
+        let deserialized: Example = serde_cbor::from_slice(&serialized).expect("bennofs 2");
         assert_eq!(EXAMPLE, deserialized);
     }
 }
diff --git a/tests/ser.rs b/tests/ser.rs
index c7d541a..a9a1443 100644
--- a/tests/ser.rs
+++ b/tests/ser.rs
@@ -82,7 +82,7 @@ mod std_tests {
         object.insert(vec![-2i64], ());
         object.insert(vec![0i64, 0i64], ());
         object.insert(vec![0i64, -1i64], ());
-        let vec = to_vec(&object).unwrap();
+        let vec = to_vec(&serde_cbor::value::to_value(object.clone()).unwrap()).unwrap();
         assert_eq!(
             vec![
                 166, 129, 0, 246, 129, 24, 100, 246, 129, 32, 246, 129, 33, 246, 130, 0, 0, 246,
@@ -112,7 +112,7 @@ mod std_tests {
         for key in keys {
             object.insert(key, ());
         }
-        let vec = to_vec(&object).unwrap();
+        let vec = to_vec(&serde_cbor::value::to_value(object.clone()).unwrap()).unwrap();
         assert_eq!(
             vec![
                 166, 161, 97, 97, 246, 246, 161, 97, 98, 246, 246, 161, 97, 99, 246, 246, 161, 97,
diff --git a/tests/std_types.rs b/tests/std_types.rs
index d226836..3b85183 100644
--- a/tests/std_types.rs
+++ b/tests/std_types.rs
@@ -134,7 +134,7 @@ mod std_tests {
         year_of_birth: 1906,
         profession: Some("computer scientist".to_string()),
     },
-    "a3646e616d656c477261636520486f707065726a70726f66657373696f6e72636f6d707574657220736369656e746973746d796561725f6f665f6269727468190772");
+    "a3646e616d656c477261636520486f707065726d796561725f6f665f62697274681907726a70726f66657373696f6e72636f6d707574657220736369656e74697374");
 
     #[derive(Debug, PartialEq, Deserialize, Serialize)]
     struct OptionalPerson {