diff --git a/src/coll/mod.rs b/src/coll/mod.rs index 8dcb98a42..3c90e8ffa 100644 --- a/src/coll/mod.rs +++ b/src/coll/mod.rs @@ -1,7 +1,7 @@ mod batch; pub mod options; -use std::{fmt, fmt::Debug, sync::Arc}; +use std::{borrow::Borrow, fmt, fmt::Debug, sync::Arc}; use futures::StreamExt; use serde::{ @@ -539,11 +539,11 @@ where async fn find_one_and_replace_common( &self, filter: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, session: impl Into>, ) -> Result> { - let replacement = to_document(&replacement)?; + let replacement = to_document(replacement.borrow())?; let mut options = options.into(); resolve_options!(self, options, [write_concern]); @@ -562,7 +562,7 @@ where pub async fn find_one_and_replace( &self, filter: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, ) -> Result> { self.find_one_and_replace_common(filter, replacement, options, None) @@ -579,7 +579,7 @@ where pub async fn find_one_and_replace_with_session( &self, filter: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, session: &mut ClientSession, ) -> Result> { @@ -643,13 +643,13 @@ where async fn insert_many_common( &self, - docs: impl IntoIterator, + docs: impl IntoIterator>, options: impl Into>, mut session: Option<&mut ClientSession>, ) -> Result { let docs: ser::Result> = docs .into_iter() - .map(|doc| bson::to_document(&doc)) + .map(|doc| bson::to_document(doc.borrow())) .collect(); let mut docs: Vec = docs?; @@ -739,7 +739,7 @@ where /// retryable writes. pub async fn insert_many( &self, - docs: impl IntoIterator, + docs: impl IntoIterator>, options: impl Into>, ) -> Result { self.insert_many_common(docs, options, None).await @@ -753,7 +753,7 @@ where /// retryable writes. pub async fn insert_many_with_session( &self, - docs: impl IntoIterator, + docs: impl IntoIterator>, options: impl Into>, session: &mut ClientSession, ) -> Result { @@ -762,11 +762,11 @@ where async fn insert_one_common( &self, - doc: T, + doc: impl Borrow, options: impl Into>, session: impl Into>, ) -> Result { - let doc = to_document(&doc)?; + let doc = to_document(doc.borrow())?; let mut options = options.into(); resolve_options!(self, options, [write_concern]); @@ -791,7 +791,7 @@ where /// retryable writes. pub async fn insert_one( &self, - doc: T, + doc: impl Borrow, options: impl Into>, ) -> Result { self.insert_one_common(doc, options, None).await @@ -805,7 +805,7 @@ where /// retryable writes. pub async fn insert_one_with_session( &self, - doc: T, + doc: impl Borrow, options: impl Into>, session: &mut ClientSession, ) -> Result { @@ -815,11 +815,11 @@ where async fn replace_one_common( &self, query: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, session: impl Into>, ) -> Result { - let replacement = to_document(&replacement)?; + let replacement = to_document(replacement.borrow())?; bson_util::replacement_document_check(&replacement)?; @@ -845,7 +845,7 @@ where pub async fn replace_one( &self, query: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, ) -> Result { self.replace_one_common(query, replacement, options, None) @@ -862,7 +862,7 @@ where pub async fn replace_one_with_session( &self, query: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, session: &mut ClientSession, ) -> Result { diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index f9b2e47d7..26db6e9b2 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -80,8 +80,7 @@ impl AsyncRuntime { #[cfg(any(feature = "sync", test))] pub(crate) fn block_on(self, fut: F) -> T where - F: Future + Send, - T: Send, + F: Future, { #[cfg(all(feature = "tokio-runtime", not(feature = "async-std-runtime")))] { diff --git a/src/sync/coll.rs b/src/sync/coll.rs index 370918d0f..a13f38ac6 100644 --- a/src/sync/coll.rs +++ b/src/sync/coll.rs @@ -1,7 +1,4 @@ -use std::{ - fmt::Debug, - marker::{Send, Sync}, -}; +use std::{borrow::Borrow, fmt::Debug}; use serde::{de::DeserializeOwned, Serialize}; @@ -76,14 +73,14 @@ use crate::{ #[derive(Clone, Debug)] pub struct Collection where - T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + T: Serialize + DeserializeOwned + Unpin + Debug, { async_collection: AsyncCollection, } impl Collection where - T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + T: Serialize + DeserializeOwned + Unpin + Debug, { pub(crate) fn new(async_collection: AsyncCollection) -> Self { Self { async_collection } @@ -92,7 +89,7 @@ where /// Gets a clone of the `Collection` with a different type `U`. pub fn clone_with_type(&self) -> Collection where - U: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + U: Serialize + DeserializeOwned + Unpin + Debug, { Collection::new(self.async_collection.clone_with_type()) } @@ -497,10 +494,9 @@ where /// retryable writes. pub fn insert_many( &self, - docs: impl IntoIterator, + docs: impl IntoIterator>, options: impl Into>, ) -> Result { - let docs: Vec = docs.into_iter().collect(); RUNTIME.block_on(self.async_collection.insert_many(docs, options.into())) } @@ -512,11 +508,10 @@ where /// retryable writes. pub fn insert_many_with_session( &self, - docs: impl IntoIterator, + docs: impl IntoIterator>, options: impl Into>, session: &mut ClientSession, ) -> Result { - let docs: Vec = docs.into_iter().collect(); RUNTIME.block_on(self.async_collection.insert_many_with_session( docs, options.into(), @@ -532,10 +527,13 @@ where /// retryable writes. pub fn insert_one( &self, - doc: T, + doc: impl Borrow, options: impl Into>, ) -> Result { - RUNTIME.block_on(self.async_collection.insert_one(doc, options.into())) + RUNTIME.block_on( + self.async_collection + .insert_one(doc.borrow(), options.into()), + ) } /// Inserts `doc` into the collection using the provided `ClientSession`. @@ -546,12 +544,12 @@ where /// retryable writes. pub fn insert_one_with_session( &self, - doc: T, + doc: impl Borrow, options: impl Into>, session: &mut ClientSession, ) -> Result { RUNTIME.block_on(self.async_collection.insert_one_with_session( - doc, + doc.borrow(), options.into(), session, )) @@ -566,13 +564,14 @@ where pub fn replace_one( &self, query: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, ) -> Result { - RUNTIME.block_on( - self.async_collection - .replace_one(query, replacement, options.into()), - ) + RUNTIME.block_on(self.async_collection.replace_one( + query, + replacement.borrow(), + options.into(), + )) } /// Replaces up to one document matching `query` in the collection with `replacement` using the @@ -585,13 +584,13 @@ where pub fn replace_one_with_session( &self, query: Document, - replacement: T, + replacement: impl Borrow, options: impl Into>, session: &mut ClientSession, ) -> Result { RUNTIME.block_on(self.async_collection.replace_one_with_session( query, - replacement, + replacement.borrow(), options.into(), session, )) diff --git a/src/sync/cursor.rs b/src/sync/cursor.rs index 2ca4409e4..1a3ebdec8 100644 --- a/src/sync/cursor.rs +++ b/src/sync/cursor.rs @@ -71,14 +71,14 @@ use crate::{ #[derive(Debug)] pub struct Cursor where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { async_cursor: AsyncCursor, } impl Cursor where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { pub(crate) fn new(async_cursor: AsyncCursor) -> Self { Self { async_cursor } @@ -87,7 +87,7 @@ where impl Iterator for Cursor where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { type Item = Result; @@ -118,14 +118,14 @@ where #[derive(Debug)] pub struct SessionCursor where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { async_cursor: AsyncSessionCursor, } impl SessionCursor where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { pub(crate) fn new(async_cursor: AsyncSessionCursor) -> Self { Self { async_cursor } @@ -149,14 +149,14 @@ where /// This updates the buffer of the parent `SessionCursor` when dropped. pub struct SessionCursorHandle<'cursor, 'session, T = Document> where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { async_handle: AsyncSessionCursorHandle<'cursor, 'session, T>, } impl Iterator for SessionCursorHandle<'_, '_, T> where - T: DeserializeOwned + Unpin + Send, + T: DeserializeOwned + Unpin, { type Item = Result; diff --git a/src/sync/db.rs b/src/sync/db.rs index 65733d459..1021b5802 100644 --- a/src/sync/db.rs +++ b/src/sync/db.rs @@ -1,7 +1,4 @@ -use std::{ - fmt::Debug, - marker::{Send, Sync}, -}; +use std::fmt::Debug; use serde::{de::DeserializeOwned, Serialize}; @@ -95,7 +92,7 @@ impl Database { /// used repeatedly without incurring any costs from I/O. pub fn collection(&self, name: &str) -> Collection where - T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + T: Serialize + DeserializeOwned + Unpin + Debug, { Collection::new(self.async_database.collection(name)) } @@ -112,7 +109,7 @@ impl Database { options: CollectionOptions, ) -> Collection where - T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + T: Serialize + DeserializeOwned + Unpin + Debug, { Collection::new(self.async_database.collection_with_options(name, options)) } diff --git a/src/sync/test.rs b/src/sync/test.rs index 85d89b118..b7fc23e51 100644 --- a/src/sync/test.rs +++ b/src/sync/test.rs @@ -1,7 +1,4 @@ -use std::{ - fmt::Debug, - marker::{Send, Sync}, -}; +use std::fmt::Debug; use pretty_assertions::assert_eq; use serde::{de::DeserializeOwned, Deserialize, Serialize}; @@ -30,7 +27,7 @@ fn init_db_and_coll(client: &Client, db_name: &str, coll_name: &str) -> Collecti fn init_db_and_typed_coll(client: &Client, db_name: &str, coll_name: &str) -> Collection where - T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + T: Serialize + DeserializeOwned + Unpin + Debug, { let coll = client.database(db_name).collection(coll_name); drop_collection(&coll); @@ -39,7 +36,7 @@ where pub fn drop_collection(coll: &Collection) where - T: Serialize + DeserializeOwned + Unpin + Debug + Send + Sync, + T: Serialize + DeserializeOwned + Unpin + Debug, { match coll.drop(None).as_ref().map_err(|e| &e.kind) { Err(ErrorKind::CommandError(CommandError { code: 26, .. })) | Ok(_) => {} diff --git a/src/test/coll.rs b/src/test/coll.rs index d97d66ddd..6e519213d 100644 --- a/src/test/coll.rs +++ b/src/test/coll.rs @@ -541,7 +541,7 @@ async fn empty_insert() { .database(function_name!()) .collection::(function_name!()); match coll - .insert_many(Vec::new(), None) + .insert_many(Vec::::new(), None) .await .expect_err("should get error") .kind