简体   繁体   中英

Implementing a generic conversion from an object implementing the `Error` trait

I cannot get the following code to compile.

  • I get an error that From is already implemented.
  • If I remove the manual impl of From I get the error that From is not implemented.
  • If I do not implement Error it works fine.

I suppose that this is due to the blank impl impl<T> From<T> for T in core. How should I work around this? Not implementing Error is not really an option.

Code ( playground )

use std::fmt;
use std::io;
use std::error::Error;

#[derive(Debug)]
enum ErrorType {
    Other(Box<Error>)
}

impl fmt::Display for ErrorType {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("not implemented")
    }
}

impl Error for ErrorType {
    fn description(&self) -> &str {
        use self::ErrorType::*;
        match *self {
            Other(ref err) => err.description(),
        }
    }
}

impl<E: Error + 'static> From<E> for ErrorType {
    fn from(other: E) -> Self {
        ErrorType::Other(Box::new(other))
    }
}

fn ret_io_err() -> Result<(), io::Error> {
    Ok(())
}

fn ret_error_type() -> Result<(), ErrorType> {
    try!(ret_io_err());
    Ok(())
}

fn main() {}

You don't.

That implementation would be a bit useless, anyway. It would mean that all other error types would just be immediately boxed. At that point, you might as well just make use of the existing conversions involving Box<Error> and use that instead.

If you want an error type that actually preserves the type of the unified errors, you'll need to implement From once for each of them. The error-type crate can help with defining unifying error types.

According comments above I've found this one approach works only:

impl<T: error::Error + 'static> From<Box<T>> for Error {
    fn from(e: Box<T>) -> Self {
        Error::Other(e)
    }
}

To use it you should Box errors:

try!(any_result().map_err(Box::new))

But if you need generic error (for user-defined tasks, as example) you don't need to wrap it with enum, try to use it directly:

trait AnyTask {
    fn do_it(&self) -> Result<Success, Box<Error>>;
}

It makes possible to use try! or .into() everywhere. This way:

fn do_it(&self) -> Result<Succeed, Box<Error>> {

    let row = try!(self.database.find_last());

    if row.some_condition {
        return Err("I don't like it!".into());
    }

    try!(self.database.insert(row));

    Ok(Success)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM