eventually/version.rs
1//! Contains the types necessary for Optimistic Locking through versioning.
2
3/// A version used for Optimistic Locking.
4///
5/// Used by the [`aggregate::Root`][crate::aggregate::Root] to avoid concurrency issues,
6/// and [`event::Store`][crate::event::Store] to implement stream-local ordering to the messages.
7pub type Version = u64;
8
9/// Used to set a specific expectation during an operation
10/// that mutates some sort of resource (e.g. an [Event Stream][crate::event::Stream])
11/// that supports versioning.
12///
13/// It allows for optimistic locking, avoiding data races
14/// when modifying the same resource at the same time.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum Check {
17 /// Disables any kind of optimistic locking check, allowing
18 /// for any [Version] to be used compared to the new one.
19 Any,
20 /// Expects that the previous [Version] used for the operation
21 /// must have the value specified.
22 MustBe(Version),
23}
24
25/// This error is returned by a function when a version conflict error has
26/// been detected.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
28#[error("conflict error detected, expected version was: {expected}, found: {actual}")]
29pub struct ConflictError {
30 /// The [Version] value that was expected when calling the function that failed.
31 pub expected: Version,
32
33 /// The actual [Version] value, which mismatch caused this error.
34 pub actual: Version,
35}