Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error converting Box<Error+Send> to &Error #19279

Closed
emk opened this issue Nov 24, 2014 · 3 comments
Closed

Error converting Box<Error+Send> to &Error #19279

emk opened this issue Nov 24, 2014 · 3 comments

Comments

@emk
Copy link
Contributor

emk commented Nov 24, 2014

I'm trying to write an Error implementation which supports a set of "important" error, plus a catch-all for various minor errors caused by low-level subsystems. The code looks something like this:

use std::error::Error;

pub enum DecodingError {
    EncodingUnknown,
    //...several named errors here...
    /// Other errors, the details of which are probably unimportant.
    Unexpected(Box<Error+Send>)
}

impl Error for DecodingError {
    fn description(&self) -> &str { "decoding error" }
    fn detail(&self) -> Option<String> { None }
    fn cause(&self) -> Option<&Error> {
        match self {
            &DecodingError::Unexpected(cause) => Some(&*cause),
            _ => None
        }
    }
}

This yields the following error in cause:

:14:9: 17:10 error: mismatched types: expected core::option::Option<&std::error::Error+'static>, found core::option::Option<&std::error::Error+Send> (expected no bounds, found Send)

I should presumably be able to convert a Box<Error+Send> to an &Error safely, but I can't figure out a way to do it.

@emk
Copy link
Contributor Author

emk commented Dec 13, 2014

Here's a demonstration of this error on play: http://is.gd/W3LG9a

@steveklabnik
Copy link
Member

I believe this is a duplicate of #18524

@Guang1234567
Copy link

Guang1234567 commented Nov 22, 2019

@emk

My solution(rust 2018 nightly):

#[derive(Debug)]
pub enum LibError {
    EmptyVec,
    DotEnvErr(Box<dyn Error>),
    LogErr(Box<dyn Error>),
    DatabaseErr(Box<dyn Error>),
}


impl Error for LibError {
    fn description(&self) -> &str {
        match self {
            EmptyVec => "empty vectors not allowed",
            DotEnvErr(e) => e.description(),
            LogErr(e) => e.description(),
            DatabaseErr(e) => e.description(),
        }
    }
    fn cause(&self) -> Option<&Error> {
        match self {
            EmptyVec => None,
            DotEnvErr(e) => Some(&**e),
            LogErr(e) => Some(&**e),
            DatabaseErr(e) => Some(&**e),   //<————————  here
        }
    }
}

lnicola pushed a commit to lnicola/rust that referenced this issue Mar 10, 2025
Improve keyword completion for 'let' and 'let mut'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants