Skip to content

Commit

Permalink
Copy docs on struct error(transparent) into readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 25, 2022
1 parent 5271eb3 commit f062061
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -165,6 +165,27 @@ pub enum DataStoreError {
}
```

Another use case is hiding implementation details of an error representation
behind an opaque error type, so that the representation is able to evolve
without breaking the crate's public API.

```rust
// PublicError is public, but opaque and easy to keep compatible.
#[derive(Error, Debug)]
#[error(transparent)]
pub struct PublicError(#[from] ErrorRepr);

impl PublicError {
// Accessors for anything we do want to expose publicly.
}

// Private and free to change across minor version of the crate.
#[derive(Error, Debug)]
enum ErrorRepr {
...
}
```

- See also the [`anyhow`] library for a convenient single error type to use in
application code.

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -202,6 +202,7 @@
//!
//! ```
//! # use thiserror::Error;
//! #
//! // PublicError is public, but opaque and easy to keep compatible.
//! #[derive(Error, Debug)]
//! #[error(transparent)]
Expand Down

0 comments on commit f062061

Please sign in to comment.