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(transparent) on structures #80

Closed
Ekleog opened this issue Apr 27, 2020 · 2 comments
Closed

error(transparent) on structures #80

Ekleog opened this issue Apr 27, 2020 · 2 comments

Comments

@Ekleog
Copy link

Ekleog commented Apr 27, 2020

Hello,

I think I haven't had yet the occasion to thank you for thiserror (as well as a lot of other crates actually), so here it is: thank you! And sorry for not having had issues with your crates earlier, which would have allowed me to thank you without just spamming 😛

I recently hit a limitation of thiserror: sometimes, I just want to hide an error type from a sub-library, and re-export it under another name, so that the implementation details don't leak.

So I originally tried this:

#[derive(Debug, thiserror::Error)]
struct E(#[error(transparent)] std::io::Error);

However, it looks like this doesn't implement Display for E.

On the other hand, this works perfectly:

#[derive(Debug, thiserror::Error)]
enum E { #[error(transparent)] Foo(std::io::Error) }

But then, it leaks the implementation detail of the inner error type.

Do you think it'd make sense to have #[error(transparent)] work on structs? Or maybe it'd be a bad idea anyway for reasons I can't think of right now?

@dtolnay
Copy link
Owner

dtolnay commented Apr 27, 2020

The transparent attr goes on an enum variant or struct, not on an individual field. I think this does what you want:

#[derive(Error, Debug)]
#[error(transparent)]
struct E(std::io::Error);

@Ekleog
Copy link
Author

Ekleog commented Apr 27, 2020

Looks like that's what I was looking for indeed! I guess a diagnostic for #81 would probably have avoided that error, closing here :)

Thank you!

@Ekleog Ekleog closed this as completed Apr 27, 2020
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

2 participants