pub trait ResultExt {
type OkType;
type ErrType;
// Required methods
fn chain_err<C, K, F>(self, op: F) -> Result<Self::OkType, ChainedError<K>>
where F: FnOnce() -> C,
K: ErrorKind,
C: ErrorContext,
Self::ErrType: Into<ChainedError<K>>;
fn into_chained<C, K, F>(
self,
op: F,
) -> Result<Self::OkType, ChainedError<K>>
where F: FnOnce() -> C,
K: ErrorKind + From<Self::ErrType>,
C: ErrorContext;
fn convert<K, U>(self) -> Result<Self::OkType, ChainedError<U>>
where K: ErrorKind,
Self::ErrType: Into<ChainedError<K>>,
U: From<K> + ErrorKind;
fn convert_with<K, U, F>(
self,
converter: F,
) -> Result<Self::OkType, ChainedError<U>>
where K: ErrorKind,
Self::ErrType: Into<ChainedError<K>>,
U: ErrorKind,
F: FnOnce(K) -> U;
}Expand description
Result extension to integrate with ChainedError
Required Associated Types§
Required Methods§
Sourcefn chain_err<C, K, F>(self, op: F) -> Result<Self::OkType, ChainedError<K>>
fn chain_err<C, K, F>(self, op: F) -> Result<Self::OkType, ChainedError<K>>
Almost same as chain_err, but takes closure.
If you want to do expensive operation like format! to generate error message,
use this method instead of chain_err.
§Usage
use error_chain_mini::*;
#[derive(ErrorKind, Eq, PartialEq, Debug)]
#[msg(short = "My Error")]
struct MyError;
fn my_func() -> Result<(), ChainedError<MyError>>{
let chained = MyError{}.into_with(|| "Error in my_func");
Err(chained)
}
let chained = my_func().chain_err(|| "Chained");
assert!(chained.is_err());
if let Err(e) = chained {
let msg = format!("{}", e);
assert_eq!(msg, r#"
kind: My Error
context 0: Error in my_func
context 1: Chained
"#);
}Sourcefn into_chained<C, K, F>(self, op: F) -> Result<Self::OkType, ChainedError<K>>
fn into_chained<C, K, F>(self, op: F) -> Result<Self::OkType, ChainedError<K>>
Takes Result and context then convert its error type into ChainedError with context.
§Usage
use error_chain_mini::*;
use std::io;
use std::fs::File;
#[derive(Debug, ErrorKind)]
enum MyError {
#[msg(short = "io error", detailed = "{:?}", _0)]
Io(io::Error),
Misc
}
impl From<io::Error> for MyError {
fn from(e: io::Error) -> Self {
MyError::Io(e)
}
}
let file: Result<_, ChainedError<MyError>> = File::open("not_existing_file").into_chained(|| "In io()");Sourcefn convert<K, U>(self) -> Result<Self::OkType, ChainedError<U>>
fn convert<K, U>(self) -> Result<Self::OkType, ChainedError<U>>
Convert Result<Ok, ChainedError<T>> into Result<Ok, ChainedError<U>> using std::convert::From.
§Usage
mod external {
#[derive(ErrorKind, Eq, PartialEq, Debug)]
#[msg(short = "error in external")]
pub struct ExtErrorKind;
pub type Error = ChainedError<ExtErrorKind>;
pub fn func() -> Result<(), Error> {
Err(ExtErrorKind{}.into_with(|| "In external::func()"))
}
}
use external::{self, ExtErrorKind};
#[derive(ErrorKind, Eq, PartialEq, Debug)]
enum MyErrorKind {
Internal,
#[msg(short = "from mod 'external'", detailed = "{:?}", _0)]
External(ExtErrorKind),
};
impl From<ExtErrorKind> for MyErrorKind {
fn from(e: ExtErrorKind) -> MyErrorKind {
MyErrorKind::External(e)
}
}
type Error = ChainedError<MyErrorKind>;
let chained: Result<(), Error> = external::func().convert();
if let Err(chained) = chained {
assert_eq!(*chained.kind(), MyErrorKind::External(ExtErrorKind {}));
}Sourcefn convert_with<K, U, F>(
self,
converter: F,
) -> Result<Self::OkType, ChainedError<U>>
fn convert_with<K, U, F>( self, converter: F, ) -> Result<Self::OkType, ChainedError<U>>
Convert Result<Ok, ChainedError<T>> into Result<Ok, ChainedError<U>> using closure.
§Usage
mod external {
#[derive(ErrorKind, Eq, PartialEq, Debug)]
#[msg(short = "error in external")]
pub struct ExtErrorKind;
pub type Error = ChainedError<ExtErrorKind>;
pub fn func() -> Result<(), Error> {
Err(ExtErrorKind{}.into_with(|| "In external::func()"))
}
}
use external::{self, ExtErrorKind};
#[derive(ErrorKind, Eq, PartialEq, Debug)]
enum MyErrorKind {
Internal,
#[msg(short = "from mod 'external'", detailed = "{:?}", _0)]
External(ExtErrorKind),
};
type Error = ChainedError<MyErrorKind>;
let chained: Result<(), Error>
= external::func().convert_with(|e| MyErrorKind::External(e));
if let Err(chained) = chained {
assert_eq!(*chained.kind(), MyErrorKind::External(ExtErrorKind {}));
}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.