Crates.io | proc-state |
lib.rs | proc-state |
version | 0.1.1 |
source | src |
created_at | 2024-10-06 07:50:25.181212 |
updated_at | 2024-10-16 00:47:37.456663 |
description | Sustain global variables between individual proc-macro call |
homepage | |
repository | https://github.com/yasuo-ozu/proc-state |
max_upload_size | |
id | 1398877 |
size | 7,422 |
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.
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.
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.