https://docs.rs/match-commutative
When you need to match
on three values that form a commutative math relation, you often need to
duplicate a lot of patterns.
Let's look at an example of what this might look like:
```rust // imagine that these values come from somewhere and we need to match on them let operant1 = Operant::Str(Some("42".into())); let operant2 = Operant::Num(Some(1)); let operator = Operator::Plus;
match (operant1, operator, operant2) {
(
Operant::Str(Some(operantstr)),
Operator::Plus,
Operant::Num(Some(operantnum)),
)
| (
Operant::Num(Some(operantnum)),
Operator::Plus,
Operant::Str(Some(operantstr)),
) if operantstr.len() < 3 => {
let result = operantnum + operantstr.parse::
// Types that we use in this example
enum Operant {
Str(Option
enum Operator {
Plus,
Mult,
Minus,
}
``
For both
Operator::{Plus, Mult}, we have to write _two patterns each_ that are exactly identical
(and connect them with
|(or-pattern)) and execute the same logic.
The only difference in the pattern is the ordering of the
Operant`. Not nice!
match-commutative
insteadWith match-commutative
this can be simplified to:
```rust
use matchcommutative::matchcommutative;
// imagine that these values come from somewhere and we need to match on them
let operant1 = Operant::Str(Some("42".into()));
let operant2 = Operant::Num(Some(1));
let operator = Operator::Plus;
matchcommutative!(
operant1,
operator,
operant2,
Operant::Str(Some(operantstr)),
Operator::Plus,
Operant::Num(Some(operantnum)) if operantstr.len() < 3 => {
let result = operantnum + operantstr.parse::non_commut
block, patterns and their execution block behave exactly like std Rust
panic!("Not relevant for this example")
}
}
);
// Types that we use in this example
enum Operant {
Str(Option
enum Operator { Plus, Mult, Minus, } ```
Note that in the above example the values of operant1
and operant2
could have been swapped, while still
leading to the same program output.
So we have successfully avoided the expression of ordering in our patterns
(where ordering is not needed between two Operant
s).✨
non_commut
block for operants that are not commutativeIf you need to match
on operants that are not commutative, you can put the pattern
in the optional non_commut
block. Within non_commut
patterns behave exactly like std Rust:
```rust
use matchcommutative::matchcommutative;
let operant1 = Operant::Str(Some("42".into()));
let operant2 = Operant::Num(Some(1));
let operator = Operator::Minus; // a non-commutative operator!
let result = matchcommutative!(
operant2,
operator,
operant1,
Operant::Str(),
Operator::Plus,
Operant::Num() => {
// do something here
todo!()
}
noncommut {
// for minus operations, we get different results depending on the
// ordering of the operants
Operant::Num(Some(opnum)),
Operator::Minus,
Operant::Str(Some(opstr)) if opstr.len() < 3 => {
opnum - opstr.parse::
assert_eq!(-41, result);
// Types that we use in this example
enum Operant {
Str(Option
enum Operator { Plus, Mult, Minus, } ```
In your Cargo.toml file add the following lines under [dependencies]
:
toml
match-commutative = "0.1.0"
This crate is implemented in 100% Safe Rust, which is ensured by using #![forbid(unsafe_code)]
.
The Minimum Supported Rust Version for this crate is 1.54. An increase of MSRV will be indicated by a minor change (according to SemVer).
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.