cfg_mixin

Crates.iocfg_mixin
lib.rscfg_mixin
version0.1.0
created_at2026-01-19 17:35:54.44081+00
updated_at2026-01-19 17:35:54.44081+00
descriptionAttribute macro that produces on/off copies of items and expands #[on(...)] / #[off(...)] attributes.
homepage
repositoryhttps://github.com/valstad-shipworks/snare
max_upload_size
id2055010
size15,257
(oh-yes-0-fps)

documentation

README

cfg_mixin

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)].

Usage

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)].

Scope and limitations

  • Supported items: 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.
Commit count: 3

cargo fmt