Skip to content

Commit

Permalink
Prefer Deref default impl rather than box (#126)
Browse files Browse the repository at this point in the history
* Implement deref impl for EventStore let the impl more generic

* clippy::let-underscore-drop
  • Loading branch information
cottinisimone authored Oct 11, 2022
1 parent 17ebf6a commit fc0cd38
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/esrs/postgres/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ where
match projector.consistency() {
Consistency::Strong => projector.project(store_event, &mut transaction).await?,
Consistency::Eventual => {
let _ = projector.project(store_event, &mut transaction).await;
let _result = projector.project(store_event, &mut transaction).await;
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/esrs/store.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Deref;

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use uuid::Uuid;
Expand Down Expand Up @@ -34,8 +36,9 @@ pub trait EventStore {
async fn delete(&self, aggregate_id: Uuid) -> Result<(), <Self::Manager as Aggregate>::Error>;
}

/// Default generic implementation for every type [`Box<dyn EventStore>`]. This is particularly useful
/// when there's the need in your codebase to have a generic [`EventStore`] inside your [`Aggregate`].
/// Default generic implementation for every type implementing [`Deref`] where its `Target` is a
/// `dyn` [`EventStore`]. This is particularly useful when there's the need in your codebase to have
/// a generic [`EventStore`] inside your [`Aggregate`].
///
/// # Example
///
Expand All @@ -59,17 +62,19 @@ pub trait EventStore {
/// }
/// ```
#[async_trait]
impl<M> EventStore for Box<dyn EventStore<Manager = M> + Send + Sync>
impl<M, T> EventStore for T
where
T: Deref<Target = dyn EventStore<Manager = M> + Send + Sync> + Sync,
M: AggregateManager,
<M as Aggregate>::Event: 'static,
{
type Manager = M;

async fn by_aggregate_id(
&self,
aggregate_id: Uuid,
) -> Result<Vec<StoreEvent<<Self::Manager as Aggregate>::Event>>, <Self::Manager as Aggregate>::Error> {
self.as_ref().by_aggregate_id(aggregate_id).await
self.deref().by_aggregate_id(aggregate_id).await
}

async fn persist(
Expand All @@ -78,13 +83,13 @@ where
events: Vec<<Self::Manager as Aggregate>::Event>,
starting_sequence_number: SequenceNumber,
) -> Result<Vec<StoreEvent<<Self::Manager as Aggregate>::Event>>, <Self::Manager as Aggregate>::Error> {
self.as_ref()
self.deref()
.persist(aggregate_id, events, starting_sequence_number)
.await
}

async fn delete(&self, aggregate_id: Uuid) -> Result<(), <Self::Manager as Aggregate>::Error> {
self.as_ref().delete(aggregate_id).await
self.deref().delete(aggregate_id).await
}
}

Expand Down

0 comments on commit fc0cd38

Please sign in to comment.