| Crates.io | pub_if |
| lib.rs | pub_if |
| version | 0.1.0 |
| created_at | 2025-12-19 17:29:27.379847+00 |
| updated_at | 2025-12-19 17:29:27.379847+00 |
| description | A Rust procedural macro that conditionally makes struct fields public based on cfg attributes. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1995161 |
| size | 20,928 |
A Rust procedural macro that conditionally makes struct fields public based on cfg attributes.
syn-free implementation using only proc_macro APIs.
Add the #[pub_if(...)] attribute to a struct to generate two versions:
#[cfg(...)] where all fields are public#[cfg(not(...))] where fields retain their original visibilityuse pub_if::pub_if;
#[pub_if(feature = "foo")]
pub struct Struct<F, B> {
field: F,
bar: B,
}
This expands to:
#[cfg(feature = "foo")]
pub struct Struct<F, B> {
pub field: F,
pub bar: B,
}
#[cfg(not(feature = "foo"))]
pub struct Struct<F, B> {
field: F,
bar: B,
}
pub remain public in both versionssyn crate using only proc_macro APIs#[pub_if(feature = "expose_internals")]
pub struct Config {
private_setting: i32,
pub public_setting: String,
}
When the feature is enabled, both fields are public.
When disabled, only public_setting remains public.
The project includes compile-time tests using trybuild to verify that:
pub stay public in both cases# Run tests without feature (verifies private fields are not accessible)
cargo test
# Run tests with feature (verifies all fields become public)
cargo test --features foo