mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Allow 'ref' in match_class macro
This commit is contained in:
1 parent
57b149ba81
commit
c8b7d9d67d
1 file changed
+13
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user