Skip to content

Commit

Permalink
Fix empty seq (#58)
Browse files Browse the repository at this point in the history
* fix serialization of empty sequences.

* add a test for empty sequences.

* update changelog + fmt
  • Loading branch information
Boog900 authored Oct 5, 2023
1 parent 3d08376 commit a322a53
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

Expand Down
9 changes: 6 additions & 3 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,12 @@ impl<'a, 'b> serde::Serializer for &'a mut Serializer<'b> {
}

fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
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)
}
Expand Down
14 changes: 14 additions & 0 deletions tests/other.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
}

#[test]
fn empty_sequence() {
let obj = TestStruct::default();
let data = to_bytes(&obj).unwrap();
assert_eq!(obj, from_bytes(data).unwrap());
}

0 comments on commit a322a53

Please sign in to comment.