pub struct UpcasterChain<Event, Upcaster> { /* private fields */ }Expand description
English:
Insertion-ordered sequence of upcasters of a single concrete type.
Each stage consumes the preceding output. The chain does not select stages by event identity,
sort versions, or restore an event after failure. Use an enum or another common upcaster type
when stages need different behaviors. It stores no event value and adds no locking.
Add stages explicitly; each successful result becomes the next input.
日本語:
同じ具体型の変換器を追加順に保持する列です。
各段階は直前の出力を消費します。イベント識別情報による段階の選択、バージョンの並べ替え、
失敗後のイベント復元はしません。異なる動作の段階には列挙型などの共通の変換器型を用います。
イベント値は保持せず、ロックも追加しません。
段階を明示的に追加すると、各段階の成功結果が次の入力になります。
§Example / 使用例
use ddd_toolkit_core::{EventUpcaster, UpcasterChain};
struct Append(&'static str);
impl EventUpcaster<String> for Append {
type Error = std::convert::Infallible;
fn upcast(&self, mut event: String) -> Result<String, Self::Error> {
event.push_str(self.0);
Ok(event)
}
}
let mut chain = UpcasterChain::new();
chain.push(Append("-v2"));
chain.push(Append("-v3"));
assert_eq!(chain.apply(String::from("v1")).unwrap(), "v1-v2-v3");
assert_eq!(chain.len(), 2);Implementations§
Source§impl<Event, Upcaster> UpcasterChain<Event, Upcaster>
impl<Event, Upcaster> UpcasterChain<Event, Upcaster>
Sourcepub fn new() -> UpcasterChain<Event, Upcaster>
pub fn new() -> UpcasterChain<Event, Upcaster>
English:
Creates an empty chain, also used by Default::default.
Applying an empty chain returns the input unchanged.
日本語:
Default::default でも使う空の列を作ります。
空の列を適用すると、入力を変更せず返します。
Sourcepub fn push(&mut self, upcaster: Upcaster)
pub fn push(&mut self, upcaster: Upcaster)
English:
Appends upcaster as the last stage without inspecting its version or running it.
日本語:
バージョンを調べたり実行したりせず、upcaster を最後の段階に追加します。
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
English:
Returns the number of stored stages, not the number of supported event versions.
日本語:
対応するイベントバージョン数ではなく、保持する段階数を返します。
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
English:
Returns true when no upcasters have been added.
日本語:
変換器を一つも追加していない場合に true を返します。
Sourcepub fn apply(
&self,
event: Event,
) -> Result<Event, <Upcaster as EventUpcaster<Event>>::Error>where
Upcaster: EventUpcaster<Event>,
pub fn apply(
&self,
event: Event,
) -> Result<Event, <Upcaster as EventUpcaster<Event>>::Error>where
Upcaster: EventUpcaster<Event>,
English:
Runs all stages in insertion order and returns the final owned event.
The first error stops evaluation and is returned unchanged; later stages are not called.
Earlier side effects are not rolled back. An empty chain returns Ok(event).
Panics from a stage propagate, and the chain itself remains stored for future calls.
日本語:
全段階を追加順に実行し、最後の所有されたイベントを返します。
最初のエラーで評価を止め、変更せず返します。後続の段階は呼び出しません。
先行する副作用は取り消しません。空の列は Ok(event) を返します。
段階のパニックは伝播し、列自体は後続の呼び出しにも使えるよう保持されます。