typewit/macros/polymatch.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
/// Match which expands top-level `|` patterns to multiple match arms.
///
/// [**examples below**](#examples)
///
/// ### Clarification
///
/// "top-level `|` patterns" means that the `|` is not inside some other pattern.
/// <br>E.g.: the pattern in `Foo(x) | Bar(x) => ` is a top-level `|` pattern.
/// <br>E.g.: the pattern in `(Foo(x) | Bar(x)) => ` is not a top-level `|` pattern,
/// because the `|` is inside parentheses.
///
/// # Syntax
///
/// This uses a `macro_rules!`-like syntax for the parameters of this macro
///
/// ```text
/// $matched_expression:expr;
/// $( $(|)? $($or_pattern:pat_param)|+ $(if $match_guard:expr)? => $arm_expr:expr ),*
/// $(,)?
/// ```
///
/// [**example demonstrating all of this syntax**](#full-syntax)
///
/// # Examples
///
/// ### Basic
///
/// ```rust
/// assert_eq!(debugify(Ok(3)), "3");
/// assert_eq!(debugify(Err("hello")), r#""hello""#);
///
/// fn debugify(res: Result<u32, &'static str>) -> String {
/// typewit::polymatch! {res;
/// Ok(x) | Err(x) => format!("{:?}", x)
/// }
/// }
/// ```
///
/// The above invocation of `polymatch` expands to:
///
/// ```rust
/// # assert_eq!(debugify(Ok(3)), "3");
/// # assert_eq!(debugify(Err("hello")), r#""hello""#);
/// #
/// # fn debugify(res: Result<u32, &'static str>) -> String {
/// match res {
/// Ok(x) => format!("{:?}", x),
/// Err(x) => format!("{:?}", x),
/// }
/// # }
/// ```
///
/// ### Full syntax
///
/// Example that uses all of the syntax supported by this macro.
///
/// ```rust
/// assert_eq!(bar(Foo::Byte(3)), 6);
/// assert_eq!(bar(Foo::Byte(9)), 18);
/// assert_eq!(bar(Foo::Byte(10)), 10);
///
/// assert_eq!(bar(Foo::U16(3)), 6);
/// assert_eq!(bar(Foo::U16(9)), 18);
/// assert_eq!(bar(Foo::U16(10)), 10);
///
/// assert_eq!(bar(Foo::U32(3)), 0);
///
/// assert_eq!(bar(Foo::Long(3)), 0);
///
/// enum Foo {
/// Byte(u8),
/// U16(u16),
/// U32(u32),
/// Long(u64),
/// }
///
/// const fn bar(foo: Foo) -> u64 {
/// typewit::polymatch! {foo;
/// // top-level `|` patterns generate a match arm for every alternate pattern
/// | Foo::Byte(x)
/// | Foo::U16(x)
/// if x < 10
/// => (x as u64) * 2,
///
/// Foo::Byte(x) | Foo::U16(x) => { x as u64 }
///
/// // `|` inside patterns behaves like in regular `match` expressions
/// (Foo::U32(_) | Foo::Long(_)) => 0
/// }
/// }
/// ```
///
/// The above invocation of `polymatch` expands to:
/// ```rust
/// # assert_eq!(bar(Foo::Byte(3)), 6);
/// # assert_eq!(bar(Foo::Byte(9)), 18);
/// # assert_eq!(bar(Foo::Byte(10)), 10);
/// #
/// # assert_eq!(bar(Foo::U16(3)), 6);
/// # assert_eq!(bar(Foo::U16(9)), 18);
/// # assert_eq!(bar(Foo::U16(10)), 10);
/// #
/// # assert_eq!(bar(Foo::U32(3)), 0);
/// #
/// # assert_eq!(bar(Foo::Long(3)), 0);
/// # enum Foo {
/// # Byte(u8),
/// # U16(u16),
/// # U32(u32),
/// # Long(u64),
/// # }
/// #
/// # const fn bar(foo: Foo) -> u64 {
/// match foo {
/// Foo::Byte(x) if x < 10 => (x as u64) * 2,
/// Foo::U16(x) if x < 10 => (x as u64) * 2,
///
/// Foo::Byte(x) => { x as u64 }
/// Foo::U16(x) => { x as u64 }
///
/// (Foo::U32(_) | Foo::Long(_)) => 0
/// }
/// # }
/// ```
///
#[macro_export]
macro_rules! polymatch {
($matching:expr; $($match_arms:tt)*) => {
$crate::__polymatch!{($matching) () $($match_arms)*}
};
($($tt:tt)*) => {
$crate::__::compile_error!{"expected arguments to be `matched expression; match arms`"}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __polymatch {
// Parsing match like syntax
(
($matching:expr)
( $( ($($pattern:tt)*) => ($expr:expr) )* )
// Nothing left to parse
$(,)?
) => {{
#[allow(unused_parens)]
match $matching {
$($($pattern)* => $expr,)*
}
}};
(
$fixed:tt
$prev_branch:tt
$(|)? $($pattern:pat_param)|+ $( if $guard:expr )? => $expr:expr
$(,$($rem:tt)*)?
) => {{
$crate::__polymatch__handle_guard!{
$fixed
$prev_branch
(($($pattern)+) => $expr)
($($guard)?)
($($($rem)*)?)
}
}};
(
$fixed:tt
$prev_branch:tt
$(|)? $($pattern:pat_param)|+ $( if $guard:expr )? => $expr:block
$($rem:tt)*
) => {{
$crate::__polymatch__handle_guard!{
$fixed
$prev_branch
(($($pattern)+) => $expr)
($($guard)?)
($($rem)*)
}
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __polymatch__handle_guard {
(
$fixed:tt
( $($prev_branch:tt)* )
(($($pattern:tt)+) => $expr:tt)
()
($($rem:tt)*)
) => {
$crate::__polymatch!{
$fixed
(
$($prev_branch)*
$(($pattern) => ($expr))*
)
$($rem)*
}
};
(
$fixed:tt
( $($prev_branch:tt)* )
(($($pattern:tt)+) => $expr:tt)
($guard:expr)
($($rem:tt)*)
) => {
$crate::__polymatch!{
$fixed
(
$($prev_branch)*
$(($pattern if $guard) => ($expr))*
)
$($rem)*
}
};
}