[][src]Macro rustpython_vm::match_class

macro_rules! match_class {
    ($obj:expr, _ => $default:expr $(,)?) => { ... };
    ($obj:expr, $binding:ident => $default:expr $(,)?) => { ... };
    ($obj:expr, $binding:ident @ $class:ty => $expr:expr, $($rest:tt)*) => { ... };
    ($obj:expr, $class:ty => $expr:expr, $($rest:tt)*) => { ... };
}

Macro to match on the built-in class of a Python object.

Like match, match_class! must be exhaustive, so a default arm with the uncasted object is required.

Examples

use num_bigint::ToBigInt;
use num_traits::Zero;

use rustpython_vm::VirtualMachine;
use rustpython_vm::match_class;
use rustpython_vm::obj::objfloat::PyFloat;
use rustpython_vm::obj::objint::PyInt;
use rustpython_vm::pyobject::PyValue;

let vm = VirtualMachine::new();
let obj = PyInt::new(0).into_ref(&vm).into_object();
assert_eq!(
    "int",
    match_class!(obj.clone(),
        PyInt => "int",
        PyFloat => "float",
        _ => "neither",
    )
);

With a binding to the downcasted type:

use num_bigint::ToBigInt;
use num_traits::Zero;

use rustpython_vm::VirtualMachine;
use rustpython_vm::match_class;
use rustpython_vm::obj::objfloat::PyFloat;
use rustpython_vm::obj::objint::PyInt;
use rustpython_vm::pyobject::PyValue;

let vm = VirtualMachine::new();
let obj = PyInt::new(0).into_ref(&vm).into_object();

let int_value = match_class!(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());