From c8b7d9d67dd85ff2638cafdc84f6f0725fb03614 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Sat, 4 Jan 2020 12:51:21 -0600 Subject: [PATCH] Allow 'ref' in match_class macro --- vm/src/macros.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 4b2ad0b04..5eeb824a3 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -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.