make-noop
Attribute macros that replace function, method, and impl-block bodies with
no-ops, and strip a struct down to a unit struct.
The point is to toggle that no-op'ing on a compile-time condition. Pair any of
these macros with cfg_attr so that a feature flag (or any cfg) decides
whether the real implementation is compiled or quietly replaced with a stub —
no #[cfg] blocks duplicating each item, and no runtime branch.
use ;
// With the `dry-run` feature on, this becomes a no-op; otherwise it runs for real.
// In `dry-run`, report success without touching the network.
// In `dry-run`, collapse the telemetry buffer to a zero-sized unit struct.
Highlights
#[make_noop]applies to a free function, a single method, or an entireimplblock (every method becomes a no-op).- Value-returning functions return
Default::default()by default; override the returned value with#[noop_returns(EXPR)]. #[noop_returns(EXPR)]works standalone on a function/method, or on a wholeimplblock — where it sets a shared return value for every method, except those carrying their own#[noop_returns(OTHER)].#[make_unit]strips a struct's fields, leaving a unit struct.- Functions with no return value get an empty body and never return a value.
- The original body is discarded — it is not type-checked or executed. Imports or items referenced only from a no-op'd body may therefore become unused.
Installation
Usage
Free functions
use ;
Impl blocks
#[make_noop] on an impl block makes every method a no-op. Individual methods
can opt into a custom return value with #[noop_returns(EXPR)]:
use ;
;
#[noop_returns(EXPR)] can also be applied to the impl block itself to set a
shared default for all methods, still overridable per method:
use noop_returns;
;
Unit structs
#[make_unit] discards a struct's fields, leaving a unit struct. It works on
structs with named fields and on tuple structs; attributes, visibility, and
generics are preserved:
use make_unit;
// Expands to `pub struct Config;` (still `#[derive(Debug, Default)]`)
Notes
- A function that returns a value must have a return type implementing
[
Default] unless you supply#[noop_returns(EXPR)]. - The original body is discarded outright: it is neither type-checked nor executed. Anything referenced only from inside a no-op'd body (for example an import) may consequently be reported as unused.
Minimum supported Rust version
make-noop requires Rust 1.87 or newer. The MSRV is verified in CI and may
be raised in a minor version bump.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.