Skip to content

Commit 62fb407

Browse files
committed
Don't allow container map keys
1 parent 22acfdc commit 62fb407

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/serializer.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,15 @@ impl<'a, 'w, W: io::Write> SerializeMap for &'a mut SeHeader<'w, W> {
744744
}
745745

746746
self.wtr.check_map_key(key)?;
747+
self.state = HeaderState::InStructField;
748+
key.serialize(&mut **self)?; // This does not actually serialize anything, just checks that the key is a scalar value.
749+
if let HeaderState::ErrorIfWrite(err) =
750+
mem::replace(&mut self.state, HeaderState::InStructField)
751+
{
752+
return Err(err);
753+
}
747754
let mut key_serializer = SeRecord { wtr: self.wtr };
748755
key.serialize(&mut key_serializer)?;
749-
self.state = HeaderState::InStructField;
750756
Ok(())
751757
}
752758

@@ -1154,6 +1160,24 @@ mod tests {
11541160
assert_eq!(got, "a,b");
11551161
}
11561162

1163+
#[test]
1164+
fn ordered_map_with_collection_as_key() {
1165+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
1166+
struct MyKey {
1167+
name: &'static str,
1168+
other_attribute: u8,
1169+
}
1170+
1171+
let mut map = BTreeMap::new();
1172+
map.insert(MyKey { name: "a", other_attribute: 1 }, 2.0);
1173+
1174+
let error = serialize_header_err(map);
1175+
assert!(
1176+
matches!(error.kind(), ErrorKind::Serialize(_)),
1177+
"Expected ErrorKind::Serialize but got '{error}'"
1178+
);
1179+
}
1180+
11571181
#[test]
11581182
fn unordered_map() {
11591183
let mut writer = Writer::from_writer(vec![]);

0 commit comments

Comments
 (0)