Skip to content

Commit

Permalink
Fix MSRV incompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sagebind committed Apr 22, 2022
1 parent a22e72c commit 0b374a7
Showing 1 changed file with 78 additions and 4 deletions.
82 changes: 78 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<cast_lifetime_free_ $name>]() {
fn do_cast<T>(value: T) -> Result<$TARGET, T> {
cast!(value, $TARGET)
}

$(
assert!(match do_cast($value) {
$matches $(if $guard)* => true,
_ => false,
});
)*
}

#[test]
#[allow(non_snake_case)]
fn [<cast_lifetime_free_ref_ $name>]() {
fn do_cast<T>(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 [<cast_lifetime_free_mut_ $name>]() {
fn do_cast<T>(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 {
$(
Expand All @@ -357,7 +417,10 @@ mod tests {
}

$(
assert!(matches!(do_cast($value), $matches $(if $guard)*));
assert!(match do_cast($value) {
$matches $(if $guard)* => true,
_ => false,
});
)*
}

Expand All @@ -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,
});
)*
}

Expand All @@ -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,
});
)*
}
}
Expand All @@ -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<u8> as Option_u8 {
0u8 => Err(0u8),
Some(42u8) => Ok(Some(42u8)),
}
}
}

0 comments on commit 0b374a7

Please sign in to comment.