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 7 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
28 changes: 25 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
5 changes: 3 additions & 2 deletions src/action/list_databases.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};

#[cfg(feature = "sync")]
use crate::sync::Client as SyncClient;
Expand Down Expand Up @@ -106,7 +106,8 @@ impl<'a> Action for ListDatabases<'a, ListSpecifications> {
.and_then(|dbs| {
dbs.into_iter()
.map(|db_spec| {
bson::from_slice(db_spec.as_bytes()).map_err(crate::error::Error::from)
crate::bson::from_slice(db_spec.as_bytes())
.map_err(crate::error::Error::from)
})
.collect()
})
Expand Down
2 changes: 1 addition & 1 deletion src/action/list_indexes.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 futures_util::stream::TryStreamExt;

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

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

use crate::{
Expand Down Expand Up @@ -31,7 +31,7 @@ impl<T: Serialize + Send + Sync> Collection<T> {
ReplaceOne {
coll: CollRef::new(self),
query,
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
6 changes: 3 additions & 3 deletions src/action/run_command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

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

use crate::{
client::session::TransactionState,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl crate::sync::Database {
#[must_use]
pub struct RunCommand<'a> {
db: &'a Database,
command: bson::raw::Result<RawDocumentBuf>,
command: crate::bson::raw::Result<RawDocumentBuf>,
options: Option<RunCommandOptions>,
session: Option<&'a mut ClientSession>,
}
Expand Down Expand Up @@ -214,7 +214,7 @@ impl<'a> Action for RunCommand<'a> {
#[must_use]
pub struct RunCursorCommand<'a, Session = ImplicitSession> {
db: &'a Database,
command: bson::raw::Result<RawDocumentBuf>,
command: crate::bson::raw::Result<RawDocumentBuf>,
options: Option<RunCursorCommandOptions>,
session: Session,
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/search_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

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

use super::{
action_impl,
Expand Down
2 changes: 1 addition & 1 deletion src/action/update.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::{Hint, UpdateModifications, UpdateOptions},
Expand Down
2 changes: 1 addition & 1 deletion src/action/watch.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, Timestamp};
use crate::bson::{Bson, Document, Timestamp};
use serde::de::DeserializeOwned;

use super::{
Expand Down
4 changes: 2 additions & 2 deletions src/bson_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub(crate) fn to_raw_bson_array(docs: &[Document]) -> Result<RawBson> {
pub(crate) fn to_raw_bson_array_ser<T: Serialize>(values: &[T]) -> Result<RawBson> {
let mut array = RawArrayBuf::new();
for value in values {
array.push(bson::to_raw_document_buf(value)?);
array.push(crate::bson::to_raw_document_buf(value)?);
}
Ok(RawBson::Array(array))
}
Expand Down Expand Up @@ -213,7 +213,7 @@ pub(crate) fn append_ser(
struct Helper<T> {
value: T,
}
let raw_doc = bson::to_raw_document_buf(&Helper { value })?;
let raw_doc = crate::bson::to_raw_document_buf(&Helper { value })?;
this.append_ref(
key,
raw_doc
Expand Down
6 changes: 3 additions & 3 deletions src/change_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::{
};

#[cfg(test)]
use bson::RawDocumentBuf;
use bson::{Document, Timestamp};
use crate::bson::RawDocumentBuf;
use crate::bson::{Document, Timestamp};
use derive_where::derive_where;
use futures_core::{future::BoxFuture, Stream};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -158,7 +158,7 @@ where
/// ```
pub async fn next_if_any(&mut self) -> Result<Option<T>> {
Ok(match NextInBatchFuture::new(self).await? {
BatchValue::Some { doc, .. } => Some(bson::from_slice(doc.as_bytes())?),
BatchValue::Some { doc, .. } => Some(crate::bson::from_slice(doc.as_bytes())?),
BatchValue::Empty | BatchValue::Exhausted => None,
})
}
Expand Down
Loading