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
946 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 →
60 /100 Good
Development Activity 32
Maintenance 52
Community 68
Maturity 60
Momentum 28

Technical Analysis

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

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 27 apps in this directory

Rust
95%
MIT

Fyrox

Game Development

9,616

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
67
Dependency
Built with
Rust 95%
Updated 6 days ago
Rust
68%
Other

GitButler

AI Development · Developer Tools · Devops

21,841

Git, but better — a modern version control client with stacked branches, parallel workflows, unlimited undo, and first-class support for AI-powered development.

View details
89
Repo Health
84
Technical
66
Dependency
Built with
Rust 68%
TypeScript 20%
Updated 5 days ago
Rust
89%
Apache 2.0

Graphite

Code Editors · Design Tools

27,703

A free, open source procedural 2D design tool that unifies vector, raster, motion graphics, and generative art in a single node-based nondestructive workflow.

View details
84
Repo Health
77
Technical
70
Dependency
Built with
Rust 89%
Updated 5 days ago
Rust
94%
Apache 2.0

helix-db

Databases

6,233

A graph-vector database built from scratch in Rust that unifies graph traversal, vector search, key-value, and relational storage into a single platform for AI applications.

View details
86
Repo Health
80
Technical
76
Dependency
Built with
Rust 94%
Updated 6 days ago
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
TypeScript
77%
Other

Joplin

Note Taking

56,853

The privacy-first, open-source note-taking app with end-to-end encrypted sync, AI assistance, and a powerful plugin ecosystem across every platform.

View details
93
Repo Health
87
Technical
60
Dependency
Built with
TypeScript 77%
JavaScript 14%
Updated 6 days ago
Rust
100%
Other

Meilisearch

Search

59,644

Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.

View details
90
Repo Health
88
Technical
67
Dependency
Built with
Rust 100%
Updated 6 days ago
Rust
71%
MIT

monty

AI Development · Developer Tools

8,763

Run LLM-generated Python code safely inside your agent—no containers, no CPython, no compromise—with sub-microsecond startup.

View details
88
Repo Health
83
Technical
73
Dependency
Built with
Rust 71%
Python 24%
Updated 6 days ago
Rust
97%
Apache 2.0

codex

AI Code Assistants · Developer Tools

130,715

OpenAI's open-source CLI coding agent that reads, edits, and runs code in your terminal using natural language prompts.

View details
89
Repo Health
80
Technical
75
Dependency
Built with
Rust 97%
Updated 6 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