pub trait ResultFrom<ST>: TryFrom<ST> {
    // Provided method
    fn result_from(status: ST) -> Result<<Self as TryFrom<ST>>::Error, Self> { ... }
}
Expand description

A trait to add to an error type which can be constructed from an underlying “status” type which contains both success and failure codes.

The goal is to be able to convert a unified status type into a Result<T, Error> type.

§Examples

use mc_sgx_util::ResultFrom;

/// An example FFI type
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct a_status_t(u32);

impl a_status_t {
    const SUCCESS: a_status_t = a_status_t(0);
    const FAIL: a_status_t = a_status_t(1);
}

/// An example rusty enum wrapper for a_status_t
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum AnError {
    Stuff,
    Unknown
}

impl TryFrom<a_status_t> for AnError {
    type Error = ();

    fn try_from(value: a_status_t) -> Result<Self, ()> {
        match value {
            a_status_t::SUCCESS => Err(()),
            a_status_t::FAIL => Ok(AnError::Stuff),
            _ => Ok(AnError::Unknown)
        }
    }
}

// Pick up the default implementation of [`ResultFrom`]
impl ResultFrom<a_status_t> for AnError {}

let status = a_status_t::SUCCESS;
assert_eq!(Ok(()), AnError::result_from(status));

let status = a_status_t::FAIL;
assert_eq!(Err(AnError::Stuff), AnError::result_from(status));

Provided Methods§

source

fn result_from(status: ST) -> Result<<Self as TryFrom<ST>>::Error, Self>

Flips the result of a TryFrom.

Object Safety§

This trait is not object safe.

Implementors§