Expand description
Enable conversion between non-local Error
types.
When using libraries, your code often needs to convert between
Error
types which aren’t part of your local crate. In this case you
can’t implement
std::error::FromError
.
This is also useful if you need context dependent translation of error types.
§Examples
§Simple
#[macro_use] extern crate from_error_scope;
use from_error_scope::FromErrorScope;
use std::str::Utf8Error;
use std::io::CharsError;
struct ErrorScope;
impl FromErrorScope<Utf8Error, CharsError> for ErrorScope {
fn from_error(&self, err: Utf8Error) -> CharsError {
CharsError::NotUtf8
}
}
fn from_utf8(v: &[u8]) -> Result<&str, CharsError> {
Ok(trys!(ErrorScope, std::str::from_utf8(v)))
}
fn main () {
let abc = vec![65,66,67];
println!("{}", from_utf8(&abc).unwrap())
}
Macros§
- trys
- A
try!
like macro that takes an additional type argument for a type that implementsFromErrorScope
. You may wish to wrap this macro for a specific error scope, to shorten invocations.
Traits§
- From
Error Scope - A
FromError
like trait, that can be implemented on an explicit Scope type, enabling conversion between non-localError
types.