Idempotent

Enum Idempotent 

Source
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§

§

Executed(T)

§

AlreadyApplied

Implementations§

Source§

impl<T> Idempotent<T>

Source

pub fn was_already_applied(&self) -> bool

Returns true if the operation was ignored due to idempotency checks.

Source

pub fn did_execute(&self) -> bool

Returns true if the operation was executed.

Source

pub fn unwrap(self) -> T

Unwraps the value if executed, panics if ignored.

Source

pub fn expect(self, msg: &str) -> T

Unwraps the value if executed, panics with custom message if ignored.

Trait Implementations§

Source§

impl<T> FromAlreadyApplied for Idempotent<T>

Source§

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more