From a322a530fb4acc72bbe699e2314b2c7c094cff4e Mon Sep 17 00:00:00 2001 From: Boog900 <108027008+Boog900@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:52:42 +0000 Subject: [PATCH] Fix empty seq (#58) * fix serialization of empty sequences. * add a test for empty sequences. * update changelog + fmt --- CHANGELOG.md | 1 + src/ser.rs | 9 ++++++--- tests/other.rs | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/other.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index dd210bb..ba77dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed serialization of empty sequences ([#58](https://github.com/monero-rs/monero-epee-bin-serde/pull/58)) - No longer panic on unknown fields ([#46](https://github.com/monero-rs/monero-epee-bin-serde/pull/46)) - Deserialization of nested structs ([#37](https://github.com/monero-rs/monero-epee-bin-serde/pull/37)). diff --git a/src/ser.rs b/src/ser.rs index e1c94c1..26e409e 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -216,9 +216,12 @@ impl<'a, 'b> serde::Serializer for &'a mut Serializer<'b> { } fn serialize_seq(self, len: Option) -> Result { - self.state = State::First { - length: len.ok_or_else(Error::no_length)?, - }; + let len = len.ok_or_else(Error::no_length)?; + self.state = State::First { length: len }; + + if len == 0 { + self.write_marker(Marker::Sequence { element: 255 })?; + } Ok(self) } diff --git a/tests/other.rs b/tests/other.rs new file mode 100644 index 0000000..2a0862f --- /dev/null +++ b/tests/other.rs @@ -0,0 +1,14 @@ +use monero_epee_bin_serde::{from_bytes, to_bytes}; +use serde::{Deserialize, Serialize}; + +#[derive(Default, Deserialize, Serialize, PartialEq, Debug)] +struct TestStruct { + seq: Vec, +} + +#[test] +fn empty_sequence() { + let obj = TestStruct::default(); + let data = to_bytes(&obj).unwrap(); + assert_eq!(obj, from_bytes(data).unwrap()); +}