pub enum Idempotent<T> {
Executed(T),
AlreadyApplied,
}Expand description
Signals if a mutation is applied or was skipped.
Distinguishes between operations that were executed versus those that were
ignored due to idempotency checks.
The idempotency_guard macro provides an easy way to do such checks.
§Examples
use es_entity::{idempotency_guard, Idempotent};
pub enum UserEvent{
Initialized {id: u64, name: String},
NameUpdated {name: String}
}
pub struct User{
events: Vec<UserEvent>
}
impl User{
// This returns `Idempotent<T>` where T is the return value we get after the event is processed
pub fn update_name(&mut self, new_name: impl Into<String>) -> Idempotent<()>{
let name = new_name.into();
idempotency_guard!(
self.events.iter().rev(),
UserEvent::NameUpdated { name: existing_name } if existing_name == &name
);
self.events.push(UserEvent::NameUpdated{name});
Idempotent::Executed(())
}
}
fn example(){
let mut user = User{ events: vec![] };
assert!(user.update_name("Alice").did_execute());
// updating "Alice" executes as no such event has been processed before.
// Signalled by returning `Idempotent::Executed(T)`, validated with `did_execute` helper method
assert!(user.update_name("Alice").was_already_applied());
// updating "Alice" again ignored because same event has been processed before.
// Signalled by returning `Idempotent::AlreadyApplied` early, validated with `was_already_applied` helper method
}Variants§
Implementations§
Source§impl<T> Idempotent<T>
impl<T> Idempotent<T>
Sourcepub fn was_already_applied(&self) -> bool
pub fn was_already_applied(&self) -> bool
Returns true if the operation was ignored due to idempotency checks.
Sourcepub fn did_execute(&self) -> bool
pub fn did_execute(&self) -> bool
Returns true if the operation was executed.
Trait Implementations§
Source§impl<T> FromAlreadyApplied for Idempotent<T>
impl<T> FromAlreadyApplied for Idempotent<T>
Source§fn from_already_applied() -> Self
fn from_already_applied() -> Self
to handle Idempotent<T> return type
Auto Trait Implementations§
impl<T> Freeze for Idempotent<T>where
T: Freeze,
impl<T> RefUnwindSafe for Idempotent<T>where
T: RefUnwindSafe,
impl<T> Send for Idempotent<T>where
T: Send,
impl<T> Sync for Idempotent<T>where
T: Sync,
impl<T> Unpin for Idempotent<T>where
T: Unpin,
impl<T> UnwindSafe for Idempotent<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more