ddd_toolkit_core/async_event_consumption.rs
1#![allow(clippy::type_complexity)]
2
3use crate::DomainEvent;
4use std::future::Future;
5use std::pin::Pin;
6
7/// **English:** Handles borrowed domain events through boxed, `Send` futures.
8///
9/// Each future borrows the consumer mutably and the event for `'a`. No executor, retries,
10/// or deduplication are supplied. Implementations define side effects and panic conditions;
11/// dropping a pending future does not promise rollback of work already performed.
12///
13/// **日本語:** ボックス化された `Send` Future を通じて借用イベントを処理します。
14///
15/// 各 Future はコンシューマーを可変借用し、イベントを `'a` の間借用します。
16/// 実行器、再試行、重複排除は提供しません。副作用とパニック条件は実装が定め、
17/// 待機中の Future の破棄は、実行済み処理のロールバックを保証しません。
18pub trait AsyncEventConsumer {
19 /// **English:** Event that can be shared by reference across threads.
20 ///
21 /// **日本語:** スレッド間で参照を共有できるイベントです。
22 type Event: DomainEvent + Sync;
23 /// **English:** Consumer failure that can be transferred across threads.
24 ///
25 /// **日本語:** スレッド間で受け渡せるコンシューマーの失敗です。
26 type Error: Send;
27
28 /// **English:** Returns a future resolving to completion or a handler error for `event`.
29 /// Errors do not establish whether side effects occurred; retry policy belongs to the caller.
30 ///
31 /// **日本語:** `event` の処理完了またはハンドラーのエラーを返す Future を作ります。
32 /// エラーだけでは副作用の有無は判断できず、再試行方針は呼び出し側が定めます。
33 fn consume<'a>(
34 &'a mut self,
35 event: &'a Self::Event,
36 ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'a>>;
37}
38
39/// **English:** Updates a read model through boxed, `Send` futures.
40///
41/// The future holds a mutable projection borrow and a shared event borrow for `'a`.
42/// Implementations define persistence, ordering, duplicate handling, panics, and cancellation
43/// behavior; this trait adds no scheduling or locking.
44///
45/// **日本語:** ボックス化された `Send` Future を通じて読み取りモデルを更新します。
46///
47/// Future はプロジェクションの可変借用とイベントの共有借用を `'a` の間保持します。
48/// 永続化、順序、重複の扱い、パニック、キャンセル時の挙動は実装が定めます。
49/// このトレイトはスケジューリングやロックを追加しません。
50pub trait AsyncProjection {
51 /// **English:** Read-model input event with thread-shareable references.
52 ///
53 /// **日本語:** スレッド間で参照を共有できる読み取りモデルの入力イベントです。
54 type Event: DomainEvent + Sync;
55 /// **English:** Projection update error transferable across threads.
56 ///
57 /// **日本語:** スレッド間で受け渡せるプロジェクション更新エラーです。
58 type Error: Send;
59
60 /// **English:** Returns a future applying `event` and reporting completion or an update error.
61 /// Neither an error nor dropping the future guarantees that partial updates were undone.
62 ///
63 /// **日本語:** `event` を適用し、完了または更新エラーを報告する Future を返します。
64 /// エラーや Future の破棄は、部分的な更新の取り消しを保証しません。
65 fn project<'a>(
66 &'a mut self,
67 event: &'a Self::Event,
68 ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'a>>;
69}