Skip to content

Commit 3e68edf

Browse files
authored
RUST-735 Remove Document as default generic type on Collection and Cursor (#323)
1 parent 1b7945b commit 3e68edf

File tree

21 files changed

+216
-113
lines changed

21 files changed

+216
-113
lines changed

.evergreen/aws-ecs-test/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mongodb::Client;
1+
use mongodb::{bson::Document, Client};
22

33
#[tokio::main]
44
async fn main() {
@@ -7,7 +7,7 @@ async fn main() {
77

88
client
99
.database("aws")
10-
.collection("somecoll")
10+
.collection::<Document>("somecoll")
1111
.find_one(None, None)
1212
.await
1313
.unwrap();

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ for collection_name in db.list_collection_names(None).await? {
9797
```
9898
#### Inserting documents into a collection
9999
```rust
100-
use mongodb::bson::doc;
100+
use mongodb::bson::{doc, Document};
101101
```
102102
```rust
103103
// Get a handle to a collection in the database.
104-
let collection = db.collection("books");
104+
let collection = db.collection::<Document>("books");
105105

106106
let docs = vec![
107107
doc! { "title": "1984", "author": "George Orwell" },
@@ -147,14 +147,14 @@ The driver also provides a blocking sync API. See the [Installation](#enabling-t
147147
The various sync-specific types are found in the `mongodb::sync` submodule rather than in the crate's top level like in the async API. The sync API calls through to the async API internally though, so it looks and behaves similarly to it.
148148
```rust
149149
use mongodb::{
150-
bson::{doc, Bson},
150+
bson::{doc, Bson, Document},
151151
sync::Client,
152152
};
153153
```
154154
```rust
155155
let client = Client::with_uri_str("mongodb://localhost:27017")?;
156156
let database = client.database("mydb");
157-
let collection = database.collection("books");
157+
let collection = database.collection::<Document>("books");
158158

159159
let docs = vec![
160160
doc! { "title": "1984", "author": "George Orwell" },

src/client/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const DEFAULT_SERVER_SELECTION_TIMEOUT: Duration = Duration::from_secs(30);
4242
///
4343
/// ```rust
4444
/// # #[cfg(not(feature = "sync"))]
45-
/// # use mongodb::{Client, error::Result};
45+
/// # use mongodb::{bson::Document, Client, error::Result};
4646
/// # #[cfg(feature = "async-std-runtime")]
4747
/// # use async_std::task;
4848
/// # #[cfg(feature = "tokio-runtime")]
@@ -56,7 +56,7 @@ const DEFAULT_SERVER_SELECTION_TIMEOUT: Duration = Duration::from_secs(30);
5656
/// let client_ref = client.clone();
5757
///
5858
/// task::spawn(async move {
59-
/// let collection = client_ref.database("items").collection(&format!("coll{}", i));
59+
/// let collection = client_ref.database("items").collection::<Document>(&format!("coll{}", i));
6060
///
6161
/// // Do something with the collection
6262
/// });

src/client/session/test.rs

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

3+
use bson::Document;
34
use futures::stream::StreamExt;
45
use tokio::sync::RwLockReadGuard;
56

@@ -38,7 +39,9 @@ macro_rules! db_op {
3839
macro_rules! collection_op {
3940
($test_name:expr, $coll:ident, $body:expr) => {
4041
|client| async move {
41-
let $coll = client.database($test_name).collection($test_name);
42+
let $coll = client
43+
.database($test_name)
44+
.collection::<bson::Document>($test_name);
4245
$body.await.unwrap();
4346
}
4447
};
@@ -285,7 +288,7 @@ async fn cluster_time_in_commands() {
285288
cluster_time_test("aggregate", |client| async move {
286289
client
287290
.database(function_name!())
288-
.collection(function_name!())
291+
.collection::<Document>(function_name!())
289292
.aggregate(vec![doc! { "$match": { "x": 1 } }], None)
290293
.await
291294
})
@@ -294,7 +297,7 @@ async fn cluster_time_in_commands() {
294297
cluster_time_test("find", |client| async move {
295298
client
296299
.database(function_name!())
297-
.collection(function_name!())
300+
.collection::<Document>(function_name!())
298301
.find(doc! {}, None)
299302
.await
300303
})
@@ -303,7 +306,7 @@ async fn cluster_time_in_commands() {
303306
cluster_time_test("insert", |client| async move {
304307
client
305308
.database(function_name!())
306-
.collection(function_name!())
309+
.collection::<Document>(function_name!())
307310
.insert_one(doc! {}, None)
308311
.await
309312
})

src/coll/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ where
215215
&self,
216216
pipeline: impl IntoIterator<Item = Document>,
217217
options: impl Into<Option<AggregateOptions>>,
218-
) -> Result<Cursor> {
218+
) -> Result<Cursor<Document>> {
219219
let mut options = options.into();
220220
resolve_options!(
221221
self,
@@ -240,7 +240,7 @@ where
240240
pipeline: impl IntoIterator<Item = Document>,
241241
options: impl Into<Option<AggregateOptions>>,
242242
session: &mut ClientSession,
243-
) -> Result<SessionCursor> {
243+
) -> Result<SessionCursor<Document>> {
244244
let mut options = options.into();
245245
resolve_options!(
246246
self,

src/cursor/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ use common::{GenericCursor, GetMoreProvider, GetMoreProviderResult};
4343
///
4444
/// ```rust
4545
/// # use futures::stream::StreamExt;
46-
/// # use mongodb::{Client, error::Result};
46+
/// # use mongodb::{bson::Document, Client, error::Result};
4747
/// #
4848
/// # async fn do_stuff() -> Result<()> {
4949
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
50-
/// # let coll = client.database("foo").collection("bar");
50+
/// # let coll = client.database("foo").collection::<Document>("bar");
5151
/// # let mut cursor = coll.find(None, None).await?;
5252
/// #
5353
/// while let Some(doc) = cursor.next().await {
@@ -81,7 +81,7 @@ use common::{GenericCursor, GetMoreProvider, GetMoreProviderResult};
8181
/// # }
8282
/// ```
8383
#[derive(Debug)]
84-
pub struct Cursor<T = Document>
84+
pub struct Cursor<T>
8585
where
8686
T: DeserializeOwned + Unpin,
8787
{
@@ -139,7 +139,7 @@ where
139139
let coll = self
140140
.client
141141
.database(ns.db.as_str())
142-
.collection(ns.coll.as_str());
142+
.collection::<Document>(ns.coll.as_str());
143143
let cursor_id = self.wrapped_cursor.id();
144144
RUNTIME.execute(async move { coll.kill_cursor(cursor_id).await });
145145
}

src/cursor/session.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ use crate::{
2424
///
2525
/// ```rust
2626
/// # use futures::stream::StreamExt;
27-
/// # use mongodb::{Client, error::Result, ClientSession, SessionCursor};
27+
/// # use mongodb::{bson::Document, Client, error::Result, ClientSession, SessionCursor};
2828
/// #
2929
/// # async fn do_stuff() -> Result<()> {
3030
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
3131
/// # let mut session = client.start_session(None).await?;
32-
/// # let coll = client.database("foo").collection("bar");
32+
/// # let coll = client.database("foo").collection::<Document>("bar");
3333
/// # let mut cursor = coll.find_with_session(None, None, &mut session).await?;
3434
/// #
3535
/// while let Some(doc) = cursor.with_session(&mut session).next().await {
@@ -40,7 +40,7 @@ use crate::{
4040
/// # }
4141
/// ```
4242
#[derive(Debug)]
43-
pub struct SessionCursor<T = Document>
43+
pub struct SessionCursor<T>
4444
where
4545
T: DeserializeOwned + Unpin,
4646
{
@@ -105,7 +105,7 @@ where
105105
let coll = self
106106
.client
107107
.database(ns.db.as_str())
108-
.collection(ns.coll.as_str());
108+
.collection::<Document>(ns.coll.as_str());
109109
let cursor_id = self.info.id;
110110
RUNTIME.execute(async move { coll.kill_cursor(cursor_id).await });
111111
}

src/db/mod.rs

+8-27
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
/// ```rust
4040
///
4141
/// # #[cfg(not(feature = "sync"))]
42-
/// # use mongodb::{Client, error::Result};
42+
/// # use mongodb::{bson::Document, Client, error::Result};
4343
/// # #[cfg(feature = "async-std-runtime")]
4444
/// # use async_std::task;
4545
/// # #[cfg(feature = "tokio-runtime")]
@@ -55,7 +55,7 @@ use crate::{
5555
/// let db_ref = db.clone();
5656
///
5757
/// task::spawn(async move {
58-
/// let collection = db_ref.collection(&format!("coll{}", i));
58+
/// let collection = db_ref.collection::<Document>(&format!("coll{}", i));
5959
///
6060
/// // Do something with the collection
6161
/// });
@@ -129,45 +129,26 @@ impl Database {
129129
self.inner.write_concern.as_ref()
130130
}
131131

132-
/// Gets a handle to a collection specified by `name` of the database. The `Collection` options
133-
/// (e.g. read preference and write concern) will default to those of the `Database`.
134-
///
135-
/// This method does not send or receive anything across the wire to the database, so it can be
136-
/// used repeatedly without incurring any costs from I/O.
137-
pub fn collection(&self, name: &str) -> Collection {
138-
Collection::new(self.clone(), name, None)
139-
}
140-
141132
/// Gets a handle to a collection with type `T` specified by `name` of the database. The
142133
/// `Collection` options (e.g. read preference and write concern) will default to those of the
143134
/// `Database`.
144135
///
145136
/// This method does not send or receive anything across the wire to the database, so it can be
146137
/// used repeatedly without incurring any costs from I/O.
147-
pub fn collection_with_type<T>(&self, name: &str) -> Collection<T>
138+
pub fn collection<T>(&self, name: &str) -> Collection<T>
148139
where
149140
T: Serialize + DeserializeOwned + Unpin + Debug,
150141
{
151142
Collection::new(self.clone(), name, None)
152143
}
153144

154-
/// Gets a handle to a collection specified by `name` in the cluster the `Client` is connected
155-
/// to. Operations done with this `Collection` will use the options specified by `options` by
156-
/// default and will otherwise default to those of the `Database`.
157-
///
158-
/// This method does not send or receive anything across the wire to the database, so it can be
159-
/// used repeatedly without incurring any costs from I/O.
160-
pub fn collection_with_options(&self, name: &str, options: CollectionOptions) -> Collection {
161-
Collection::new(self.clone(), name, Some(options))
162-
}
163-
164145
/// Gets a handle to a collection with type `T` specified by `name` in the cluster the `Client`
165146
/// is connected to. Operations done with this `Collection` will use the options specified by
166147
/// `options` by default and will otherwise default to those of the `Database`.
167148
///
168149
/// This method does not send or receive anything across the wire to the database, so it can be
169150
/// used repeatedly without incurring any costs from I/O.
170-
pub fn collection_with_type_and_options<T>(
151+
pub fn collection_with_options<T>(
171152
&self,
172153
name: &str,
173154
options: CollectionOptions,
@@ -213,7 +194,7 @@ impl Database {
213194
&self,
214195
filter: impl Into<Option<Document>>,
215196
options: impl Into<Option<ListCollectionsOptions>>,
216-
) -> Result<Cursor> {
197+
) -> Result<Cursor<Document>> {
217198
let list_collections = ListCollections::new(
218199
self.name().to_string(),
219200
filter.into(),
@@ -234,7 +215,7 @@ impl Database {
234215
filter: impl Into<Option<Document>>,
235216
options: impl Into<Option<ListCollectionsOptions>>,
236217
session: &mut ClientSession,
237-
) -> Result<SessionCursor> {
218+
) -> Result<SessionCursor<Document>> {
238219
let list_collections = ListCollections::new(
239220
self.name().to_string(),
240221
filter.into(),
@@ -392,7 +373,7 @@ impl Database {
392373
&self,
393374
pipeline: impl IntoIterator<Item = Document>,
394375
options: impl Into<Option<AggregateOptions>>,
395-
) -> Result<Cursor> {
376+
) -> Result<Cursor<Document>> {
396377
let mut options = options.into();
397378
resolve_options!(
398379
self,
@@ -417,7 +398,7 @@ impl Database {
417398
pipeline: impl IntoIterator<Item = Document>,
418399
options: impl Into<Option<AggregateOptions>>,
419400
session: &mut ClientSession,
420-
) -> Result<SessionCursor> {
401+
) -> Result<SessionCursor<Document>> {
421402
let mut options = options.into();
422403
resolve_options!(
423404
self,

src/sdam/description/topology/server_selection/test/in_window.rs

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

33
use approx::abs_diff_eq;
4+
use bson::Document;
45
use semver::VersionReq;
56
use serde::Deserialize;
67
use tokio::sync::RwLockWriteGuard;
@@ -157,7 +158,7 @@ async fn load_balancing_test() {
157158
for _ in 0..10 {
158159
let collection = client
159160
.database("load_balancing_test")
160-
.collection("load_balancing_test");
161+
.collection::<Document>("load_balancing_test");
161162
handles.push(
162163
RUNTIME
163164
.spawn(async move {

src/sync/client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
/// so it can safely be shared across threads. For example:
2727
///
2828
/// ```rust
29-
/// # use mongodb::{sync::Client, error::Result};
29+
/// # use mongodb::{bson::Document, sync::Client, error::Result};
3030
/// #
3131
/// # fn start_workers() -> Result<()> {
3232
/// let client = Client::with_uri_str("mongodb://example.com")?;
@@ -35,7 +35,7 @@ use crate::{
3535
/// let client_ref = client.clone();
3636
///
3737
/// std::thread::spawn(move || {
38-
/// let collection = client_ref.database("items").collection(&format!("coll{}", i));
38+
/// let collection = client_ref.database("items").collection::<Document>(&format!("coll{}", i));
3939
///
4040
/// // Do something with the collection
4141
/// });

src/sync/coll.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use crate::{
7474
/// ```
7575
7676
#[derive(Clone, Debug)]
77-
pub struct Collection<T = Document>
77+
pub struct Collection<T>
7878
where
7979
T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync,
8080
{
@@ -153,7 +153,7 @@ where
153153
&self,
154154
pipeline: impl IntoIterator<Item = Document>,
155155
options: impl Into<Option<AggregateOptions>>,
156-
) -> Result<Cursor> {
156+
) -> Result<Cursor<Document>> {
157157
let pipeline: Vec<Document> = pipeline.into_iter().collect();
158158
RUNTIME
159159
.block_on(self.async_collection.aggregate(pipeline, options.into()))
@@ -169,7 +169,7 @@ where
169169
pipeline: impl IntoIterator<Item = Document>,
170170
options: impl Into<Option<AggregateOptions>>,
171171
session: &mut ClientSession,
172-
) -> Result<SessionCursor> {
172+
) -> Result<SessionCursor<Document>> {
173173
let pipeline: Vec<Document> = pipeline.into_iter().collect();
174174
RUNTIME
175175
.block_on(self.async_collection.aggregate_with_session(

0 commit comments

Comments
 (0)