AGENT SKILL

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-06-26
Stars
★ 0
Forks
0

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

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

  1. Identify whether the API consumes, borrows, returns, stores, or dispatches behavior.
  2. Start with concrete types for local code. Generalize only when at least two callers or implementations need the flexibility.
  3. Use generic bounds for compile-time polymorphism and inlining. Use dyn Trait for heterogeneous values, plugin-like extension points, or runtime dispatch.
  4. Place bounds at the point that needs them. Prefer where clauses when bounds are long or involve associated types.
  5. Decide whether a trait is intended for downstream implementation. Seal it, keep fields private, or use constructors when invariants must not be implemented externally.
  6. 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.
  7. 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.

04 同仓库更多 Skills

ConcertoNotes
ConcertoNotes/codex-plugins

android-emulator-qa

Use when validating Android feature flows in an emulator with adb-driven launch, input, UI-tree inspection, screenshots, and logcat capture.

★ 0
ConcertoNotes
ConcertoNotes/codex-plugins

supabase-postgres-best-practices

Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.

★ 0
ConcertoNotes
ConcertoNotes/codex-plugins

sentry

Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.

★ 0
ConcertoNotes
ConcertoNotes/codex-plugins

render-workflows

Sets up, develops, tests, and deploys Render Workflows. Covers first-time scaffolding (via CLI or manual), SDK installation (Python or TypeScript), task patterns (retries, subtasks, fan-out), local development, Dashboard deployment, and troubleshooting. Use when a user wants to set up Render Workflows for the first time, scaffold a workflow service, add or modify workflow tasks, test workflows locally, or deploy workflows to Render.

★ 0
ConcertoNotes
ConcertoNotes/codex-plugins

render-web-services

Configures Render web services—port binding, TLS, health checks, custom domains, auto-deploy, PR previews, persistent disks, and deploy lifecycle. Use when the user needs to set up a web service, fix health check failures, add a custom domain, configure zero-downtime deploys, or troubleshoot port binding issues.

★ 0
ConcertoNotes
ConcertoNotes/codex-plugins

render-static-sites

Deploys and configures static sites on Render's global CDN—build commands, publish paths, SPA routing, redirects, custom headers, and PR previews. Use when the user needs to deploy a static site, set up a React/Vue/Hugo/Gatsby frontend, configure SPA fallback routing, add redirect rules, customize response headers, or choose between a static site and a web service for their frontend. Trigger terms: static site, CDN, SPA, single-page app, React deploy, Vue deploy, Hugo, Gatsby, Docusaurus, Jekyll, staticPublishPath.

★ 0