Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Ordering feature for bson serialization #490

Closed
mahmoud-moursy opened this issue Aug 17, 2024 · 2 comments
Closed

[feat] Ordering feature for bson serialization #490

mahmoud-moursy opened this issue Aug 17, 2024 · 2 comments
Assignees

Comments

@mahmoud-moursy
Copy link

I was trying to cryptographically sign some BSON documents using the dryoc crate, and noticed that, sometimes my tests would pass, other times they would somehow fail!? And when I made some seemingly random changes to the code, it would start working again!?

It turns out the root cause was that, when I was serializing the same data, it was not always in the same order, causing the signature to be different! 😮

I guess that's on me for trying this with an unstructured data format, but it would be nice to have a feature that automatically sorts entries. I currently hacked together a to_sorted function that sorts the entries using &str's handy built-in Cmp.

@abr-egn
Copy link
Contributor

abr-egn commented Aug 19, 2024

Yep, the Document type is backed by a standard HashMap, which is nondeterministic in iteration order. Would the RawDocument type work for your purposes? Iteration there is deterministic (it's whatever order the serialized data is in).

@mahmoud-moursy
Copy link
Author

I fixed my problems just using a BTreeMap without needing any extra trickery 😁. BTreeMap keeps everything in a deterministic order thanks to its structure, and that seems to carry over when serializing it into a Document 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants