Skip to main content

ExnResult

Type Alias ExnResult 

Source
pub type ExnResult<T = (), E = Untyped> = Result<T, Exn<E>>;
Expand description

A result with an Exn<E> error, defaulting to unit success and an erased error type.

ExnResult<T> uses the same exn::Untyped marker as bare Exn. Specify E to retain a concrete error type; ExnMessageResult is the shorthand for message contexts. All standard result operations and ResultExt methods remain available. Use ResultExt::or_erased() for callbacks accepting different error types, and ? to propagate exceptions into Error at API boundaries.

use gix_error::{message, ErrorExt, ExnMessageResult, ExnResult, ResultExt};

fn parse_count(input: &str) -> ExnMessageResult<u64> {
    input.parse::<u64>().or_raise(|| message("could not parse count"))
}

fn process(callback: impl FnOnce() -> ExnResult<u64>) -> ExnMessageResult {
    let count = callback().or_raise(|| message("callback failed"))?;
    assert_eq!(count, 42, "the callback supplies the parsed count");
    Ok(())
}

let done: ExnResult = process(|| parse_count("42").or_erased()).or_erased();
done?;

let io: ExnResult<(), std::io::Error> =
    Err(std::io::Error::from(std::io::ErrorKind::NotFound).raise());
assert_eq!(io.expect_err("the I/O operation failed").error().kind(), std::io::ErrorKind::NotFound);

Aliased Type§

pub enum ExnResult<T = (), E = Untyped> {
    Ok(T),
    Err(Exn<E>),
}

Variants§

§1.0.0

Ok(T)

Contains the success value

§1.0.0

Err(Exn<E>)

Contains the error value

Trait Implementations§

Source§

impl<T, E> ResultExt for ExnResult<T, E>
where E: Error + Send + Sync + 'static,

Source§

type Success = T

The Ok type.
Source§

type Error = E

The Err type that would be wrapped in an Exn.
Source§

fn or_raise<A, F>(self, err: F) -> ExnResult<Self::Success, A>
where A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Raise a new exception on the Exn inside the Result. Read more
Source§

fn or_erased(self) -> ExnResult<Self::Success>

Raise a new exception on the Exn inside the Result, but erase its type. Read more
Source§

fn or_raise_erased<A, F>(self, err: F) -> ExnResult<Self::Success>
where A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Raise a new exception on the Exn inside the Result, and type-erase the result. Read more