Closed annmuor closed 7 months ago
Hi @annmuor, thanks for opening this issue! The problem you're experiencing is due to the fact that a HashMap
will serialize to a Document
when converting to BSON, which only supports string keys. I added the following lines to your test case:
let doc: Document = bson::from_slice(&buf).unwrap();
dbg!("{}", doc);
and the output printed was:
doc = Document({
"field": Document({
"127.0.0.1": Int64(
10,
),
}),
})
which differs from how a Vec<IpAddr>
will be serialized because our serializer needs to serialize the IpAddr
keys as strings.
When you call bson::from_slice
with the buffer you've created, our raw BSON deserializer will try to deserialize the key string into the specified type, which is IpAddr
in your case. I looked into the Deserialize
implementation for IpAddr
, and it looks like deserializing from a string is only supported when the deserializer is human readable. Our deserializer for raw BSON is not human readable, so the deserializer tries to deserialize an enum and fails when it encounters a string. That said, our non-raw BSON deserializer is human readable, so changing the last two lines in your test to the following will work just fine:
let doc = bson::to_document(&test).unwrap();
let _: Test = bson::from_document(doc).unwrap();
I recommend using this approach instead to fix the error you're encountering. If that's not feasible, you'll likely need to write a custom deserialization method for your HashMap<IpAddr, usize>
field and use it in a deserialize_with
attribute. I can help out with writing that method if needed. Please let me know if you have any further questions!
There has not been any recent activity on this ticket, so we are marking it as stale. If we do not hear anything further from you, this issue will be automatically closed in one week.
Thank you very much.
Versions/Environment
Describe the bug
A clear and concise description of what the bug is.
bson encoder fails to properly encode IpAddr ( 2-variant enum ) into HashMap. But it works fine with Vec.
To Reproduce