Skip to content

Commit

Permalink
refactoring model with Send + Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
douggynix committed Jul 9, 2024
1 parent e541c32 commit a76a737
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -136,21 +136,21 @@ pub trait CollectionConfig {
/// Utilities methods to get a `Repository`. Implemented for `mongodb::Database`.
pub trait ToRepository {
/// Shorthand for `Repository::<Model>::new`.
fn repository<M: Model + Send + Sync>(&self) -> Repository<M>;
fn repository<M: Model>(&self) -> Repository<M>;

/// Shorthand for `Repository::<Model>::new_with_options`.
fn repository_with_options<M: Model + Send + Sync>(
fn repository_with_options<M: Model>(
&self,
options: mongodb::options::CollectionOptions,
) -> Repository<M>;
}

impl ToRepository for mongodb::Database {
fn repository<M: Model + Send + Sync>(&self) -> Repository<M> {
fn repository<M: Model>(&self) -> Repository<M> {
Repository::new(self.clone())
}

fn repository_with_options<M: Model + Send + Sync>(
fn repository_with_options<M: Model>(
&self,
options: mongodb::options::CollectionOptions,
) -> Repository<M> {
Expand Down Expand Up @@ -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::{
Expand Down
15 changes: 5 additions & 10 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M: Model + Send + Sync> {
pub struct Repository<M: Model> {
db: mongodb::Database, // FIXME: temporary keep reference to database object for `bulk_update` operation
coll: mongodb::Collection<M>,
}

impl<M: Model + Send + Sync> Deref for Repository<M> {
impl<M: Model> Deref for Repository<M> {
type Target = mongodb::Collection<M>;
fn deref(&self) -> &mongodb::Collection<M> {
&self.coll
}
}

impl<M: Model + Send + Sync> Clone
for Repository<M>
{
impl<M: Model> Clone for Repository<M> {
fn clone(&self) -> Self {
Self {
db: self.db.clone(),
Expand All @@ -66,7 +64,7 @@ impl<M: Model + Send + Sync> Clone
}
}

impl<M: Model + Send + Sync> Repository<M> {
impl<M: Model> Repository<M> {
/// 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() {
Expand Down Expand Up @@ -222,9 +220,7 @@ impl<M: Model + Send + Sync> Repository<M> {
/// ```
pub fn cast_model<OtherModel>(self) -> Repository<OtherModel>
where
OtherModel: Model<CollConf = M::CollConf>
+ Send
+ Sync
OtherModel: Model<CollConf = M::CollConf>,
{
Repository {
db: self.db,
Expand Down Expand Up @@ -281,7 +277,6 @@ impl<M: Model + Send + Sync> Repository<M> {
/// ```
pub async fn bulk_update<V, U>(&self, updates: V) -> Result<BulkUpdateResult>
where
M: Send + Sync,
V: Borrow<Vec<U>> + Send + Sync,
U: Borrow<BulkUpdate> + Send + Sync,
{
Expand Down

0 comments on commit a76a737

Please sign in to comment.