Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Deref default impl rather than box #126

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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