From a76a737560a218cc44fc7ff0e68d583993850c90 Mon Sep 17 00:00:00 2001 From: James Douglass Date: Tue, 9 Jul 2024 00:15:58 -0400 Subject: [PATCH] refactoring model with Send + Sync --- Cargo.toml | 7 ------- src/lib.rs | 12 ++++++------ src/repository.rs | 15 +++++---------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f3470f3..77a9396 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,15 +17,8 @@ serde = { version = "1", features = ["derive"] } futures-core = "0.3" futures-util = "0.3" async-trait = "0.1" -#tokio = { version = "1", features = ["full"] } [dev-dependencies] tokio = "1.14.0" pretty_assertions = "1.0.0" chrono = { version = "0.4.38", features = [ "serde" ] } - -#[features] -#default = ["tokio-runtime"] -#tokio-runtime = ["mongodb/tokio-runtime"] -#async-std-runtime = ["mongodb/async-std-runtime"] -#chrono-0_4 = ["mongodb/bson-chrono-0_4"] diff --git a/src/lib.rs b/src/lib.rs index e04f524..a56c5c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,7 +108,7 @@ pub use mongodb::bson; pub use mongodb::bson::{bson, doc}; /// Associate a collection configuration. -pub trait Model: serde::ser::Serialize + serde::de::DeserializeOwned + Unpin { +pub trait Model: serde::ser::Serialize + serde::de::DeserializeOwned + Unpin + Send + Sync { type CollConf: CollectionConfig; } @@ -136,21 +136,21 @@ pub trait CollectionConfig { /// Utilities methods to get a `Repository`. Implemented for `mongodb::Database`. pub trait ToRepository { /// Shorthand for `Repository::::new`. - fn repository(&self) -> Repository; + fn repository(&self) -> Repository; /// Shorthand for `Repository::::new_with_options`. - fn repository_with_options( + fn repository_with_options( &self, options: mongodb::options::CollectionOptions, ) -> Repository; } impl ToRepository for mongodb::Database { - fn repository(&self) -> Repository { + fn repository(&self) -> Repository { Repository::new(self.clone()) } - fn repository_with_options( + fn repository_with_options( &self, options: mongodb::options::CollectionOptions, ) -> Repository { @@ -178,7 +178,7 @@ pub mod prelude { JavaScriptCodeWithScope as BsonJavaScriptCodeWithScope, Regex as BsonRegex, Serializer as BsonSerializer, Timestamp as BsonTimestamp, }; - //See driver pull request change for errors: https://github.com/mongodb/mongo-rust-driver/pull/1102 + #[doc(no_inline)] pub use crate::mongo::{ error::{ diff --git a/src/repository.rs b/src/repository.rs index 72c8785..3762253 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -43,21 +43,19 @@ pub struct BulkUpdateUpsertResult { /// This type can safely be copied and passed around because `std::sync::Arc` is used internally. /// Underlying `mongodb::Collection` can be retrieved at anytime with `Repository::get_underlying`. #[derive(Debug)] -pub struct Repository { +pub struct Repository { db: mongodb::Database, // FIXME: temporary keep reference to database object for `bulk_update` operation coll: mongodb::Collection, } -impl Deref for Repository { +impl Deref for Repository { type Target = mongodb::Collection; fn deref(&self) -> &mongodb::Collection { &self.coll } } -impl Clone - for Repository -{ +impl Clone for Repository { fn clone(&self) -> Self { Self { db: self.db.clone(), @@ -66,7 +64,7 @@ impl Clone } } -impl Repository { +impl Repository { /// Create a new repository from the given mongo client. pub fn new(db: mongodb::Database) -> Self { let coll = if let Some(options) = M::CollConf::collection_options() { @@ -222,9 +220,7 @@ impl Repository { /// ``` pub fn cast_model(self) -> Repository where - OtherModel: Model - + Send - + Sync + OtherModel: Model, { Repository { db: self.db, @@ -281,7 +277,6 @@ impl Repository { /// ``` pub async fn bulk_update(&self, updates: V) -> Result where - M: Send + Sync, V: Borrow> + Send + Sync, U: Borrow + Send + Sync, {