Macro rustpython_vm::match_class
source · macro_rules! match_class { (match ($obj:expr) { _ => $default:expr $(,)? }) => { ... }; (match ($obj:expr) { $binding:ident => $default:expr $(,)? }) => { ... }; (match ($obj:expr) { ref $binding:ident => $default:expr $(,)? }) => { ... }; (match ($obj:expr) { $binding:ident @ $class:ty => $expr:block $($rest:tt)* }) => { ... }; (match ($obj:expr) { ref $binding:ident @ $class:ty => $expr:block $($rest:tt)* }) => { ... }; (match ($obj:expr) { $binding:ident @ $class:ty => $expr:expr, $($rest:tt)* }) => { ... }; (match ($obj:expr) { ref $binding:ident @ $class:ty => $expr:expr, $($rest:tt)* }) => { ... }; (match ($obj:expr) { $class:ty => $expr:block $($rest:tt)* }) => { ... }; (match ($obj:expr) { $class:ty => $expr:expr, $($rest:tt)* }) => { ... }; (match $($rest:tt)*) => { ... }; (@parse_match ($($target:tt)*) ({ $($inner:tt)* })) => { ... }; (@parse_match ($($target:tt)*) ($next:tt $($rest:tt)*)) => { ... }; }
Expand description
Macro to match on the built-in class of a Python object.
Like match
, match_class!
must be exhaustive, so a default arm without
casting is required.
§Examples
use malachite_bigint::ToBigInt;
use num_traits::Zero;
use rustpython_vm::match_class;
use rustpython_vm::builtins::{PyFloat, PyInt};
use rustpython_vm::{PyPayload};
let obj = PyInt::from(0).into_pyobject(vm);
assert_eq!(
"int",
match_class!(match obj {
PyInt => "int",
PyFloat => "float",
_ => "neither",
})
);
With a binding to the downcasted type:
use malachite_bigint::ToBigInt;
use num_traits::Zero;
use rustpython_vm::match_class;
use rustpython_vm::builtins::{PyFloat, PyInt};
use rustpython_vm::{ PyPayload};
let obj = PyInt::from(0).into_pyobject(vm);
let int_value = match_class!(match obj {
i @ PyInt => i.as_bigint().clone(),
f @ PyFloat => f.to_f64().to_bigint().unwrap(),
obj => panic!("non-numeric object {:?}", obj),
});
assert!(int_value.is_zero());