#failure #pattern #match #coerce-pattern #assert-pattern

macro coerce_pattern

Macros that coerce expressions into patterns, panicking on failure

1 unstable release

Uses new Rust 2024

new 0.1.0 Apr 7, 2025

#627 in Procedural macros

MIT license

13KB
99 lines

This crate contains two proc macros relating to asserting, under penalty of panic, that expressions match patterns.

  1. coerce_pattern! matches an expression to a target pattern and evaluates to an expression of the inner variables from the pattern, panicking if the pattern doesn't match
  2. assert_pattern! matches an expression to a target and panics if the pattern doesn't match.

To use them, run the following command in your project directory.

cargo add coerce_pattern

Then, you can use the macros using

use coerce_pattern::{coerce_pattern, assert_pattern};

See below for detailed documentation on the macros.


coerce_pattern

coerce_pattern is a Rust library crate containing macros that force expressions into patterns. It is version-controlled on GitHub here.

This crate contains two proc macros relating to asserting, under penalty of panic, that expressions match patterns.

  1. coerce_pattern! matches an expression to a target pattern and evaluates to an expression of the inner variables from the pattern, panicking if the pattern doesn't match
  2. assert_pattern! matches an expression to a target and panics if the pattern doesn't match.

To use them, run the following command in your project directory.

cargo add coerce_pattern

Then, you can use the macros using

use coerce_pattern::{coerce_pattern, assert_pattern};

See the documentation in src/lib.rs for more details

Dependencies

~195–620KB
~15K SLoC