Skip to main content

rsplit

Macro rsplit 

Source
macro_rules! rsplit {
    ([$min:literal $max:literal] $val:tt
        $( 
            $minval:literal..=$maxval:literal $varame:ident => $e:block
        )*
    ) => { ... };
}
Expand description

Ranged pattern matching macro: bounds recalculation, strictly inclusive ranges as patterns

Allows to match a Ranged value over a range it covers. The feature has the following limitations:

  • The bounds must be explicitly specified; they are checked, but not inferred
  • The macro syntax supports only the inclusive ranges patterns. Use X..=X for a single value pattern
  • The unclear error reporting
fn to_upper_half(r: Ranged<1,6>) -> Ranged<4,6> {
    // turn 1 to 4, 2 to 5, 3 to 6
 
    rsplit!{[1 6] r  // Bounds and expression (token tree, 
                     // complex expressions must be in parentheses)
        1..=3 low => {low + r!(3)}  // inside the expression, low is set to Ranged<0, 3>
        4..=6 high => {high}  // inside the expression, high is set to Ranged<4, 6>
    }
}