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. Use these aliases when callers need typed exceptions or exception-tree operations; other public and private code uses Result. All standard result operations and ResultExt methods remain available. Use ResultExt::or_erased() for callbacks requiring erased exceptions, and ? to propagate exceptions into Error. Preserve this alias in public signatures that already expose a concrete error type.

use gix_error::{ErrorExt, ExnResult};

fn parse_count(input: &str) -> ExnResult<u64, std::num::ParseIntError> {
    input.parse::<u64>().map_err(ErrorExt::raise_typed)
}

let error = parse_count("not a count").expect_err("the count contains letters");
assert_eq!(
    error.error().kind(),
    &std::num::IntErrorKind::InvalidDigit,
    "callers can inspect the concrete error without downcasting"
);

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_typed<A, F>(self, err: F) -> ExnResult<Self::Success, A>
where A: Error + Send + Sync + 'static, F: FnOnce() -> A,

Like Self::or_raise(), retaining the context’s type in an exception.
Source§

fn or_error(self) -> Result<T>

Convert to a public Result without adding context. Native errors are raised at the caller; existing Error values and exceptions retain their locations.
Source§

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

Raise a new exception on the Exn inside the Result, but erase its type. Errors implementing std::error::Error use ErrorExt::raise_erased. Read more
Source§

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

Add err() as context on failure, returning a public Result. Read more
Source§

fn or_raise_erased<A, F>(self, err: F) -> ExnResult<Self::Success>
where Self: Sized, 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