Skip to content

Commit 5fdda29

Browse files
RUST-1876 Require T: Send + Sync for Collection<T> (#1043)
1 parent c00348f commit 5fdda29

File tree

20 files changed

+139
-50
lines changed

20 files changed

+139
-50
lines changed

src/action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub(crate) struct CollRef<'a> {
202202
}
203203

204204
impl<'a> CollRef<'a> {
205-
fn new<T>(coll: &'a Collection<T>) -> Self {
205+
fn new<T: Send + Sync>(coll: &'a Collection<T>) -> Self {
206206
Self {
207207
inner: coll.clone_with_type(),
208208
_ref: PhantomData,

src/action/aggregate.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ impl Database {
3636
}
3737
}
3838

39-
impl<T> Collection<T> {
39+
impl<T> Collection<T>
40+
where
41+
T: Send + Sync,
42+
{
4043
/// Runs an aggregation operation.
4144
///
4245
/// See the documentation [here](https://www.mongodb.com/docs/manual/aggregation/) for more
@@ -69,7 +72,10 @@ impl crate::sync::Database {
6972
}
7073

7174
#[cfg(feature = "sync")]
72-
impl<T> crate::sync::Collection<T> {
75+
impl<T> crate::sync::Collection<T>
76+
where
77+
T: Send + Sync,
78+
{
7379
/// Runs an aggregation operation.
7480
///
7581
/// See the documentation [here](https://www.mongodb.com/docs/manual/aggregation/) for more

src/action/count.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::{
99

1010
use super::{action_impl, option_setters, CollRef};
1111

12-
impl<T> Collection<T> {
12+
impl<T> Collection<T>
13+
where
14+
T: Send + Sync,
15+
{
1316
/// Estimates the number of documents in the collection using collection metadata.
1417
///
1518
/// Due to an oversight in versions 5.0.0 - 5.0.7 of MongoDB, the `count` server command,
@@ -46,7 +49,10 @@ impl<T> Collection<T> {
4649
}
4750

4851
#[cfg(feature = "sync")]
49-
impl<T> crate::sync::Collection<T> {
52+
impl<T> crate::sync::Collection<T>
53+
where
54+
T: Send + Sync,
55+
{
5056
/// Estimates the number of documents in the collection using collection metadata.
5157
///
5258
/// Due to an oversight in versions 5.0.0 - 5.0.7 of MongoDB, the `count` server command,

src/action/create_index.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crate::{
1515

1616
use super::{action_impl, option_setters, CollRef, Multiple, Single};
1717

18-
impl<T> Collection<T> {
18+
impl<T> Collection<T>
19+
where
20+
T: Send + Sync,
21+
{
1922
/// Creates the given index on this collection.
2023
///
2124
/// `await` will return `Result<CreateIndexResult>`.
@@ -46,8 +49,11 @@ impl<T> Collection<T> {
4649
}
4750
}
4851

49-
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
50-
impl<T> crate::sync::Collection<T> {
52+
#[cfg(feature = "sync")]
53+
impl<T> crate::sync::Collection<T>
54+
where
55+
T: Send + Sync,
56+
{
5157
/// Creates the given index on this collection.
5258
///
5359
/// [`run`](CreateIndex::run) will return `Result<CreateIndexResult>`.

src/action/delete.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use crate::{
1313

1414
use super::{action_impl, option_setters, CollRef};
1515

16-
impl<T> Collection<T> {
16+
impl<T> Collection<T>
17+
where
18+
T: Send + Sync,
19+
{
1720
/// Deletes up to one document found matching `query`.
1821
///
1922
/// This operation will retry once upon failure if the connection and encountered error support
@@ -46,8 +49,11 @@ impl<T> Collection<T> {
4649
}
4750
}
4851

49-
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
50-
impl<T> crate::sync::Collection<T> {
52+
#[cfg(feature = "sync")]
53+
impl<T> crate::sync::Collection<T>
54+
where
55+
T: Send + Sync,
56+
{
5157
/// Deletes up to one document found matching `query`.
5258
///
5359
/// This operation will retry once upon failure if the connection and encountered error support

src/action/distinct.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crate::{
1515

1616
use super::{action_impl, option_setters, CollRef};
1717

18-
impl<T> Collection<T> {
18+
impl<T> Collection<T>
19+
where
20+
T: Send + Sync,
21+
{
1922
/// Finds the distinct values of the field specified by `field_name` across the collection.
2023
///
2124
/// `await` will return `Result<Vec<Bson>>`.
@@ -30,8 +33,11 @@ impl<T> Collection<T> {
3033
}
3134
}
3235

33-
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
34-
impl<T> crate::sync::Collection<T> {
36+
#[cfg(feature = "sync")]
37+
impl<T> crate::sync::Collection<T>
38+
where
39+
T: Send + Sync,
40+
{
3541
/// Finds the distinct values of the field specified by `field_name` across the collection.
3642
///
3743
/// [`run`](Distinct::run) will return `Result<Vec<Bson>>`.

src/action/drop.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ action_impl! {
6969
}
7070
}
7171

72-
impl<T> Collection<T> {
72+
impl<T> Collection<T>
73+
where
74+
T: Send + Sync,
75+
{
7376
/// Drops the collection, deleting all data and indexes stored in it.
7477
///
7578
/// `await` will return `Result<()>`.
@@ -83,7 +86,10 @@ impl<T> Collection<T> {
8386
}
8487

8588
#[cfg(feature = "sync")]
86-
impl<T> crate::sync::Collection<T> {
89+
impl<T> crate::sync::Collection<T>
90+
where
91+
T: Send + Sync,
92+
{
8793
/// Drops the collection, deleting all data and indexes stored in it.
8894
///
8995
/// [`run`](DropCollection::run) will return `Result<()>`.

src/action/drop_index.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use crate::{
1212
Collection,
1313
};
1414

15-
impl<T> Collection<T> {
15+
impl<T> Collection<T>
16+
where
17+
T: Send + Sync,
18+
{
1619
/// Drops the index specified by `name` from this collection.
1720
///
1821
/// `await` will return `Result<()>`.
@@ -38,8 +41,11 @@ impl<T> Collection<T> {
3841
}
3942
}
4043

41-
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
42-
impl<T> crate::sync::Collection<T> {
44+
#[cfg(feature = "sync")]
45+
impl<T> crate::sync::Collection<T>
46+
where
47+
T: Send + Sync,
48+
{
4349
/// Drops the index specified by `name` from this collection.
4450
///
4551
/// [`run`](DropIndex::run) will return `Result<()>`.

src/action/list_indexes.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use super::{
2424
ListSpecifications,
2525
};
2626

27-
impl<T> Collection<T> {
27+
impl<T> Collection<T>
28+
where
29+
T: Send + Sync,
30+
{
2831
/// Lists all indexes on this collection.
2932
///
3033
/// `await` will return `Result<Cursor<IndexModel>>` (or `Result<SessionCursor<IndexModel>>` if
@@ -51,8 +54,11 @@ impl<T> Collection<T> {
5154
}
5255
}
5356

54-
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
55-
impl<T> crate::sync::Collection<T> {
57+
#[cfg(feature = "sync")]
58+
impl<T> crate::sync::Collection<T>
59+
where
60+
T: Send + Sync,
61+
{
5662
/// Lists all indexes on this collection.
5763
///
5864
/// [`run`](ListIndexes::run) will return `Result<Cursor<IndexModel>>` (or

src/action/update.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use crate::{
1313

1414
use super::{action_impl, option_setters, CollRef};
1515

16-
impl<T> Collection<T> {
16+
impl<T> Collection<T>
17+
where
18+
T: Send + Sync,
19+
{
1720
/// Updates all documents matching `query` in the collection.
1821
///
1922
/// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be
@@ -58,8 +61,11 @@ impl<T> Collection<T> {
5861
}
5962
}
6063

61-
#[cfg(any(feature = "sync", feature = "tokio-sync"))]
62-
impl<T> crate::sync::Collection<T> {
64+
#[cfg(feature = "sync")]
65+
impl<T> crate::sync::Collection<T>
66+
where
67+
T: Send + Sync,
68+
{
6369
/// Updates all documents matching `query` in the collection.
6470
///
6571
/// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be

src/action/watch.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ impl Database {
7979
}
8080
}
8181

82-
impl<T> Collection<T> {
82+
impl<T> Collection<T>
83+
where
84+
T: Send + Sync,
85+
{
8386
/// Starts a new [`ChangeStream`](change_stream/struct.ChangeStream.html) that receives events
8487
/// for all changes in this collection. A
8588
/// [`ChangeStream`](change_stream/struct.ChangeStream.html) cannot be started on system
@@ -133,7 +136,10 @@ impl crate::sync::Database {
133136
}
134137

135138
#[cfg(feature = "sync")]
136-
impl<T> crate::sync::Collection<T> {
139+
impl<T> crate::sync::Collection<T>
140+
where
141+
T: Send + Sync,
142+
{
137143
/// Starts a new [`ChangeStream`](change_stream/struct.ChangeStream.html) that receives events
138144
/// for all changes in this collection. A
139145
/// [`ChangeStream`](change_stream/struct.ChangeStream.html) cannot be started on system

src/coll.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,20 @@ use crate::{
8282
/// # }
8383
/// ```
8484
#[derive(Debug)]
85-
pub struct Collection<T> {
85+
pub struct Collection<T>
86+
where
87+
T: Send + Sync,
88+
{
8689
inner: Arc<CollectionInner>,
8790
_phantom: std::marker::PhantomData<fn() -> T>,
8891
}
8992

9093
// Because derive is too conservative, derive only implements Clone if T is Clone.
9194
// Collection<T> does not actually store any value of type T (so T does not need to be clone).
92-
impl<T> Clone for Collection<T> {
95+
impl<T> Clone for Collection<T>
96+
where
97+
T: Send + Sync,
98+
{
9399
fn clone(&self) -> Self {
94100
Self {
95101
inner: self.inner.clone(),
@@ -109,7 +115,10 @@ struct CollectionInner {
109115
human_readable_serialization: bool,
110116
}
111117

112-
impl<T> Collection<T> {
118+
impl<T> Collection<T>
119+
where
120+
T: Send + Sync,
121+
{
113122
pub(crate) fn new(db: Database, name: &str, options: Option<CollectionOptions>) -> Self {
114123
let options = options.unwrap_or_default();
115124
let selection_criteria = options
@@ -139,7 +148,7 @@ impl<T> Collection<T> {
139148
}
140149

141150
/// Gets a clone of the `Collection` with a different type `U`.
142-
pub fn clone_with_type<U>(&self) -> Collection<U> {
151+
pub fn clone_with_type<U: Send + Sync>(&self) -> Collection<U> {
143152
Collection {
144153
inner: self.inner.clone(),
145154
_phantom: Default::default(),
@@ -294,7 +303,7 @@ where
294303

295304
impl<T> Collection<T>
296305
where
297-
T: DeserializeOwned,
306+
T: DeserializeOwned + Send + Sync,
298307
{
299308
async fn find_one_and_delete_common(
300309
&self,
@@ -402,7 +411,7 @@ where
402411

403412
impl<T> Collection<T>
404413
where
405-
T: Serialize + DeserializeOwned,
414+
T: Serialize + DeserializeOwned + Send + Sync,
406415
{
407416
async fn find_one_and_replace_common(
408417
&self,
@@ -463,7 +472,7 @@ where
463472

464473
impl<T> Collection<T>
465474
where
466-
T: Serialize,
475+
T: Serialize + Send + Sync,
467476
{
468477
#[allow(clippy::needless_option_as_deref)]
469478
async fn insert_many_common(

src/coll/action/drop.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ action_impl! {
2424
}
2525

2626
#[cfg(feature = "in-use-encryption-unstable")]
27-
impl<T> crate::Collection<T> {
27+
impl<T> crate::Collection<T>
28+
where
29+
T: Send + Sync,
30+
{
2831
#[allow(clippy::needless_option_as_deref)]
2932
async fn drop_aux_collections(
3033
&self,

src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Database {
117117
///
118118
/// This method does not send or receive anything across the wire to the database, so it can be
119119
/// used repeatedly without incurring any costs from I/O.
120-
pub fn collection<T>(&self, name: &str) -> Collection<T> {
120+
pub fn collection<T: Send + Sync>(&self, name: &str) -> Collection<T> {
121121
Collection::new(self.clone(), name, None)
122122
}
123123

@@ -130,7 +130,7 @@ impl Database {
130130
///
131131
/// This method does not send or receive anything across the wire to the database, so it can be
132132
/// used repeatedly without incurring any costs from I/O.
133-
pub fn collection_with_options<T>(
133+
pub fn collection_with_options<T: Send + Sync>(
134134
&self,
135135
name: &str,
136136
options: CollectionOptions,

src/gridfs/upload.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ impl GridFsBucket {
174174
Ok(())
175175
}
176176

177-
async fn create_index<T>(&self, coll: &Collection<T>, keys: Document) -> Result<()> {
177+
async fn create_index<T: Send + Sync>(
178+
&self,
179+
coll: &Collection<T>,
180+
keys: Document,
181+
) -> Result<()> {
178182
// listIndexes returns an error if the collection has not yet been created.
179183
// Ignore NamespaceExists errors if the collection has already been created.
180184
if let Err(error) = self

src/search_index.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use bson::doc;
1212
use serde::{Deserialize, Serialize};
1313
use typed_builder::TypedBuilder;
1414

15-
impl<T> Collection<T> {
15+
impl<T> Collection<T>
16+
where
17+
T: Send + Sync,
18+
{
1619
/// Convenience method for creating a single search index.
1720
pub async fn create_search_index(
1821
&self,

0 commit comments

Comments
 (0)