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

RUST-2027 Impl Hash/Eq for BSON #495

Merged
merged 6 commits into from
Sep 6, 2024
Merged

RUST-2027 Impl Hash/Eq for BSON #495

merged 6 commits into from
Sep 6, 2024

Conversation

NineLord
Copy link
Contributor

@NineLord NineLord commented Sep 3, 2024

#494
Summery of changes:

  • Added feature hashable to require the user to choose to opt-in using Bson as hash key.
    Although, serde_json didn't bother with having this behind the feature flag, so If you like I can remove it.
  • Bson::Double(f64) - implemented the same solution as serde_json.
  • Bson::Document(Document) - implemented the same solution as serde_json.
  • Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope) - Able to impl Hash/Eq only if Document is impl Hash/Eq.
  • Bson::{RegularExpression(Regex), Binary(Binary), Decimal128(Decimal128), DbPointer(DbPointer)} - Added Derive Hash/Eq without feature flag hashable, no reason for it not to implement those by default.
  • Added unit test to show that using Bson as key works.

I didn't implement it yet, but if the above feature is acceptable,
it might be worth adding another feature like not(preserve_order) from serde_json,
to change Bson::Document to use BTreeMap instead of indexmap for more efficient hashing (like serde_json).
I don't mind adding this as a separate PR.

* Added feature `hashable` (and to README.md).
* `Bson::Double` - implemented the same solution as [`serde_json`](https://docs.rs/serde_json/1.0.127/src/serde_json/number.rs.html#53-70).
* `Bson::Document(Document)` - implemented the same solution as [`serde_json`](https://docs.rs/serde_json/1.0.127/src/serde_json/map.rs.html#395-397).
* `Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope)` - Able to impl Hash/Eq only if Document is impl Hash/Eq.
* `Bson::{RegularExpression(Regex), Binary(Binary), Decimal128(Decimal128), DbPointer(DbPointer)}` - Added Derive Hash/Eq without feature flag `hashable`, no reason for it not to implement those by default.
@abr-egn abr-egn self-assigned this Sep 3, 2024
@abr-egn abr-egn self-requested a review September 3, 2024 18:04
@abr-egn
Copy link
Contributor

abr-egn commented Sep 3, 2024

Thank you for the well-thought-out PR! The changes look good to me and I've started a test run; once the tests pass and this is reviewed by another team member we'll merge it in.

@abr-egn
Copy link
Contributor

abr-egn commented Sep 3, 2024

On the topic of switching Document to use BTreeMap, I'm a bit more hesitant, but if you have the time to put together a PR and a basic benchmark it's certainly something we'd consider 🙂

@abr-egn abr-egn changed the title Impl Hash/Eq for BSON RUST-2027 Impl Hash/Eq for BSON Sep 3, 2024
Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few docs suggestions, otherwise looks good. thanks for your contribution!

Cargo.toml Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
NineLord and others added 2 commits September 5, 2024 08:49
@isabelatkinson isabelatkinson merged commit 28e3925 into mongodb:main Sep 6, 2024
11 checks passed
@NineLord NineLord deleted the feature-hashable-bson branch September 8, 2024 06:06
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

Successfully merging this pull request may close these issues.

3 participants