pub struct AggregateSnapshot<S> { /* private fields */ }Expand description
English:
Owned aggregate state paired with the last event revision it represents.
No identifier or schema version is added; include validation metadata in S when needed.
日本語:
集約状態と、その状態が反映する最後のイベントのリビジョンを所有します。
識別子やスキーマバージョンは追加しません。検証用メタデータは必要に応じて S に含めます。
§Example / 使用例
use ddd_toolkit_core::{AggregateSnapshot, StreamRevision};
let snapshot = AggregateSnapshot::new(42_u64, StreamRevision::new(3));
assert_eq!(*snapshot.state(), 42);
assert_eq!(snapshot.into_parts(), (42, StreamRevision::new(3)));Implementations§
Source§impl<S> AggregateSnapshot<S>
impl<S> AggregateSnapshot<S>
Sourcepub fn new(state: S, revision: StreamRevision) -> AggregateSnapshot<S>
pub fn new(state: S, revision: StreamRevision) -> AggregateSnapshot<S>
English:
Pairs state with revision without checking consistency or compatibility.
日本語:
整合性や互換性を検証せず、state と revision を組み合わせます。
Examples found in repository?
examples/event_sourcing.rs (line 158)
151 fn load(
152 &self,
153 id: &u64,
154 ) -> Result<Option<AggregateSnapshot<u32>>, SnapshotLoadError<Infallible>> {
155 Ok(self
156 .0
157 .get(id)
158 .map(|snapshot| AggregateSnapshot::new(*snapshot.state(), snapshot.revision())))
159 }
160 fn save(
161 &mut self,
162 id: &u64,
163 snapshot: AggregateSnapshot<u32>,
164 ) -> Result<(), SnapshotSaveError<Infallible>> {
165 self.0.insert(*id, snapshot);
166 Ok(())
167 }
168}
169fn history(store: &MemoryEvents) -> StreamHistory<Added> {
170 let LoadResult::Found(history) = store.load(&1).unwrap() else {
171 panic!("stream missing");
172 };
173 history
174}
175fn main() {
176 let mut store = MemoryEvents::default();
177 assert!(matches!(store.load(&1).unwrap(), LoadResult::NoStream(_)));
178 let first = store
179 .append(&1, ExpectedRevision::NoStream(NoStream), vec![Added::V1(2)])
180 .unwrap();
181 assert_eq!(first.resulting_revision, StreamRevision::new(0));
182 let mut replayed = replay_aggregate::<Counter>(&1, &history(&store)).unwrap();
183 assert_eq!(replayed.aggregate.value, 2);
184 assert!(replayed.aggregate.pull_events().is_empty());
185 let mut snapshots = MemorySnapshots::default();
186 snapshots
187 .save(
188 &1,
189 AggregateSnapshot::new(replayed.aggregate.value, replayed.revision),
190 )
191 .unwrap();
192 store
193 .append(
194 &1,
195 ExpectedRevision::Revision(replayed.revision),
196 vec![Added::V2 { amount: 3 }],
197 )
198 .unwrap();
199 assert!(matches!(
200 store.append(
201 &1,
202 ExpectedRevision::Revision(replayed.revision),
203 vec![Added::V1(99)]
204 ),
205 Err(AppendError::Conflict(_))
206 ));
207 let history = history(&store);
208 let mut restored =
209 replay_with_optional_snapshot::<Counter>(&1, &history, snapshots.load(&1).unwrap())
210 .unwrap();
211 let full = replay_aggregate::<Counter>(&1, &history).unwrap();
212 assert_eq!(restored.aggregate.value, 5);
213 assert_eq!(restored.aggregate.value, full.aggregate.value);
214 assert_eq!(restored.revision, StreamRevision::new(1));
215 assert!(restored.aggregate.pull_events().is_empty());
216 println!("event_sourcing: upcast v1; rejected stale write; snapshot + tail = 5");
217}Sourcepub fn revision(&self) -> StreamRevision
pub fn revision(&self) -> StreamRevision
English:
Returns the last event revision represented by the state.
日本語:
状態が反映する最後のイベントのリビジョンを返します。
Sourcepub fn into_parts(self) -> (S, StreamRevision)
pub fn into_parts(self) -> (S, StreamRevision)
English:
Consumes the snapshot, returning state and revision without cloning.
日本語:
スナップショットを消費し、複製せずに状態とリビジョンを返します。
Trait Implementations§
Source§impl<S> Debug for AggregateSnapshot<S>where
S: Debug,
impl<S> Debug for AggregateSnapshot<S>where
S: Debug,
impl<S> Eq for AggregateSnapshot<S>where
S: Eq,
Source§impl<S> PartialEq for AggregateSnapshot<S>where
S: PartialEq,
impl<S> PartialEq for AggregateSnapshot<S>where
S: PartialEq,
impl<S> StructuralPartialEq for AggregateSnapshot<S>where
S: PartialEq,
Auto Trait Implementations§
impl<S> Freeze for AggregateSnapshot<S>where
S: Freeze,
impl<S> RefUnwindSafe for AggregateSnapshot<S>where
S: RefUnwindSafe,
impl<S> Send for AggregateSnapshot<S>where
S: Send,
impl<S> Sync for AggregateSnapshot<S>where
S: Sync,
impl<S> Unpin for AggregateSnapshot<S>where
S: Unpin,
impl<S> UnsafeUnpin for AggregateSnapshot<S>where
S: UnsafeUnpin,
impl<S> UnwindSafe for AggregateSnapshot<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more