Skip to content

Commit bb945e7

Browse files
authored
RUST-754 Accept impl Borrow<T> in various CRUD methods (#331)
1 parent 5aa29c2 commit bb945e7

File tree

7 files changed

+53
-61
lines changed

7 files changed

+53
-61
lines changed

src/coll/mod.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod batch;
22
pub mod options;
33

4-
use std::{fmt, fmt::Debug, sync::Arc};
4+
use std::{borrow::Borrow, fmt, fmt::Debug, sync::Arc};
55

66
use futures::StreamExt;
77
use serde::{
@@ -539,11 +539,11 @@ where
539539
async fn find_one_and_replace_common(
540540
&self,
541541
filter: Document,
542-
replacement: T,
542+
replacement: impl Borrow<T>,
543543
options: impl Into<Option<FindOneAndReplaceOptions>>,
544544
session: impl Into<Option<&mut ClientSession>>,
545545
) -> Result<Option<T>> {
546-
let replacement = to_document(&replacement)?;
546+
let replacement = to_document(replacement.borrow())?;
547547

548548
let mut options = options.into();
549549
resolve_options!(self, options, [write_concern]);
@@ -562,7 +562,7 @@ where
562562
pub async fn find_one_and_replace(
563563
&self,
564564
filter: Document,
565-
replacement: T,
565+
replacement: impl Borrow<T>,
566566
options: impl Into<Option<FindOneAndReplaceOptions>>,
567567
) -> Result<Option<T>> {
568568
self.find_one_and_replace_common(filter, replacement, options, None)
@@ -579,7 +579,7 @@ where
579579
pub async fn find_one_and_replace_with_session(
580580
&self,
581581
filter: Document,
582-
replacement: T,
582+
replacement: impl Borrow<T>,
583583
options: impl Into<Option<FindOneAndReplaceOptions>>,
584584
session: &mut ClientSession,
585585
) -> Result<Option<T>> {
@@ -643,13 +643,13 @@ where
643643

644644
async fn insert_many_common(
645645
&self,
646-
docs: impl IntoIterator<Item = T>,
646+
docs: impl IntoIterator<Item = impl Borrow<T>>,
647647
options: impl Into<Option<InsertManyOptions>>,
648648
mut session: Option<&mut ClientSession>,
649649
) -> Result<InsertManyResult> {
650650
let docs: ser::Result<Vec<Document>> = docs
651651
.into_iter()
652-
.map(|doc| bson::to_document(&doc))
652+
.map(|doc| bson::to_document(doc.borrow()))
653653
.collect();
654654
let mut docs: Vec<Document> = docs?;
655655

@@ -739,7 +739,7 @@ where
739739
/// retryable writes.
740740
pub async fn insert_many(
741741
&self,
742-
docs: impl IntoIterator<Item = T>,
742+
docs: impl IntoIterator<Item = impl Borrow<T>>,
743743
options: impl Into<Option<InsertManyOptions>>,
744744
) -> Result<InsertManyResult> {
745745
self.insert_many_common(docs, options, None).await
@@ -753,7 +753,7 @@ where
753753
/// retryable writes.
754754
pub async fn insert_many_with_session(
755755
&self,
756-
docs: impl IntoIterator<Item = T>,
756+
docs: impl IntoIterator<Item = impl Borrow<T>>,
757757
options: impl Into<Option<InsertManyOptions>>,
758758
session: &mut ClientSession,
759759
) -> Result<InsertManyResult> {
@@ -762,11 +762,11 @@ where
762762

763763
async fn insert_one_common(
764764
&self,
765-
doc: T,
765+
doc: impl Borrow<T>,
766766
options: impl Into<Option<InsertOneOptions>>,
767767
session: impl Into<Option<&mut ClientSession>>,
768768
) -> Result<InsertOneResult> {
769-
let doc = to_document(&doc)?;
769+
let doc = to_document(doc.borrow())?;
770770

771771
let mut options = options.into();
772772
resolve_options!(self, options, [write_concern]);
@@ -791,7 +791,7 @@ where
791791
/// retryable writes.
792792
pub async fn insert_one(
793793
&self,
794-
doc: T,
794+
doc: impl Borrow<T>,
795795
options: impl Into<Option<InsertOneOptions>>,
796796
) -> Result<InsertOneResult> {
797797
self.insert_one_common(doc, options, None).await
@@ -805,7 +805,7 @@ where
805805
/// retryable writes.
806806
pub async fn insert_one_with_session(
807807
&self,
808-
doc: T,
808+
doc: impl Borrow<T>,
809809
options: impl Into<Option<InsertOneOptions>>,
810810
session: &mut ClientSession,
811811
) -> Result<InsertOneResult> {
@@ -815,11 +815,11 @@ where
815815
async fn replace_one_common(
816816
&self,
817817
query: Document,
818-
replacement: T,
818+
replacement: impl Borrow<T>,
819819
options: impl Into<Option<ReplaceOptions>>,
820820
session: impl Into<Option<&mut ClientSession>>,
821821
) -> Result<UpdateResult> {
822-
let replacement = to_document(&replacement)?;
822+
let replacement = to_document(replacement.borrow())?;
823823

824824
bson_util::replacement_document_check(&replacement)?;
825825

@@ -845,7 +845,7 @@ where
845845
pub async fn replace_one(
846846
&self,
847847
query: Document,
848-
replacement: T,
848+
replacement: impl Borrow<T>,
849849
options: impl Into<Option<ReplaceOptions>>,
850850
) -> Result<UpdateResult> {
851851
self.replace_one_common(query, replacement, options, None)
@@ -862,7 +862,7 @@ where
862862
pub async fn replace_one_with_session(
863863
&self,
864864
query: Document,
865-
replacement: T,
865+
replacement: impl Borrow<T>,
866866
options: impl Into<Option<ReplaceOptions>>,
867867
session: &mut ClientSession,
868868
) -> Result<UpdateResult> {

src/runtime/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ impl AsyncRuntime {
8080
#[cfg(any(feature = "sync", test))]
8181
pub(crate) fn block_on<F, T>(self, fut: F) -> T
8282
where
83-
F: Future<Output = T> + Send,
84-
T: Send,
83+
F: Future<Output = T>,
8584
{
8685
#[cfg(all(feature = "tokio-runtime", not(feature = "async-std-runtime")))]
8786
{

src/sync/coll.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
fmt::Debug,
3-
marker::{Send, Sync},
4-
};
1+
use std::{borrow::Borrow, fmt::Debug};
52

63
use serde::{de::DeserializeOwned, Serialize};
74

@@ -76,14 +73,14 @@ use crate::{
7673
#[derive(Clone, Debug)]
7774
pub struct Collection<T>
7875
where
79-
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
76+
T: Serialize + DeserializeOwned + Unpin + Debug,
8077
{
8178
async_collection: AsyncCollection<T>,
8279
}
8380

8481
impl<T> Collection<T>
8582
where
86-
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
83+
T: Serialize + DeserializeOwned + Unpin + Debug,
8784
{
8885
pub(crate) fn new(async_collection: AsyncCollection<T>) -> Self {
8986
Self { async_collection }
@@ -92,7 +89,7 @@ where
9289
/// Gets a clone of the `Collection` with a different type `U`.
9390
pub fn clone_with_type<U>(&self) -> Collection<U>
9491
where
95-
U: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
92+
U: Serialize + DeserializeOwned + Unpin + Debug,
9693
{
9794
Collection::new(self.async_collection.clone_with_type())
9895
}
@@ -497,10 +494,9 @@ where
497494
/// retryable writes.
498495
pub fn insert_many(
499496
&self,
500-
docs: impl IntoIterator<Item = T>,
497+
docs: impl IntoIterator<Item = impl Borrow<T>>,
501498
options: impl Into<Option<InsertManyOptions>>,
502499
) -> Result<InsertManyResult> {
503-
let docs: Vec<T> = docs.into_iter().collect();
504500
RUNTIME.block_on(self.async_collection.insert_many(docs, options.into()))
505501
}
506502

@@ -512,11 +508,10 @@ where
512508
/// retryable writes.
513509
pub fn insert_many_with_session(
514510
&self,
515-
docs: impl IntoIterator<Item = T>,
511+
docs: impl IntoIterator<Item = impl Borrow<T>>,
516512
options: impl Into<Option<InsertManyOptions>>,
517513
session: &mut ClientSession,
518514
) -> Result<InsertManyResult> {
519-
let docs: Vec<T> = docs.into_iter().collect();
520515
RUNTIME.block_on(self.async_collection.insert_many_with_session(
521516
docs,
522517
options.into(),
@@ -532,10 +527,13 @@ where
532527
/// retryable writes.
533528
pub fn insert_one(
534529
&self,
535-
doc: T,
530+
doc: impl Borrow<T>,
536531
options: impl Into<Option<InsertOneOptions>>,
537532
) -> Result<InsertOneResult> {
538-
RUNTIME.block_on(self.async_collection.insert_one(doc, options.into()))
533+
RUNTIME.block_on(
534+
self.async_collection
535+
.insert_one(doc.borrow(), options.into()),
536+
)
539537
}
540538

541539
/// Inserts `doc` into the collection using the provided `ClientSession`.
@@ -546,12 +544,12 @@ where
546544
/// retryable writes.
547545
pub fn insert_one_with_session(
548546
&self,
549-
doc: T,
547+
doc: impl Borrow<T>,
550548
options: impl Into<Option<InsertOneOptions>>,
551549
session: &mut ClientSession,
552550
) -> Result<InsertOneResult> {
553551
RUNTIME.block_on(self.async_collection.insert_one_with_session(
554-
doc,
552+
doc.borrow(),
555553
options.into(),
556554
session,
557555
))
@@ -566,13 +564,14 @@ where
566564
pub fn replace_one(
567565
&self,
568566
query: Document,
569-
replacement: T,
567+
replacement: impl Borrow<T>,
570568
options: impl Into<Option<ReplaceOptions>>,
571569
) -> Result<UpdateResult> {
572-
RUNTIME.block_on(
573-
self.async_collection
574-
.replace_one(query, replacement, options.into()),
575-
)
570+
RUNTIME.block_on(self.async_collection.replace_one(
571+
query,
572+
replacement.borrow(),
573+
options.into(),
574+
))
576575
}
577576

578577
/// Replaces up to one document matching `query` in the collection with `replacement` using the
@@ -585,13 +584,13 @@ where
585584
pub fn replace_one_with_session(
586585
&self,
587586
query: Document,
588-
replacement: T,
587+
replacement: impl Borrow<T>,
589588
options: impl Into<Option<ReplaceOptions>>,
590589
session: &mut ClientSession,
591590
) -> Result<UpdateResult> {
592591
RUNTIME.block_on(self.async_collection.replace_one_with_session(
593592
query,
594-
replacement,
593+
replacement.borrow(),
595594
options.into(),
596595
session,
597596
))

src/sync/cursor.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ use crate::{
7171
#[derive(Debug)]
7272
pub struct Cursor<T>
7373
where
74-
T: DeserializeOwned + Unpin + Send,
74+
T: DeserializeOwned + Unpin,
7575
{
7676
async_cursor: AsyncCursor<T>,
7777
}
7878

7979
impl<T> Cursor<T>
8080
where
81-
T: DeserializeOwned + Unpin + Send,
81+
T: DeserializeOwned + Unpin,
8282
{
8383
pub(crate) fn new(async_cursor: AsyncCursor<T>) -> Self {
8484
Self { async_cursor }
@@ -87,7 +87,7 @@ where
8787

8888
impl<T> Iterator for Cursor<T>
8989
where
90-
T: DeserializeOwned + Unpin + Send,
90+
T: DeserializeOwned + Unpin,
9191
{
9292
type Item = Result<T>;
9393

@@ -118,14 +118,14 @@ where
118118
#[derive(Debug)]
119119
pub struct SessionCursor<T>
120120
where
121-
T: DeserializeOwned + Unpin + Send,
121+
T: DeserializeOwned + Unpin,
122122
{
123123
async_cursor: AsyncSessionCursor<T>,
124124
}
125125

126126
impl<T> SessionCursor<T>
127127
where
128-
T: DeserializeOwned + Unpin + Send,
128+
T: DeserializeOwned + Unpin,
129129
{
130130
pub(crate) fn new(async_cursor: AsyncSessionCursor<T>) -> Self {
131131
Self { async_cursor }
@@ -149,14 +149,14 @@ where
149149
/// This updates the buffer of the parent `SessionCursor` when dropped.
150150
pub struct SessionCursorHandle<'cursor, 'session, T = Document>
151151
where
152-
T: DeserializeOwned + Unpin + Send,
152+
T: DeserializeOwned + Unpin,
153153
{
154154
async_handle: AsyncSessionCursorHandle<'cursor, 'session, T>,
155155
}
156156

157157
impl<T> Iterator for SessionCursorHandle<'_, '_, T>
158158
where
159-
T: DeserializeOwned + Unpin + Send,
159+
T: DeserializeOwned + Unpin,
160160
{
161161
type Item = Result<T>;
162162

src/sync/db.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
fmt::Debug,
3-
marker::{Send, Sync},
4-
};
1+
use std::fmt::Debug;
52

63
use serde::{de::DeserializeOwned, Serialize};
74

@@ -95,7 +92,7 @@ impl Database {
9592
/// used repeatedly without incurring any costs from I/O.
9693
pub fn collection<T>(&self, name: &str) -> Collection<T>
9794
where
98-
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
95+
T: Serialize + DeserializeOwned + Unpin + Debug,
9996
{
10097
Collection::new(self.async_database.collection(name))
10198
}
@@ -112,7 +109,7 @@ impl Database {
112109
options: CollectionOptions,
113110
) -> Collection<T>
114111
where
115-
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
112+
T: Serialize + DeserializeOwned + Unpin + Debug,
116113
{
117114
Collection::new(self.async_database.collection_with_options(name, options))
118115
}

src/sync/test.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
fmt::Debug,
3-
marker::{Send, Sync},
4-
};
1+
use std::fmt::Debug;
52

63
use pretty_assertions::assert_eq;
74
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -30,7 +27,7 @@ fn init_db_and_coll(client: &Client, db_name: &str, coll_name: &str) -> Collecti
3027

3128
fn init_db_and_typed_coll<T>(client: &Client, db_name: &str, coll_name: &str) -> Collection<T>
3229
where
33-
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
30+
T: Serialize + DeserializeOwned + Unpin + Debug,
3431
{
3532
let coll = client.database(db_name).collection(coll_name);
3633
drop_collection(&coll);
@@ -39,7 +36,7 @@ where
3936

4037
pub fn drop_collection<T>(coll: &Collection<T>)
4138
where
42-
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
39+
T: Serialize + DeserializeOwned + Unpin + Debug,
4340
{
4441
match coll.drop(None).as_ref().map_err(|e| &e.kind) {
4542
Err(ErrorKind::CommandError(CommandError { code: 26, .. })) | Ok(_) => {}

src/test/coll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ async fn empty_insert() {
541541
.database(function_name!())
542542
.collection::<Document>(function_name!());
543543
match coll
544-
.insert_many(Vec::new(), None)
544+
.insert_many(Vec::<Document>::new(), None)
545545
.await
546546
.expect_err("should get error")
547547
.kind

0 commit comments

Comments
 (0)