Skip to main content

ErrorState

Struct ErrorState 

Source
pub struct ErrorState<Error, Origin, Next> {
    pub err: Error,
    pub at: FailedAt<Origin, Next>,
}
Expand description

Wraps an error with information about what state survived the failed transition.

ErrorState implements State, it can be used as a state in error recovery transitions.

Note that we do not depend on core::error::Error, for all our use cases requiring only Debug is sufficient.

Fields§

§err: Error

Actual error

§at: FailedAt<Origin, Next>

Recovered state

Implementations§

Source§

impl<E, Origin, Next> ErrorState<E, Origin, Next>

ErrorState implements constructors that ergonomically create Result::Err instances.

Source

pub const fn err(err: E) -> Result<Next, Self>

Error with no state recoverable state. The at field is set to None.

§Example

#[derive(Debug)]
enum MyError { Bad }

impl StateError for MyError {}

#[derive(Debug)]
struct Src;
impl State<MyError> for Src {}

#[derive(Debug)]
struct Dst;
impl State<MyError> for Dst {}

impl Transition<Dst, MyError> for Src {
    fn change(self, _ctx: &dyn Any) -> TransitionResult<Src, Dst, MyError> {
        // Change fails, source consumed, nothing survived
        ErrorState::err(MyError::Bad)
    }
}

let result = Src.try_transition();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(error.at, FailedAt::None));
Source

pub const fn err_origin(err: E, origin: Origin) -> Result<Next, Self>

Error with origin state preserved, invariant/leave/guard failed. The at field will contain FailedAt::Origin(self).

§Example

#[derive(Debug)]
enum MyError { Bad }

impl StateError for MyError {}

#[derive(Debug)]
struct Src;
impl State<MyError> for Src {}

#[derive(Debug)]
struct Dst;
impl State<MyError> for Dst {}

impl Transition<Dst, MyError> for Src {
    fn change(self, _ctx: &dyn Any) -> TransitionResult<Src, Dst, MyError> {
        // Change fails, origin state preserved for recovery
        ErrorState::err_origin(MyError::Bad, self)
    }
}

let result = Src.try_transition();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(error.at, FailedAt::Origin(_)));
Examples found in repository?
examples/02_vending/main.rs (line 142)
133    fn change(self, item: &dyn Any) -> TransitionResult<HasCredit, Dispensing, VendError> {
134        let item = item.downcast_ref::<Item>().unwrap();
135        if self.amount >= item.cost {
136            Ok(Dispensing {
137                item: item.name,
138                price: item.cost,
139                change: self.amount - item.cost,
140            })
141        } else {
142            ErrorState::err_origin(VendError::NotEnoughCredit, self)
143        }
144    }
Source

pub const fn err_next(err: E, next: Next) -> Result<Next, Self>

Error with next state preserved, enter/invariant on target failed. The at field will contain FailedAt::Next(next).

§Example

#[derive(Debug)]
enum MyError { Bad }

impl StateError for MyError {}

#[derive(Debug)]
struct Src;
impl State<MyError> for Src {}

#[derive(Debug)]
struct Dst;
impl State<MyError> for Dst {}

impl Transition<Dst, MyError> for Src {
    fn change(self, _ctx: &dyn Any) -> TransitionResult<Src, Dst, MyError> {
        let next = Dst;
        // Target constructed but invalid; return it so caller can recover
        ErrorState::err_next(MyError::Bad, next)
    }
}

let result = Src.try_transition();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(error.at, FailedAt::Next(_)));

Trait Implementations§

Source§

impl<Error: Debug, Origin: Debug, Next: Debug> Debug for ErrorState<Error, Origin, Next>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Error: PartialEq, Origin: PartialEq, Next: PartialEq> PartialEq for ErrorState<Error, Origin, Next>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<Error, Origin, Next> State for ErrorState<Error, Origin, Next>

Any ErrorState itself implements State<Infallible> thus they can be used in a statemachine to recover from errors.

Source§

fn invariant(&self) -> Result<(), Error>

Invariants check on States. Can be used for debugging (conditionally implemented). The blanket implementation is a no-op, when not required then don’t implement it. Its on the users decision if invariant may panic or return an error. Read more
Source§

fn leave(&self, _ctx: &dyn Any) -> Result<(), Error>

Called before leaving this state. Read more
Source§

fn enter(&self, _ctx: &dyn Any) -> Result<(), Error>

Called after entering this state. Read more
Source§

impl<Error: PartialEq, Origin: PartialEq, Next: PartialEq> StructuralPartialEq for ErrorState<Error, Origin, Next>

Auto Trait Implementations§

§

impl<Error, Origin, Next> Freeze for ErrorState<Error, Origin, Next>
where Error: Freeze, FailedAt<Origin, Next>: Freeze,

§

impl<Error, Origin, Next> RefUnwindSafe for ErrorState<Error, Origin, Next>
where Error: RefUnwindSafe, FailedAt<Origin, Next>: RefUnwindSafe,

§

impl<Error, Origin, Next> Send for ErrorState<Error, Origin, Next>
where Error: Send, FailedAt<Origin, Next>: Send,

§

impl<Error, Origin, Next> Sync for ErrorState<Error, Origin, Next>
where Error: Sync, FailedAt<Origin, Next>: Sync,

§

impl<Error, Origin, Next> Unpin for ErrorState<Error, Origin, Next>
where Error: Unpin, FailedAt<Origin, Next>: Unpin,

§

impl<Error, Origin, Next> UnsafeUnpin for ErrorState<Error, Origin, Next>
where Error: UnsafeUnpin, FailedAt<Origin, Next>: UnsafeUnpin,

§

impl<Error, Origin, Next> UnwindSafe for ErrorState<Error, Origin, Next>
where Error: UnwindSafe, FailedAt<Origin, Next>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.