Skip to main content

Fallible

Trait Fallible 

Source
pub trait Fallible {
    type Error;
}
Expand description

A type with fallible operations that return its associated error type.

Fallible turns an error type parameter into an associated type of another parameter. You can equip an existing type with a Fallible implementation by wrapping it in a Strategy.

§Example

use rancor::{Failure, Fallible, Strategy};

trait Operator<E = <Self as Fallible>::Error> {
    fn operate(&self, lhs: i32, rhs: i32) -> Result<i32, E>;
}

impl<T: Operator<E> + ?Sized, E> Operator<E> for Strategy<T, E> {
    fn operate(&self, lhs: i32, rhs: i32) -> Result<i32, E> {
        T::operate(self, lhs, rhs)
    }
}

struct Add;

impl<E> Operator<E> for Add {
    fn operate(&self, lhs: i32, rhs: i32) -> Result<i32, E> {
        Ok(lhs + rhs)
    }
}

fn operate_one_one<T: Operator + Fallible>(
    operator: &T,
) -> Result<i32, T::Error> {
    operator.operate(1, 1)
}

assert_eq!(
    operate_one_one(Strategy::<_, Failure>::wrap(&mut Add)),
    Ok(2)
);

Required Associated Types§

Source

type Error

The error type associated with this type’s operations.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: ?Sized, E> Fallible for Strategy<T, E>

Source§

type Error = E