| Crates.io | cfg_mixin |
| lib.rs | cfg_mixin |
| version | 0.1.0 |
| created_at | 2026-01-19 17:35:54.44081+00 |
| updated_at | 2026-01-19 17:35:54.44081+00 |
| description | Attribute macro that produces on/off copies of items and expands #[on(...)] / #[off(...)] attributes. |
| homepage | |
| repository | https://github.com/valstad-shipworks/snare |
| max_upload_size | |
| id | 2055010 |
| size | 15,257 |
cfg_mixin is a procedural attribute macro that emits two cfg-gated copies of a struct or impl item. It expands #[on(...)] and #[off(...)] attribute lists into normal Rust attributes for the corresponding copy and can optionally gate individual fields or impl items using #[cfg(on)] / #[cfg(off)].
Add the crate to your Cargo.toml:
[dependencies]
cfg_mixin = "0.1"
Apply #[cfg_mixin(...)] to a struct or impl item. The macro requires a valid cfg predicate and emits an #[cfg(predicate)] copy (the "on" version) and an #[cfg(not(predicate))] copy (the "off" version).
use cfg_mixin::cfg_mixin;
#[cfg_mixin(feature = "py")]
pub struct Person {
#[on(pyo3(get, set))]
id: String,
#[off(serde(serialize_with = "serialize_name"))]
name: String,
#[cfg(on)]
py_only: i32,
#[cfg(off)]
rust_only: i32,
}
In the example above, the pyo3 attribute is emitted only for the feature = "py" copy, and the serde attribute is emitted only for the non-py copy. The py_only and rust_only fields are gated entirely via #[cfg(on)] and #[cfg(off)].
struct and impl only. Other item kinds will produce a compile error.#[on(...)] and #[off(...)] accept comma-separated Meta arguments and are expanded into standard attributes.#[cfg(on)] and #[cfg(off)] are interpreted as item-level gates for fields and impl items and are not emitted as attributes.