proc-state

Crates.ioproc-state
lib.rsproc-state
version0.1.1
sourcesrc
created_at2024-10-06 07:50:25.181212
updated_at2024-10-16 00:47:37.456663
descriptionSustain global variables between individual proc-macro call
homepage
repositoryhttps://github.com/yasuo-ozu/proc-state
max_upload_size
id1398877
size7,422
yozu (yasuo-ozu)

documentation

README

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.

Commit count: 4

cargo fmt