Skip to content

RUST-2217 Optionally support bson crate 3.0 #1380

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

Merged
merged 8 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .evergreen/aws-lambda-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ To deploy the application, you need the folllowing tools:

* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
* [Rust](https://www.rust-lang.org/) version 1.74.0 or newer
* [Rust](https://www.rust-lang.org/) version 1.81.0 or newer
* [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda) for cross-compilation

To build and deploy your application for the first time, run the following in your shell:
Expand Down
5 changes: 1 addition & 4 deletions .evergreen/compile-only.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ source ./.evergreen/env.sh
if [ "$RUST_VERSION" != "" ]; then
rustup toolchain install $RUST_VERSION
TOOLCHAIN="+${RUST_VERSION}"
# Remove the local git dependencies for bson and mongocrypt, which don't work properly with the MSRV resolver.
sed -i "s/bson =.*/bson = \"2\"/" Cargo.toml
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I accidentally broke these by changing the formatting of Cargo.toml to no longer match, but AFAICT the MSRV resolver is working fine with the git dependencies now.

I'm not sure if this would have been a viable solution as we progress with bson 3.0 since there's no published version yet to fall back on, so hopefully I'm not missing a failure mode here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@isabelatkinson I'd like your input here - I can't find any brokenness but I'm deeply wary of things that work when I don't expect them to :)

Copy link
Contributor

Choose a reason for hiding this comment

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

This originally had to be fixed when the driver's MSRV (1.74) was less than bson's (1.81) - the MSRV resolver couldn't look back for an older MSRV-compatible version with a git dependency instead of a published version dependency. That problem goes away with the driver's MSRV being >= bson's MSRV (which is done in this PR) since an older version doesn't need to be selected, but it will come up again if we bump bson without bumping the driver. I think we can just figure that out if/when it comes up again, though - we should be able to do something similar once 3.0 is actually published.

sed -i "s/mongocrypt =.*/mongocrypt = { version = \"0.2\", optional = true }/" Cargo.toml
CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=fallback cargo +nightly -Zmsrv-policy generate-lockfile
fi

Expand All @@ -22,4 +19,4 @@ cargo $TOOLCHAIN build
cargo $TOOLCHAIN build --all-features

# Test with no default features.
cargo $TOOLCHAIN build --no-default-features --features compat-3-0-0,rustls-tls
cargo $TOOLCHAIN build --no-default-features --features compat-3-3-0,bson-3,rustls-tls
2 changes: 1 addition & 1 deletion .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ tasks:
- func: "compile only"
vars:
# Our minimum supported Rust version. This should be updated whenever the MSRV is bumped.
RUST_VERSION: 1.74.0
RUST_VERSION: 1.81.0

- name: check-cargo-deny
commands:
Expand Down
38 changes: 31 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ license = "Apache-2.0"
readme = "README.md"
name = "mongodb"
version = "3.2.3"
rust-version = "1.74"
rust-version = "1.81"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needed to change because bson's msrv at head is 1.81; when doing an msrv compile against 2.x it would work because the crates.io published version had an msrv of 1.48, but there's no published version for 3.x so it needs to use the one from git.


exclude = [
"etc/**",
Expand All @@ -30,7 +30,10 @@ exclude = [

[features]
default = ["compat-3-0-0", "rustls-tls", "dns-resolver"]
compat-3-0-0 = []
compat-3-0-0 = ["compat-3-3-0", "bson-2"]
compat-3-3-0 = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping a rolling compat-VERSION feature allows us to do this again for other things if we need to, e.g. if we need to go back to supporting multiple async runtimes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. That seems like a good precaution in case there is a future need to make a required feature optional.

bson-2 = ["dep:bson2", "mongocrypt/bson-2"]
bson-3 = ["dep:bson3", "mongocrypt/bson-3"]
sync = []
rustls-tls = ["dep:rustls", "dep:tokio-rustls"]
openssl-tls = ["dep:openssl", "dep:openssl-probe", "dep:tokio-openssl"]
Expand Down Expand Up @@ -73,7 +76,6 @@ tracing-unstable = ["dep:tracing", "dep:log"]
async-trait = "0.1.42"
base64 = "0.13.0"
bitflags = "1.1.0"
bson = { git = "https://github.com/mongodb/bson-rust", branch = "main", version = "2.14.0" }
chrono = { version = "0.4.7", default-features = false, features = [
"clock",
"std",
Expand All @@ -92,7 +94,6 @@ hmac = "0.12.1"
once_cell = "1.19.0"
log = { version = "0.4.17", optional = true }
md-5 = "0.10.1"
mongocrypt = { git = "https://github.com/mongodb/libmongocrypt-rust.git", branch = "main", optional = true, version = "0.2.1" }
mongodb-internal-macros = { path = "macros", version = "3.2.3" }
num_cpus = { version = "1.13.1", optional = true }
openssl = { version = "0.10.38", optional = true }
Expand Down Expand Up @@ -120,6 +121,27 @@ zstd = { version = "0.11.2", optional = true }
macro_magic = "0.5.1"
rustversion = "1.0.20"

[dependencies.bson2]
git = "https://github.com/mongodb/bson-rust"
branch = "2.15.x"
package = "bson"
version = "2.15.0"
optional = true

[dependencies.bson3]
git = "https://github.com/mongodb/bson-rust"
branch = "main"
package = "bson"
version = "3.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly out of curiosity: will this break if we try to release the driver before bson 3.0 is released?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cargo publish will fail since there's no non-git version, yeah. If we do need to do a driver release before 3.0 we can either push a patch to the release branch that strips out the bson-3 feature entirely or we could push an alpha release of the bson crate to cargo.

optional = true

[dependencies.mongocrypt]
git = "https://github.com/mongodb/libmongocrypt-rust.git"
branch = "main"
version = "0.3.0"
default-features = false
optional = true

[dependencies.pbkdf2]
version = "0.11.0"
default-features = false
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ For more details, including features, runnable examples, troubleshooting resourc

## Installation
### Requirements
- Rust 1.74.0+ (See the [MSRV policy](#minimum-supported-rust-version-msrv-policy) for more information)
- Rust 1.81.0+ (See the [MSRV policy](#minimum-supported-rust-version-msrv-policy) for more information)
- MongoDB 4.0+

#### Supported Platforms
Expand Down Expand Up @@ -149,7 +149,7 @@ Commits to main are run automatically on [evergreen](https://evergreen.mongodb.c

## Minimum supported Rust version (MSRV) policy

The MSRV for this crate is currently 1.74.0. Increases to the MSRV will only happen in a minor or major version release, and will be to a Rust version at least six months old.
The MSRV for this crate is currently 1.81.0. Increases to the MSRV will only happen in a minor or major version release, and will be to a Rust version at least six months old.

## License

Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.74.0"
msrv = "1.81.0"
3 changes: 1 addition & 2 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ pub trait Action: private::Sealed + IntoFuture {
/// If the value is `Some`, call the provided function on `self`. Convenient for chained
/// updates with values that need to be set conditionally. For example:
/// ```rust
/// # use mongodb::{Client, error::Result};
/// # use bson::Document;
/// # use mongodb::{Client, error::Result, bson::Document};
/// use mongodb::action::Action;
/// async fn list_my_collections(client: &Client, filter: Option<Document>) -> Result<Vec<String>> {
/// client.database("my_db")
Expand Down
2 changes: 1 addition & 1 deletion src/action/aggregate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{marker::PhantomData, time::Duration};

use bson::{Bson, Document};
use crate::bson::{Bson, Document};

use crate::{
coll::options::{AggregateOptions, Hint},
Expand Down
2 changes: 1 addition & 1 deletion src/action/count.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{Bson, Document};
use crate::bson::{Bson, Document};
use std::time::Duration;

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/action/create_collection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{Bson, Document};
use crate::bson::{Bson, Document};
use std::time::Duration;

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/action/create_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{marker::PhantomData, time::Duration};

use bson::Bson;
use crate::bson::Bson;

use crate::{
coll::options::{CommitQuorum, CreateIndexOptions},
Expand Down
2 changes: 1 addition & 1 deletion src/action/csfle/create_encrypted_collection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use bson::{doc, Bson, Document};
use crate::bson::{doc, Bson, Document};

use crate::{
action::{action_impl, export_doc, option_setters, options_doc},
Expand Down
6 changes: 3 additions & 3 deletions src/action/csfle/encrypt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{Binary, Bson, RawDocumentBuf};
use crate::bson::{Binary, Bson, RawDocumentBuf};
use macro_magic::export_tokens;
use mongocrypt::ctx::Algorithm;
use serde::Serialize;
Expand All @@ -22,7 +22,7 @@ impl ClientEncryption {
#[options_doc(encrypt)]
pub fn encrypt(
&self,
value: impl Into<bson::RawBson>,
value: impl Into<crate::bson::RawBson>,
key: impl Into<EncryptKey>,
algorithm: Algorithm,
) -> Encrypt {
Expand Down Expand Up @@ -101,7 +101,7 @@ pub struct Encrypt<'a, Mode = Value> {
}

pub struct Value {
pub(crate) value: bson::RawBson,
pub(crate) value: crate::bson::RawBson,
}

pub struct Expression {
Expand Down
2 changes: 1 addition & 1 deletion src/action/delete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{Bson, Document};
use crate::bson::{Bson, Document};

use crate::{
coll::options::{DeleteOptions, Hint},
Expand Down
2 changes: 1 addition & 1 deletion src/action/distinct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use bson::{Bson, Document};
use crate::bson::{Bson, Document};

use crate::{
coll::options::{DistinctOptions, Hint},
Expand Down
2 changes: 1 addition & 1 deletion src/action/drop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "in-use-encryption")]
use bson::Document;
use crate::bson::Document;

use crate::{
coll::options::DropCollectionOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/action/drop_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use bson::Bson;
use crate::bson::Bson;

use crate::{
coll::options::DropIndexOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/action/find.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use bson::{Bson, Document};
use crate::bson::{Bson, Document};
use serde::de::DeserializeOwned;

use crate::{
Expand Down
4 changes: 2 additions & 2 deletions src/action/find_and_modify.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Borrow, time::Duration};

use bson::{Bson, Document, RawDocumentBuf};
use crate::bson::{Bson, Document, RawDocumentBuf};
use serde::{de::DeserializeOwned, Serialize};

use crate::{
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<T: Serialize + DeserializeOwned + Send + Sync> Collection<T> {
FindOneAndReplace {
coll: self,
filter,
replacement: bson::to_raw_document_buf(replacement.borrow()).map_err(Into::into),
replacement: crate::bson::to_raw_document_buf(replacement.borrow()).map_err(Into::into),
options: None,
session: None,
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/gridfs/delete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{doc, Bson};
use crate::bson::{doc, Bson};

#[cfg(docsrs)]
use crate::gridfs::FilesCollectionDocument;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'a> Action for DeleteByName<'a> {
.find(doc! { "filename": self.filename.clone() })
.projection(doc! { "_id": 1 })
.await?
.with_type::<bson::Document>()
.with_type::<crate::bson::Document>()
.map(|r| match r {
Ok(mut d) => d
.remove("_id")
Expand Down
2 changes: 1 addition & 1 deletion src/action/gridfs/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{doc, Bson};
use crate::bson::{doc, Bson};

use crate::{
action::{action_impl, deeplink, export_doc, option_setters, options_doc},
Expand Down
2 changes: 1 addition & 1 deletion src/action/gridfs/find.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use bson::Document;
use crate::bson::Document;

use crate::{
action::{action_impl, deeplink, export_doc, option_setters, options_doc},
Expand Down
2 changes: 1 addition & 1 deletion src/action/gridfs/rename.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{doc, Bson};
use crate::bson::{doc, Bson};

use crate::{
action::action_impl,
Expand Down
2 changes: 1 addition & 1 deletion src/action/gridfs/upload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{oid::ObjectId, Bson, Document};
use crate::bson::{oid::ObjectId, Bson, Document};

#[cfg(docsrs)]
use crate::gridfs::FilesCollectionDocument;
Expand Down
4 changes: 2 additions & 2 deletions src/action/insert_many.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Borrow, collections::HashSet, ops::Deref};

use bson::{Bson, RawDocumentBuf};
use crate::bson::{Bson, RawDocumentBuf};
use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -34,7 +34,7 @@ impl<T: Serialize + Send + Sync> Collection<T> {
coll: CollRef::new(self),
docs: docs
.into_iter()
.map(|v| bson::to_raw_document_buf(v.borrow()).map_err(Into::into))
.map(|v| crate::bson::to_raw_document_buf(v.borrow()).map_err(Into::into))
.collect(),
options: None,
session: None,
Expand Down
4 changes: 2 additions & 2 deletions src/action/insert_one.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Borrow, ops::Deref};

use bson::{Bson, RawDocumentBuf};
use crate::bson::{Bson, RawDocumentBuf};
use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -32,7 +32,7 @@ impl<T: Serialize + Send + Sync> Collection<T> {
pub fn insert_one(&self, doc: impl Borrow<T>) -> InsertOne {
InsertOne {
coll: CollRef::new(self),
doc: bson::to_raw_document_buf(doc.borrow()).map_err(Into::into),
doc: crate::bson::to_raw_document_buf(doc.borrow()).map_err(Into::into),
options: None,
session: None,
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/list_collections.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use bson::{Bson, Document};
use crate::bson::{Bson, Document};
use futures_util::TryStreamExt;

use crate::{
Expand Down
Loading