Allow 'ref' in match_class macro

This commit is contained in:
coolreader18 authored and Noah committed 2020-03-01 23:09:28 -06:00
1 parent 57b149ba81
commit c8b7d9d67d
1 file changed
+13
+13
View File
@@ -244,6 +244,10 @@ macro_rules! match_class {
let $binding = $obj;
$default
}};
(match ($obj:expr) { ref $binding:ident => $default:expr $(,)? }) => {{
let $binding = &$obj;
$default
}};
// An arm taken when the object is an instance of the specified built-in
// class and binding the downcasted object to the specified identifier and
@@ -251,6 +255,9 @@ macro_rules! match_class {
(match ($obj:expr) { $binding:ident @ $class:ty => $expr:block $($rest:tt)* }) => {
$crate::match_class!(match ($obj) { $binding @ $class => ($expr), $($rest)* })
};
(match ($obj:expr) { ref $binding:ident @ $class:ty => $expr:block $($rest:tt)* }) => {
$crate::match_class!(match ($obj) { ref $binding @ $class => ($expr), $($rest)* })
};
// An arm taken when the object is an instance of the specified built-in
// class and binding the downcasted object to the specified identifier.
@@ -260,6 +267,12 @@ macro_rules! match_class {
Err(_obj) => $crate::match_class!(match (_obj) { $($rest)* }),
}
};
(match ($obj:expr) { ref $binding:ident @ $class:ty => $expr:expr, $($rest:tt)* }) => {
match $obj.payload::<$class>() {
Some($binding) => $expr,
None => $crate::match_class!(match ($obj) { $($rest)* }),
}
};
// An arm taken when the object is an instance of the specified built-in
// class and the target expression is a block.