async-trait

Type erasure for async fn in Rust traits, enabling async methods behind dyn dispatch.

Library
Cargo
v0.1.92
2,208 stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
69 /100 Good
Development Activity 56
Maintenance 72
Community 48
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
91 /100 Excellent
Architecture 90
Code Quality 92
Innovation 93
Learning Curve 88

async-trait is a Rust procedural-macro crate that makes it possible to write async fn inside traits and use those traits as dyn Trait objects. Although Rust 1.75 stabilized async functions in traits, it still does not allow such traits to be object-safe, which breaks common patterns like storing heterogeneous handlers in a Vec<Box<dyn Trait>>.

The crate solves this with a single #[async_trait] attribute macro applied to a trait and its impls. Under the hood it rewrites each async method into one that returns a boxed, type-erased Pin<Box<dyn Future + Send>>, so the trait becomes dyn-compatible again with no unsafe code in the generated output.

What You Get

  • A single #[async_trait] attribute macro that works on both trait definitions and impl blocks.
  • Automatic transformation of async methods into Pin<Box<dyn Future + Send + 'async_trait>> returns, making the trait usable as dyn Trait.
  • A #[async_trait(?Send)] variant for futures that do not need to be Send.
  • Full support for self by value/ref/mut-ref, generics, lifetimes, associated types, and default method implementations, with no unsafe in generated code.

Common Use Cases

  • Defining trait-based plugin or handler interfaces where implementations are stored behind Box<dyn Trait> or &dyn Trait.
  • Building async service abstractions (repositories, clients, middleware) that must be object-safe for dynamic dispatch.
  • Maintaining libraries that need to support async trait methods across older Rust toolchains and dyn use cases the native feature does not yet cover.

Under The Hood

Architecture

The crate is a proc-macro ([lib] proc-macro = true) whose entry point in src/lib.rs is the #[proc_macro_attribute] async_trait function. It parses the annotated item into an Item::Trait or Item::Impl (src/parse.rs) and dispatches to expand() in src/expand.rs, the ~500-line core that rewrites each async method. The transform relies on syn’s VisitMut visitor pattern across focused modules: src/receiver.rs detects and rewrites self usage (ReplaceSelf, has_self_in_sig), src/lifetime.rs collects and injects the synthetic 'async_trait lifetime, and src/bound.rs infers Send/Sync supertrait bounds. Each async fn body is wrapped in Box::pin(async move { ... }) returning Pin<Box<dyn Future + Send + 'async_trait>>.

Tech Stack

Pure Rust (edition 2021, MSRV 1.71) with the standard proc-macro toolchain: proc-macro2, quote for token generation, and syn v3 with the full, visit-mut, parsing, and printing features for AST manipulation. A minimal build.rs only declares a cfg for nightly test gating. Dev-dependencies include futures, tracing/tracing-attributes, rustversion, and trybuild for compile-fail testing.

Code Quality

The code is idiomatic and cleanly separated by concern, with each transformation step in its own module. Testing is thorough: tests/test.rs contains 205 test functions exercising receivers, generics, lifetimes, and associated types, backed by a custom executor harness and an extensive tests/ui suite of trybuild compile-fail cases with expected .stderr output. The generated expansion contains no unsafe, an explicit safety guarantee stated in the README.

API Design The public surface is a single attribute macro, making adoption nearly frictionless: annotate the trait and its impls, keep writing ordinary async fn, and the trait becomes dyn-compatible. The #[async_trait(?Send)] toggle cleanly covers the non-thread-safe case. Documentation is excellent, with a worked example, an explanation of the desugaring, and guidance on lifetime-elision pitfalls in both the README and the rustdoc on docs.rs.

Used by 43 apps in this directory

Rust
72%
Other

iii

Developer Tools · Devops

18,854

Compose, extend, and observe every backend service in real time using three primitives: Workers, Functions, and Triggers.

View details
87
Repo Health
85
Technical
65
Dependency
Built with
Rust 72%
TypeScript 16%
Updated 6 days ago
Rust
96%
Apache 2.0

InfluxDB

Analytics · Databases

31,779

Open-source time-series database built for real-time ingest, fast SQL queries, and embedded Python automation — powered by Apache Arrow and Parquet.

View details
80
Repo Health
76
Technical
72
Dependency
Built with
Rust 96%
Updated 6 days ago
Rust
56%
Other

Jan

AI Assistants

45,052

Run LLMs 100% locally with full privacy, or connect to cloud AI — your machine, your data, your control.

View details
90
Repo Health
81
Technical
65
Dependency
Built with
Rust 56%
TypeScript 40%
Updated 5 days ago
TypeScript
44%
MIT

Kuku

Note Taking

230

A local-first, open-source Markdown knowledge workspace for macOS — plain files, personal wiki and Second Brain workflows, AI-assisted diffs, and encrypted sync, built as an Obsidian alternative.

View details
77
Repo Health
67
Technical
67
Dependency
Built with
TypeScript 44%
Rust 41%
Updated 1 weeks ago
TypeScript
63%
Apache 2.0

Laminar

AI Development · Monitoring

3,434

Open-source observability platform purpose-built for AI agents — trace, evaluate, debug, and monitor at scale with SQL access and real-time replay.

View details
86
Repo Health
76
Technical
65
Dependency
Built with
TypeScript 63%
Rust 35%
Updated 5 days ago
Rust
43%
Apache 2.0

LanceDB

AI Development · Databases

11,757

Open-source, embedded vector database built on the Lance columnar format for fast multimodal search across billions of vectors, backed by Y Combinator (W23).

View details
90
Repo Health
86
Technical
72
Dependency
Built with
Rust 43%
Python 27%
HTML 21%
Updated 5 days ago
Rust
78%
AGPL 3.0

Lemmy

Community · Social Media

14,644

Federated, self-hosted Reddit alternative with full community ownership and no corporate control.

View details
91
Repo Health
74
Technical
62
Dependency
Built with
Rust 78%
PLpgSQL 14%
Updated 6 days ago
JavaScript
55%
Other

Lokus

Knowledge Management · Note Taking

829

Local-first note-taking with graph view, canvas & AI plugins—your Markdown files, zero telemetry, blazing-fast Rust performance.

View details
75
Repo Health
75
Technical
64
Dependency
Built with
JavaScript 55%
HTML 21%
Rust 12%
Updated 2 months ago
TypeScript
92%
Apache 2.0

Apache Maka

AI Agents · AI Development · Developer Tools

5,868

An Apache-incubated agent workspace that treats every model call, tool run, and permission decision as an append-only, replayable event log instead of disposable chat history.

View details
82
Repo Health
85
Technical
73
Dependency
Built with
TypeScript 92%
Updated 5 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers