Skip to content

Commit 5d09bf4

Browse files
authored
RUST-793 Reduce size of returned futures (#417)
1 parent 114a519 commit 5d09bf4

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

src/client/executor.rs

+39-33
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,45 @@ impl Client {
6969
op: T,
7070
session: impl Into<Option<&mut ClientSession>>,
7171
) -> Result<T::O> {
72-
// TODO RUST-9: allow unacknowledged write concerns
73-
if !op.is_acknowledged() {
74-
return Err(ErrorKind::InvalidArgument {
75-
message: "Unacknowledged write concerns are not supported".to_string(),
72+
Box::pin(async {
73+
// TODO RUST-9: allow unacknowledged write concerns
74+
if !op.is_acknowledged() {
75+
return Err(ErrorKind::InvalidArgument {
76+
message: "Unacknowledged write concerns are not supported".to_string(),
77+
}
78+
.into());
7679
}
77-
.into());
78-
}
79-
match session.into() {
80-
Some(session) => {
81-
if !Arc::ptr_eq(&self.inner, &session.client().inner) {
82-
return Err(ErrorKind::InvalidArgument {
83-
message: "the session provided to an operation must be created from the \
84-
same client as the collection/database"
85-
.into(),
80+
match session.into() {
81+
Some(session) => {
82+
if !Arc::ptr_eq(&self.inner, &session.client().inner) {
83+
return Err(ErrorKind::InvalidArgument {
84+
message: "the session provided to an operation must be created from \
85+
the same client as the collection/database"
86+
.into(),
87+
}
88+
.into());
8689
}
87-
.into());
88-
}
8990

90-
if let Some(SelectionCriteria::ReadPreference(read_preference)) =
91-
op.selection_criteria()
92-
{
93-
if session.in_transaction() && read_preference != &ReadPreference::Primary {
94-
return Err(ErrorKind::Transaction {
95-
message: "read preference in a transaction must be primary".into(),
91+
if let Some(SelectionCriteria::ReadPreference(read_preference)) =
92+
op.selection_criteria()
93+
{
94+
if session.in_transaction() && read_preference != &ReadPreference::Primary {
95+
return Err(ErrorKind::Transaction {
96+
message: "read preference in a transaction must be primary".into(),
97+
}
98+
.into());
9699
}
97-
.into());
98100
}
101+
self.execute_operation_with_retry(op, Some(session)).await
102+
}
103+
None => {
104+
let mut implicit_session = self.start_implicit_session(&op).await?;
105+
self.execute_operation_with_retry(op, implicit_session.as_mut())
106+
.await
99107
}
100-
self.execute_operation_with_retry(op, Some(session)).await
101-
}
102-
None => {
103-
let mut implicit_session = self.start_implicit_session(&op).await?;
104-
self.execute_operation_with_retry(op, implicit_session.as_mut())
105-
.await
106108
}
107-
}
109+
})
110+
.await
108111
}
109112

110113
/// Execute the given operation, returning the implicit session created for it if one was.
@@ -114,10 +117,13 @@ impl Client {
114117
&self,
115118
op: T,
116119
) -> Result<(T::O, Option<ClientSession>)> {
117-
let mut implicit_session = self.start_implicit_session(&op).await?;
118-
self.execute_operation_with_retry(op, implicit_session.as_mut())
119-
.await
120-
.map(|result| (result, implicit_session))
120+
Box::pin(async {
121+
let mut implicit_session = self.start_implicit_session(&op).await?;
122+
self.execute_operation_with_retry(op, implicit_session.as_mut())
123+
.await
124+
.map(|result| (result, implicit_session))
125+
})
126+
.await
121127
}
122128

123129
/// Selects a server and executes the given operation on it, optionally using a provided

src/operation/find/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::CursorResponse;
2121
pub(crate) struct Find<T> {
2222
ns: Namespace,
2323
filter: Option<Document>,
24-
options: Option<FindOptions>,
24+
options: Option<Box<FindOptions>>,
2525
_phantom: PhantomData<T>,
2626
}
2727

@@ -46,7 +46,7 @@ impl<T> Find<T> {
4646
Self {
4747
ns,
4848
filter,
49-
options,
49+
options: options.map(Box::new),
5050
_phantom: Default::default(),
5151
}
5252
}

src/test/spec/retryable_reads.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{sync::Arc, time::Duration};
22

33
use bson::doc;
4-
use futures::FutureExt;
54
use tokio::sync::RwLockWriteGuard;
65

76
use crate::{
@@ -63,10 +62,7 @@ async fn retry_releases_connection() {
6362
let _fp_guard = client.enable_failpoint(failpoint, None).await.unwrap();
6463

6564
RUNTIME
66-
.timeout(
67-
Duration::from_secs(1),
68-
collection.find_one(doc! {}, None).boxed(),
69-
)
65+
.timeout(Duration::from_secs(1), collection.find_one(doc! {}, None))
7066
.await
7167
.expect("operation should not time out")
7268
.expect("find should succeed");

0 commit comments

Comments
 (0)