SoftwareMill
Read post

Adapter Pattern in Rust: Overcoming the Orphan Rule with Newtype and Extension Traits

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.

    #rust
May 06•16m read time•From softwaremill.com
Post cover image
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?Summary
2.3K Impressions
SoftwareMill's image
SoftwareMill

SoftwareMill is a software consultancy specializing in custom software development and distributed s...

41 Followers

•

639 Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard