Skip to content

Commit f062061

Browse files
committed
Copy docs on struct error(transparent) into readme
1 parent 5271eb3 commit f062061

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ pub enum DataStoreError {
165165
}
166166
```
167167

168+
Another use case is hiding implementation details of an error representation
169+
behind an opaque error type, so that the representation is able to evolve
170+
without breaking the crate's public API.
171+
172+
```rust
173+
// PublicError is public, but opaque and easy to keep compatible.
174+
#[derive(Error, Debug)]
175+
#[error(transparent)]
176+
pub struct PublicError(#[from] ErrorRepr);
177+
178+
impl PublicError {
179+
// Accessors for anything we do want to expose publicly.
180+
}
181+
182+
// Private and free to change across minor version of the crate.
183+
#[derive(Error, Debug)]
184+
enum ErrorRepr {
185+
...
186+
}
187+
```
188+
168189
- See also the [`anyhow`] library for a convenient single error type to use in
169190
application code.
170191

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
//!
203203
//! ```
204204
//! # use thiserror::Error;
205+
//! #
205206
//! // PublicError is public, but opaque and easy to keep compatible.
206207
//! #[derive(Error, Debug)]
207208
//! #[error(transparent)]

0 commit comments

Comments
 (0)