#proc-macro #macro #syn

proc-state

Sustain global variables between individual proc-macro call

2 releases

new 0.1.1 Oct 16, 2024
0.1.0 Oct 6, 2024

#866 in Procedural macros

Download history 108/week @ 2024-09-30 67/week @ 2024-10-07 186/week @ 2024-10-14

361 downloads per month

MIT license

8KB
90 lines

proc-state crate Latest Version Documentation GitHub Actions

Overview

This Rust library solves a fundamental limitation in Rust's procedural macros where state cannot be shared across macro invocations. Each time a macro is called, it runs in a new, isolated environment, making it impossible to maintain state between calls.

To overcome this, the library introduces the Global<T> type, where T must implement Send and Sync. Additionally, it provides the [new!()] macro, which can be invoked in a const context, enabling global state to persist across multiple macro invocations.

By using this library, you can easily manage persistent global state across procedural macro invocations in a safe and efficient manner.

Limitation

In some cases, rustc may invocates each proc-macro calls in different rustc processes. In that case, Global<T> is initialized as empty in each processes independently and thus we cannot any state beyond the process boundary. It is not a problem when you use this crate to cache someting.

Caution

This is an experimental and tricky library, which depends deeply on detailed rustc implementations. It may be broken at any time. This macro is intended to be deal with your proc-macro code, which means it only generate codes and it does not affect of other ways. So the interface is defined as safe for now. Use it on your own responsibility.

Dependencies

~225–650KB
~16K SLoC