proc-macro2

A drop-in substitute for Rust's compiler proc_macro API that works anywhere and makes procedural macros unit-testable.

Library
Cargo
v1.0.107
931stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
68/100Good
Development Activity52
Maintenance64
Community68
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
90/100Excellent
Architecture90
Code Quality90
Innovation92
Learning Curve78

proc-macro2 is a wrapper around the Rust compiler’s built-in proc_macro crate. Where proc_macro types can only ever exist inside a running procedural macro, proc-macro2 mirrors the same TokenStream, TokenTree, Ident, Literal, Punct, Group, and Span API in types that work in any context: build scripts, regular binaries, ordinary libraries, and unit tests.

It is the foundation the modern Rust macro ecosystem is built on. Libraries like syn and quote target proc-macro2 rather than proc_macro so that token-based tooling can be developed, reused, and tested outside the narrow confines of an actual macro invocation. Inside a real proc macro it transparently delegates to the compiler; everywhere else it falls back to a self-contained pure-Rust implementation.

What You Get

  • A complete mirror of the compiler’s proc_macro types — TokenStream, TokenTree, Group, Ident, Punct, Literal, and Span — usable outside a proc macro.
  • Zero-cost delegation to the real compiler API when running inside a procedural macro, and a pure-Rust fallback lexer/parser everywhere else.
  • The ability to unit-test macro helper logic in isolation, since proc-macro2 values can be constructed and inspected in ordinary #[test] functions.
  • An optional span-locations feature exposing line/column source positions for better diagnostics.
  • A no_std-compatible core with a single lightweight dependency (unicode-ident).

Common Use Cases

  • Writing custom derive, attribute, and function-like procedural macros, typically alongside syn and quote.
  • Building reusable token-processing libraries that need to run in build.rs or plain application code, not just inside a macro.
  • Unit-testing the internal logic of a procedural macro without a full compiler macro-expansion harness.
  • Generating Rust source code programmatically from build scripts and codegen tools.

Under The Hood

Architecture

proc-macro2 is structured as a thin public facade over two interchangeable backends selected at runtime. src/lib.rs (~51KB) defines the public TokenStream, TokenTree, Group, Ident, Punct, Literal, and Span types. src/detection.rs decides, once and cached, whether the real compiler proc_macro API is available; src/wrapper.rs (~31KB) bridges to it when so, while src/fallback.rs (~35KB) provides a complete standalone implementation for build.rs, binaries, and tests. The fallback path parses source text via a hand-written lexer in src/parse.rs (~29KB), with src/rustc_literal_escaper.rs handling literal escape semantics and src/rcvec.rs providing a reference-counted vector for cheap token-stream cloning. A build.rs probe compiles feature-detection snippets to gate unstable compiler APIs.

Tech Stack

Pure Rust, edition 2021, with a minimum supported Rust version of 1.71. The only runtime dependency is unicode-ident for identifier validation; the crate is #![no_std] at its core. Cargo features gate behavior: proc-macro (default) enables the compiler bridge, span-locations exposes line/column data, and a procmacro2_semver_exempt cfg opts into nightly-tracking APIs. Dev-dependencies (quote, flate2, tar, rayon, rustversion) drive the test and benchmark suites.

Code Quality

Maturity shows throughout: deny(unsafe_op_in_unsafe_fn), an extensive curated clippy allow-list, and a broad tests/ suite (test.rs, comments.rs, features.rs, marker.rs, test_fmt.rs, test_size.rs) plus a tests/ui trybuild directory, a fuzz/ target, and benches/. The dual-backend design is carefully partitioned so the two implementations stay behaviorally identical, and the code is authored and maintained primarily by David Tolnay with 1,288 commits over eight years.

API Design

The defining design choice is deliberate one-to-one fidelity with the standard library’s proc_macro API, so migrating code between the two is largely a matter of swapping the import path. Types implement the expected Display, FromStr, Clone, and iterator conversions, making token streams ergonomic to build and inspect. Documentation is thorough via docs.rs with runnable examples, and the two-line Cargo.toml setup gets users started immediately.

Used by 25 apps in this directory

Rust
64%
Apache 2.0

agentgateway

AI Development · Developer Tools

4,420

An open source AI-native proxy that secures, observes, and governs agent-to-LLM, agent-to-tool, and agent-to-agent communication through MCP, A2A, and unified LLM routing.

View details
88
Repo Health
82
Technical
72
Dependency
Built with
Rust64%
Go24%
Updated yesterday
Dart
74%
AGPL 3.0

AppFlowy

Productivity · Project Management · Collaboration

75,729

The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.

View details
66
Repo Health
81
Technical
66
Dependency
Built with
Dart74%
Rust24%
Updated 1 weeks ago
Rust
86%
Other

Arroyo

Data Engineering · Analytics

5,005

A distributed stream processing engine written in Rust that lets you write SQL to run stateful, real-time computations over data streams with subsecond results.

View details
86
Repo Health
74
Technical
62
Dependency
Built with
Rust86%
Updated 1 weeks ago
Rust
67%
MIT

Bun

Developer Tools

95,452

An all-in-one JavaScript and TypeScript toolkit — one Rust-and-JavaScriptCore binary that replaces Node.js, npm, a bundler, and a test runner with faster equivalents.

View details
92
Repo Health
91
Technical
66
Dependency
Built with
Rust67%
C++19%
Updated today
TypeScript
55%
Other

Cap

Team Chat · Video Conferencing

20,986

Open source Loom alternative with GPU-accelerated recording, instant share links, AI summaries, and full self-hosting via Docker Compose.

View details
89
Repo Health
81
Technical
65
Dependency
Built with
TypeScript55%
Rust40%
Updated yesterday
Rust
52%
Apache 2.0

cocoindex

Data Engineering · AI Development

11,350

An incremental data indexing engine that keeps AI agent context perpetually fresh by reprocessing only what changed.

View details
87
Repo Health
85
Technical
64
Dependency
Built with
Rust52%
Python48%
Updated yesterday
Rust
95%
Other

Databend

Databases · Data Engineering

9,414

Open-source enterprise data warehouse unifying analytics, vector search, full-text search, and AI agent orchestration in a single Rust-built engine on S3.

View details
91
Repo Health
84
Technical
79
Dependency
Built with
Rust95%
Updated today
Rust
87%
MIT

fabro

Developer Tools · Devops

1,516

Define AI agent workflows as code graphs, route tasks across any LLM, and intervene only where it matters.

View details
81
Repo Health
83
Technical
70
Dependency
Built with
Rust87%
TypeScript11%
Updated yesterday
Rust
95%
MIT

Fyrox

Game Development

9,512

A production-ready 2D/3D game engine written in Rust with a built-in scene editor, physics, and hot-reloading game scripts

View details
85
Repo Health
79
Technical
68
Dependency
Built with
Rust95%
Updated yesterday

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

Search