Rust's Orphan Rule prevents implementing external traits on external types, which creates friction when bridging unrelated crates. The adapter pattern — wrapping an external type in a newtype struct — solves this by giving you ownership of the type in the impl block. The post walks through the pattern step by step: starting with a basic owned adapter, then a reference-based adapter with lifetimes, and finally using extension traits to make the API ergonomic. A practical axum + eyre integration example shows how to map eyre::Report errors to HTTP responses (500, 404, 403) using multiple specialized adapters and ResultExt/ErrExt extension traits. The post also covers Deref/AsRef tradeoffs on adapter types, field visibility conventions, and when crate-specific alternatives like serde's remote syntax may be preferable.
Table of contents
Orphan RuleThe problemAdapter patternReference as underlying typeExtension trait for creationDeref and AsRef on the adapter typeSolving Axum problem with adapterMultiple adapters on trait-type pairAdapter alternatives - trait crate utilitiesPublic or private?SummarySort: