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

Add transparent attribute for delegating Error impl to one field #50

Merged
merged 5 commits into from Dec 1, 2019

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Dec 1, 2019

This is useful for hiding error variants from a library's public error type:

#[derive(Error, Debug)]
#[error(transparent)]  // source and Display delegate to ErrorKind
pub struct Error(ErrorKind);

#[derive(Error, Debug)]
/*private*/ enum ErrorKind {
    #[error("...")]
    E0,
    #[error("...")]
    E1(#[source] io::Error),
}

And also for enums that need an "anything else" variant; such variants tend not to have their own Display message but just forward through to the underlying error's Display and source:

#[derive(Error, Debug)]
pub enum MyError {
    ...

    #[error(transparent)]
    Other(#[from] anyhow::Error),  // source and Display delegate to anyhow::Error
}

Closes #40.

@dtolnay dtolnay merged commit 62e8e66 into master Dec 1, 2019
@dtolnay dtolnay deleted the transparent branch December 1, 2019 06:34
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

Successfully merging this pull request may close these issues.

Attribute for delegating Error impl to one field
1 participant