Trait ErrorHelper

Source
pub trait ErrorHelper {
    type Error;

    // Required method
    fn err(&self, msg: impl Into<String>) -> Self::Error;

    // Provided methods
    fn fail<T>(&self, msg: impl Into<String>) -> Result<T, Self::Error> { ... }
    fn unwrap<T>(
        &self,
        opt: Option<T>,
        msg: impl Into<String>,
    ) -> Result<T, Self::Error> { ... }
    fn assert(&self, b: bool) -> Result<(), Self::Error> { ... }
    fn ok<T, E>(
        &self,
        _res: Result<T, E>,
        msg: impl Into<String>,
    ) -> Result<T, Self::Error>
       where E: Error + 'static { ... }
}
Expand description

Helper trait for re-use among our many conversion tree-walkers. Each implementer will generally have some internal state to report upon failure, which it can inject in the implementation-required err method. The fail method, provided by default, simply returns the err value.

Required Associated Types§

Required Methods§

Source

fn err(&self, msg: impl Into<String>) -> Self::Error

Create and return a Self::Error value.

Provided Methods§

Source

fn fail<T>(&self, msg: impl Into<String>) -> Result<T, Self::Error>

Return failure

Source

fn unwrap<T>( &self, opt: Option<T>, msg: impl Into<String>, ) -> Result<T, Self::Error>

Unwrap the Option opt if it is Some, and return our error if not.

Source

fn assert(&self, b: bool) -> Result<(), Self::Error>

Assert boolean condition b. Returns through self.fail if not.

Source

fn ok<T, E>( &self, _res: Result<T, E>, msg: impl Into<String>, ) -> Result<T, Self::Error>
where E: Error + 'static,

Unwrap the Result res. Return through our failure method if it is Err. Optional method, but must be implemented to be (usefully) called. The default implementation simply returns an error via self.fail.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§