From 6f6c1b3cf0eb54923fa22328e79f03e42b1839a2 Mon Sep 17 00:00:00 2001 From: Varun Chawla Date: Fri, 13 Feb 2026 00:50:10 -0800 Subject: [PATCH] Fix documentation example for transparent error pattern The example showing how to use #[error(transparent)] with #[from] on a struct wrapping an enum had a placeholder (...) that didn't compile when users tried to complete it. Added a concrete example showing: - A variant in ErrorRepr with #[from] attribute - Helper code demonstrating the From trait chain works correctly - A working example function that uses the ? operator The key insight is that functions need to return Result<(), ErrorRepr> for the From chain to work: InvalidInputError -> ErrorRepr -> PublicError Fixes #439 --- README.md | 4 +++- src/lib.rs | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7c67cb2..60fbef0 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,9 @@ pub enum DataStoreError { // Private and free to change across minor version of the crate. #[derive(Error, Debug)] enum ErrorRepr { - ... + #[error("invalid input")] + InvalidInput(#[from] InvalidInputError), + // Additional error variants can be added here } ``` diff --git a/src/lib.rs b/src/lib.rs index c26aa0c..fae9b80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -245,10 +245,22 @@ //! // Private and free to change across minor version of the crate. //! #[derive(Error, Debug)] //! enum ErrorRepr { -//! # /* -//! ... -//! # */ +//! #[error("invalid input")] +//! InvalidInput(#[from] InvalidInputError), //! } +//! # +//! # #[derive(Error, Debug)] +//! # #[error("invalid input")] +//! # struct InvalidInputError; +//! # +//! # fn process() -> Result<(), ErrorRepr> { +//! # Err(InvalidInputError.into()) +//! # } +//! # +//! # fn example() -> Result<(), PublicError> { +//! # process()?; +//! # Ok(()) +//! # } //! ``` //! //! - See also the [`anyhow`] library for a convenient single error type to use