Skip to main content

Crate make_noop

Crate make_noop 

Source
Expand description

Procedural macros that turn functions, methods, or every method in an impl block into no-ops, and that strip a struct down to a unit struct.

The intended use is to toggle the no-op’ing on a compile-time condition: pair any of these macros with cfg_attr so a feature flag (or any cfg) decides whether the real implementation is compiled or quietly replaced with a stub — without #[cfg] blocks duplicating each item or a runtime branch.

use make_noop::{make_noop, make_unit, noop_returns};

// No-op under the `dry-run` feature; the real body otherwise.
#[cfg_attr(feature = "dry-run", make_noop)]
fn launch_missiles(hardware: &Hardware) {
    hardware.arm();
    hardware.fire();
}

// Report success without touching the network under `dry-run`.
#[cfg_attr(feature = "dry-run", noop_returns(Ok(())))]
fn upload(client: &Client, blob: &[u8]) -> Result<(), Error> {
    client.put(blob)
}

// Collapse to `struct Telemetry;` under `dry-run`.
#[cfg_attr(feature = "dry-run", make_unit)]
struct Telemetry {
    events: Vec<Event>,
}

Each function’s body is replaced with one that does nothing; functions returning a value return Default::default() unless tagged with noop_returns. The macros also apply unconditionally (without cfg_attr) for stubbing in tests and prototypes.

Attribute Macros§

make_noop
Replaces the body of the tagged function(s) with a no-op.
make_unit
Strips a struct’s fields, turning it into a unit struct.
noop_returns
Makes the tagged function/method a no-op that returns the given expression.