Skip to content

Commit eee7cdf

Browse files
authored
RUST-2228 Sync the driver to the BSON conversion API changes (#1437)
1 parent ca436db commit eee7cdf

File tree

20 files changed

+63
-101
lines changed

20 files changed

+63
-101
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/action/run_command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Database {
4141
pub fn run_command(&self, command: Document) -> RunCommand {
4242
RunCommand {
4343
db: self,
44-
command: RawDocumentBuf::from_document(&command),
44+
command: RawDocumentBuf::try_from(&command),
4545
options: None,
4646
session: None,
4747
}
@@ -76,7 +76,7 @@ impl Database {
7676
pub fn run_cursor_command(&self, command: Document) -> RunCursorCommand {
7777
RunCursorCommand {
7878
db: self,
79-
command: RawDocumentBuf::from_document(&command),
79+
command: RawDocumentBuf::try_from(&command),
8080
options: None,
8181
session: ImplicitSession,
8282
}

src/bson_compat.rs

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,18 @@ pub(crate) fn cstr_to_str(cs: &CStr) -> &str {
2929
}
3030
}
3131

32+
#[cfg(feature = "bson-3")]
3233
pub(crate) trait RawDocumentBufExt: Sized {
33-
fn append_ref_compat<'a>(
34+
fn append_ref<'a>(
3435
&mut self,
3536
key: impl AsRef<CStr>,
3637
value: impl Into<crate::bson::raw::RawBsonRef<'a>> + 'a,
3738
);
38-
39-
#[cfg(not(feature = "bson-3"))]
40-
fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self>;
4139
}
4240

4341
#[cfg(feature = "bson-3")]
4442
impl RawDocumentBufExt for crate::bson::RawDocumentBuf {
45-
fn append_ref_compat<'a>(
43+
fn append_ref<'a>(
4644
&mut self,
4745
key: impl AsRef<CStr>,
4846
value: impl Into<crate::bson::raw::RawBsonRef<'a>> + 'a,
@@ -51,45 +49,15 @@ impl RawDocumentBufExt for crate::bson::RawDocumentBuf {
5149
}
5250
}
5351

54-
#[cfg(not(feature = "bson-3"))]
55-
impl RawDocumentBufExt for crate::bson::RawDocumentBuf {
56-
fn append_ref_compat<'a>(
57-
&mut self,
58-
key: impl AsRef<CStr>,
59-
value: impl Into<crate::bson::raw::RawBsonRef<'a>>,
60-
) {
61-
self.append_ref(key, value)
62-
}
63-
64-
fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self> {
65-
Self::from_bytes(data)
66-
}
67-
}
68-
69-
#[cfg(not(feature = "bson-3"))]
70-
pub(crate) trait RawDocumentExt {
71-
fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self>;
72-
}
73-
74-
#[cfg(not(feature = "bson-3"))]
75-
impl RawDocumentExt for crate::bson::RawDocument {
76-
fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self> {
77-
Self::from_bytes(data)
78-
}
79-
}
80-
81-
#[cfg(not(feature = "bson-3"))]
82-
#[allow(dead_code)]
83-
pub(crate) trait DocumentExt {
84-
fn encode_to_vec(&self) -> crate::bson::ser::Result<Vec<u8>>;
52+
#[cfg(feature = "bson-3")]
53+
pub(crate) trait RawBsonRefExt {
54+
fn to_raw_bson(&self) -> crate::bson::RawBson;
8555
}
8656

87-
#[cfg(not(feature = "bson-3"))]
88-
impl DocumentExt for crate::bson::Document {
89-
fn encode_to_vec(&self) -> crate::bson::ser::Result<Vec<u8>> {
90-
let mut out = vec![];
91-
self.to_writer(&mut out)?;
92-
Ok(out)
57+
#[cfg(feature = "bson-3")]
58+
impl RawBsonRefExt for crate::bson::RawBsonRef<'_> {
59+
fn to_raw_bson(&self) -> crate::bson::RawBson {
60+
(*self).into()
9361
}
9462
}
9563

@@ -113,6 +81,7 @@ use_either! {
11381
RawError => error::Error | raw::Error;
11482
DeError => error::Error | de::Error;
11583
SerError => error::Error | ser::Error;
84+
Utf8Lossy => Utf8Lossy | serde_helpers::Utf8LossyDeserialization;
11685
serialize_to_raw_document_buf => serialize_to_raw_document_buf | to_raw_document_buf;
11786
serialize_to_document => serialize_to_document | to_document;
11887
serialize_to_bson => serialize_to_bson | to_bson;

src/bson_util.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::{
66

77
use serde::Serialize;
88

9+
#[cfg(feature = "bson-3")]
10+
use crate::bson_compat::{RawBsonRefExt as _, RawDocumentBufExt as _};
911
use crate::{
1012
bson::{
1113
oid::ObjectId,
@@ -17,7 +19,6 @@ use crate::{
1719
RawBsonRef,
1820
RawDocumentBuf,
1921
},
20-
bson_compat::RawDocumentBufExt as _,
2122
checked::Checked,
2223
error::{Error, ErrorKind, Result},
2324
runtime::SyncLittleEndianRead,
@@ -78,7 +79,7 @@ pub(crate) fn to_bson_array(docs: &[Document]) -> Bson {
7879
pub(crate) fn to_raw_bson_array(docs: &[Document]) -> Result<RawBson> {
7980
let mut array = RawArrayBuf::new();
8081
for doc in docs {
81-
array.push(RawDocumentBuf::from_document(doc)?);
82+
array.push(RawDocumentBuf::try_from(doc)?);
8283
}
8384
Ok(RawBson::Array(array))
8485
}
@@ -215,7 +216,7 @@ pub(crate) fn append_ser(
215216
value: T,
216217
}
217218
let raw_doc = crate::bson_compat::serialize_to_raw_document_buf(&Helper { value })?;
218-
this.append_ref_compat(
219+
this.append_ref(
219220
key,
220221
raw_doc
221222
.get("value")?
@@ -241,7 +242,7 @@ pub(crate) fn get_or_prepend_id_field(doc: &mut RawDocumentBuf) -> Result<Bson>
241242
let new_length: i32 = Checked::new(new_bytes.len()).try_into()?;
242243
new_bytes[0..4].copy_from_slice(&new_length.to_le_bytes());
243244

244-
*doc = RawDocumentBuf::decode_from_bytes(new_bytes)?;
245+
*doc = RawDocumentBuf::from_bytes(new_bytes)?;
245246

246247
Ok(id.into())
247248
}

src/change_stream.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use serde::de::DeserializeOwned;
2020
#[cfg(test)]
2121
use tokio::sync::oneshot;
2222

23+
#[cfg(feature = "bson-3")]
24+
use crate::bson_compat::RawBsonRefExt as _;
2325
use crate::{
2426
change_stream::event::{ChangeStreamEvent, ResumeToken},
2527
cursor::{stream_poll_next, BatchValue, CursorStream, NextInBatchFuture},

src/client/auth/aws.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ use crate::{
2525
serde_util,
2626
};
2727

28-
#[cfg(not(feature = "bson-3"))]
29-
use crate::bson_compat::DocumentExt as _;
30-
3128
const AWS_ECS_IP: &str = "169.254.170.2";
3229
const AWS_EC2_IP: &str = "169.254.169.254";
3330
const AWS_LONG_DATE_FMT: &str = "%Y%m%dT%H%M%SZ";
@@ -75,7 +72,8 @@ async fn authenticate_stream_inner(
7572
// channel binding is not supported.
7673
"p": 110i32,
7774
};
78-
let client_first_payload_bytes = client_first_payload.encode_to_vec()?;
75+
let mut client_first_payload_bytes = vec![];
76+
client_first_payload.to_writer(&mut client_first_payload_bytes)?;
7977

8078
let sasl_start = SaslStart::new(
8179
source.into(),
@@ -125,7 +123,8 @@ async fn authenticate_stream_inner(
125123
client_second_payload.insert("t", security_token);
126124
}
127125

128-
let client_second_payload_bytes = client_second_payload.encode_to_vec()?;
126+
let mut client_second_payload_bytes = vec![];
127+
client_second_payload.to_writer(&mut client_second_payload_bytes)?;
129128

130129
let sasl_continue = SaslContinue::new(
131130
source.into(),

src/client/csfle/client_encryption.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use mongocrypt::{ctx::KmsProvider, Crypt};
99
use serde::{Deserialize, Serialize};
1010
use typed_builder::TypedBuilder;
1111

12+
#[cfg(feature = "bson-3")]
13+
use crate::bson_compat::RawBsonRefExt as _;
1214
use crate::{
1315
bson::{doc, spec::BinarySubtype, Binary, RawBinaryRef, RawDocumentBuf},
1416
client::options::TlsOptions,

src/client/csfle/client_encryption/encrypt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ impl<'a> Action for Encrypt<'a, Expression> {
5454
let doc_ref = result
5555
.get_document("v")
5656
.map_err(|e| Error::internal(format!("invalid encryption result: {}", e)))?;
57-
let doc = doc_ref
58-
.to_owned()
59-
.to_document()
57+
let doc = Document::try_from(doc_ref.to_owned())
6058
.map_err(|e| Error::internal(format!("invalid encryption result: {}", e)))?;
6159
Ok(doc)
6260
}

src/client/csfle/state_machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl CryptExecutor {
125125
}
126126
State::NeedMongoMarkings => {
127127
let ctx = result_mut(&mut ctx)?;
128-
let command = ctx.mongo_op()?.to_raw_document_buf();
128+
let command = ctx.mongo_op()?.to_owned();
129129
let db = db.as_ref().ok_or_else(|| {
130130
Error::internal("db required for NeedMongoMarkings state")
131131
})?;

src/client/executor.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#[cfg(feature = "in-use-encryption")]
22
use crate::bson::RawDocumentBuf;
33
use crate::bson::{doc, RawBsonRef, RawDocument, Timestamp};
4-
#[cfg(not(feature = "bson-3"))]
5-
use crate::bson_compat::RawDocumentExt as _;
64
#[cfg(feature = "in-use-encryption")]
75
use futures_core::future::BoxFuture;
86
use once_cell::sync::Lazy;
@@ -823,7 +821,7 @@ impl Client {
823821
is_sharded: bool,
824822
response: RawCommandResponse,
825823
) -> Result<RawCommandResponse> {
826-
let raw_doc = RawDocument::decode_from_bytes(response.as_bytes())?;
824+
let raw_doc = RawDocument::from_bytes(response.as_bytes())?;
827825

828826
let ok = match raw_doc.get("ok")? {
829827
Some(b) => {

0 commit comments

Comments
 (0)