Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

These Macros Should Be Illegal

The API reference with tested examples is on docs.rs.

This crate is a small collection of experimental Rust macros for deleting boilerplate and trying syntax that ordinary Rust—quite reasonably—does not accept. The useful bits come first; the cursed implementation details are here when you actually need them.

cargo add these-macros-should-be-illegal

Import only the macros used at each call site. For example:

use these_macros_should_be_illegal::{
    callable, enum_fn, make_fn, perm, qf, reflect, stringify_as, strutuct,
};

Pick your crime

  • discriminated_str assigns each variant a unique string and generates a literal-selected delegating constructor.
  • enum_fn generates a method from inline per-variant match-arm expressions.
  • callable and make_fn! give a local object function-like syntax without erasing its type or its ordinary methods.
  • strutuct!, with emmun! as an exact alias, declares a small family of nested algebraic types in one place and generates constructor macros for them.
  • The stringify_* macros turn identifiers, string literals, and Rust types into compile-time string literals.
  • forward_attributes makes outer attributes usable as function-like macro configuration.
  • qf! gives common standard-library types their fully qualified paths inside generated code.
  • shared_match_arms! shares an RHS between patterns whose bindings have different concrete types.
  • literally_literal_string! and expand! let token rewriting handle syntax that Rust cannot parse as an ordinary source file.
  • reflect! and perm! transform macro invocation and token-tree structure without assigning a Rust grammar to the opaque payload.

The macro map groups every public entry point by what it consumes and produces. The individual pages then give the complete accepted syntax, generated shape, and current boundaries.

Stability

This is a personal stable-Rust macro laboratory. The APIs are intentionally small and experimental, and may evolve whenever a better crime presents itself. Releases are available from crates.io, and the ordinary API reference lives on docs.rs.