rust-trait-api-design
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.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- rust-trait-api-design
- license
- MIT
- metadata
- { "tags": "rust,traits,generics,api-design,trait-objects", "version": "0.1.0", "category": "Rust", "displayName": "Rust Trait API Design" }
- description
- 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.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 2
- 参考文档agents/openai.yaml244 B
- 参考文档references/trait-api-patterns.md2.0 KB
03 SKILL.md 正文
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 Traitfor heterogeneous values, plugin-like extension points, or runtime dispatch. - Place bounds at the point that needs them. Prefer
whereclauses 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, andCow. - 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 Traitor a named generic for parameters when callers should pass many concrete types. - Return
impl Traitwhen hiding one concrete return type. ReturnBox<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
Fromfor infallible conversions andTryFromfor fallible conversions. Implementing these gives callersIntoandTryInto. - Use
AsReffor cheap reference-to-reference conversion. UseBorrowonly when borrowed and owned forms have equivalentEq,Hash, andOrdbehavior. - Avoid
Copybounds 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
Selfunless constrained withwhere 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>, orArc<dyn Trait + Send + Sync>.
Common Smells
- A public function takes
&Vec<T>because it only needs iteration. - The API accepts
Stringand immediately borrows it as&str. - The API implements
Intodirectly instead ofFrom. - 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-traitis used in a public trait without checking whether nativeasync fnin traits,trait-variant, or boxed futures fit the dispatch andSendneeds better.
04 同仓库更多 Skills
worktable-review-with-annotations
Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.
worktable-find-and-synthesize-context
Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.
worktable-create-or-update-docs
Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.
worktable-create-or-manage-records
Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.
worktable-create-or-manage-interactive-html
Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.
worktable-collaborate-threads
Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.