Skip to main content

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