From c78dc6abb11020237aee42ee21d582ef3aa00635 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 14 Mar 2021 19:48:11 +0000 Subject: [PATCH] utils: Improve matches_doesnot Signed-off-by: Ian Jackson --- src/utils.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 4a4a29b6..de6dc560 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -347,10 +347,25 @@ impl IteratorExt for T where } } +#[macro_export] // <- otherwise bogus warning `unused_macros` +macro_rules! matches_doesnot_yn2bool { + (=) => (true); + (!) => (false); +} + #[macro_export] macro_rules! matches_doesnot { - ($v:expr, = $($y:pat)|*, ! $($n:pat)|* $(,)?) => - { match $v { $($y)|* => true, $($n)|* => false } } + ($v:expr, + $( + $yn:tt $($p:pat)|* + ),* + ) => { + match $v { + $( + $($p)|* => matches_doesnot_yn2bool!($yn), + )* + } + } } #[test] @@ -369,6 +384,13 @@ fn matches_doesnot_test() { ! Some(_) | None ) ); + assert!( + ! matches_doesnot!( + Some(1), + ! Some(1) | Some(2), + = Some(_) | None + ) + ); } pub fn dbgc_helper(file: &'static str, line: u32, -- 2.30.2