From 0b374a77fa703e6eac31c2fa1f74684173376530 Mon Sep 17 00:00:00 2001 From: "Stephen M. Coakley" Date: Thu, 21 Apr 2022 22:36:00 -0500 Subject: [PATCH] Fix MSRV incompatibility --- src/lib.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4549296..eefd7ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -340,6 +340,66 @@ mod tests { macro_rules! test_lifetime_free_cast { () => {}; + ( + for $TARGET:ty as $name:ident { + $( + $value:expr => $matches:pat $(if $guard:expr)?, + )+ + } + $($tail:tt)* + ) => { + paste::paste! { + #[test] + #[allow(non_snake_case)] + fn []() { + fn do_cast(value: T) -> Result<$TARGET, T> { + cast!(value, $TARGET) + } + + $( + assert!(match do_cast($value) { + $matches $(if $guard)* => true, + _ => false, + }); + )* + } + + #[test] + #[allow(non_snake_case)] + fn []() { + fn do_cast(value: &T) -> Result<&$TARGET, &T> { + cast!(value, &$TARGET) + } + + $( + assert!(match do_cast(&$value).map(|t| t.clone()).map_err(|e| e.clone()) { + $matches $(if $guard)* => true, + _ => false, + }); + )* + } + + #[test] + #[allow(non_snake_case)] + fn []() { + fn do_cast(value: &mut T) -> Result<&mut $TARGET, &mut T> { + cast!(value, &mut $TARGET) + } + + $( + assert!(match do_cast(&mut $value).map(|t| t.clone()).map_err(|e| e.clone()) { + $matches $(if $guard)* => true, + _ => false, + }); + )* + } + } + + test_lifetime_free_cast! { + $($tail)* + } + }; + ( for $TARGET:ty { $( @@ -357,7 +417,10 @@ mod tests { } $( - assert!(matches!(do_cast($value), $matches $(if $guard)*)); + assert!(match do_cast($value) { + $matches $(if $guard)* => true, + _ => false, + }); )* } @@ -369,7 +432,10 @@ mod tests { } $( - assert!(matches!(do_cast(&$value).map(|t| t.clone()).map_err(|e| e.clone()), $matches $(if $guard)*)); + assert!(match do_cast(&$value).map(|t| t.clone()).map_err(|e| e.clone()) { + $matches $(if $guard)* => true, + _ => false, + }); )* } @@ -381,7 +447,10 @@ mod tests { } $( - assert!(matches!(do_cast(&mut $value).map(|t| t.clone()).map_err(|e| e.clone()), $matches $(if $guard)*)); + assert!(match do_cast(&mut $value).map(|t| t.clone()).map_err(|e| e.clone()) { + $matches $(if $guard)* => true, + _ => false, + }); )* } } @@ -406,12 +475,17 @@ mod tests { for f32 { 3.2f32 => Ok(v) if v == 3.2, - 3.2f64 => Err(_), + 3.2f64 => Err(v) if v == 3.2f64, } for String { String::from("hello world") => Ok(v) if v.as_str() == "hello world", "hello world" => Err("hello world"), } + + for Option as Option_u8 { + 0u8 => Err(0u8), + Some(42u8) => Ok(Some(42u8)), + } } }