Rust Trait API Design
Use this skill to make Rust APIs generic where useful, concrete where simpler,
and object-safe when dynamic dispatch is part of the design. Optimize for the
smallest capability contract that callers and implementors can understand.
Core Workflow
- Identify whether the API consumes, borrows, returns, stores, or dispatches
behavior.
- Start with concrete types for local code. Generalize only when at least two
callers or implementations need the flexibility.
- Use generic bounds for compile-time polymorphism and inlining. Use
dyn Trait for heterogeneous values, plugin-like extension points, or runtime
dispatch.
- Place bounds at the point that needs them. Prefer
where clauses when
bounds are long or involve associated types.
- Decide whether a trait is intended for downstream implementation. Seal it,
keep fields private, or use constructors when invariants must not be
implemented externally.
- Use standard conversion traits where they match exactly. Do not invent a
custom conversion trait before checking
From, TryFrom, AsRef, AsMut,
Borrow, ToOwned, and Cow.
- Add compile tests, unit tests, or examples that prove the public API is
callable the way the skill expects future users to call it.
API Design Rules
- Accept
impl Trait or a named generic for parameters when callers should
pass many concrete types.
- Return
impl Trait when hiding one concrete return type. Return Box<dyn Trait> when the concrete type varies at runtime.
- Prefer associated types when each implementation has one natural related
type. Prefer generic trait parameters when one implementation supports many
target types.
- Implement
From for infallible conversions and TryFrom for fallible
conversions. Implementing these gives callers Into and TryInto.
- Use
AsRef for cheap reference-to-reference conversion. Use Borrow only
when borrowed and owned forms have equivalent Eq, Hash, and Ord
behavior.
- Avoid
Copy bounds unless the algorithm semantically requires bitwise copy.
Trait Object Review
Read references/trait-api-patterns.md when choosing between generics and
dyn Trait, or when public traits fail object-safety checks.
Before making a public trait object-safe, check:
- Methods do not use generic type parameters.
- Methods do not return
Self unless constrained with where Self: Sized.
- Associated types are specified on the trait object where needed.
- The object is behind a pointer such as
&dyn Trait, Box<dyn Trait>, or
Arc<dyn Trait + Send + Sync>.
Common Smells
- A public function takes
&Vec<T> because it only needs iteration.
- The API accepts
String and immediately borrows it as &str.
- The API implements
Into directly instead of From.
- A trait has many blanket bounds that only one method needs.
- A trait object was used because lifetimes were confusing, not because runtime
dispatch is required.
async-trait is used in a public trait without checking whether native
async fn in traits, trait-variant, or boxed futures fit the dispatch and
Send needs better.
1---2name: rust-trait-api-design3description: Design and review Rust trait APIs, generic bounds, dispatch models, and conversion contracts for clear public interfaces. Use when designing, refactoring, or reviewing traits, generics, trait objects, associated types, impl Trait, From/TryFrom, AsRef, Borrow, or crate APIs.4license: MIT5compatibility: Codex, Claude Code, and other Agent Skills-compatible clients.6metadata:7 version: "0.1.0"8 displayName: Rust Trait API Design9 category: Rust10 tags: rust,traits,generics,api-design,trait-objects11---1213# Rust Trait API Design1415Use this skill to make Rust APIs generic where useful, concrete where simpler,16and object-safe when dynamic dispatch is part of the design. Optimize for the17smallest capability contract that callers and implementors can understand.1819## Core Workflow20211. Identify whether the API consumes, borrows, returns, stores, or dispatches22 behavior.232. Start with concrete types for local code. Generalize only when at least two24 callers or implementations need the flexibility.253. Use generic bounds for compile-time polymorphism and inlining. Use `dyn26 Trait` for heterogeneous values, plugin-like extension points, or runtime27 dispatch.284. Place bounds at the point that needs them. Prefer `where` clauses when29 bounds are long or involve associated types.305. Decide whether a trait is intended for downstream implementation. Seal it,31 keep fields private, or use constructors when invariants must not be32 implemented externally.336. Use standard conversion traits where they match exactly. Do not invent a34 custom conversion trait before checking `From`, `TryFrom`, `AsRef`, `AsMut`,35 `Borrow`, `ToOwned`, and `Cow`.367. Add compile tests, unit tests, or examples that prove the public API is37 callable the way the skill expects future users to call it.3839## API Design Rules4041- Accept `impl Trait` or a named generic for parameters when callers should42 pass many concrete types.43- Return `impl Trait` when hiding one concrete return type. Return `Box<dyn44 Trait>` when the concrete type varies at runtime.45- Prefer associated types when each implementation has one natural related46 type. Prefer generic trait parameters when one implementation supports many47 target types.48- Implement `From` for infallible conversions and `TryFrom` for fallible49 conversions. Implementing these gives callers `Into` and `TryInto`.50- Use `AsRef` for cheap reference-to-reference conversion. Use `Borrow` only51 when borrowed and owned forms have equivalent `Eq`, `Hash`, and `Ord`52 behavior.53- Avoid `Copy` bounds unless the algorithm semantically requires bitwise copy.5455## Trait Object Review5657Read `references/trait-api-patterns.md` when choosing between generics and58`dyn Trait`, or when public traits fail object-safety checks.5960Before making a public trait object-safe, check:6162- Methods do not use generic type parameters.63- Methods do not return `Self` unless constrained with `where Self: Sized`.64- Associated types are specified on the trait object where needed.65- The object is behind a pointer such as `&dyn Trait`, `Box<dyn Trait>`, or66 `Arc<dyn Trait + Send + Sync>`.6768## Common Smells6970- A public function takes `&Vec<T>` because it only needs iteration.71- The API accepts `String` and immediately borrows it as `&str`.72- The API implements `Into` directly instead of `From`.73- A trait has many blanket bounds that only one method needs.74- A trait object was used because lifetimes were confusing, not because runtime75 dispatch is required.76- `async-trait` is used in a public trait without checking whether native77 `async fn` in traits, `trait-variant`, or boxed futures fit the dispatch and78 `Send` needs better.