How to check if 'Box<Any>' contains 'Unit' in a short expression?

70 Views Asked by At

What is a convenient way to check a Box<Any> is () (created by Box::new(())).

Currently I have this, but I suspect there might be a more compact way to express this since the assignment seems redundant.

pub fn some_function(value: Box<Any>) {
    if let Some(&()) = value.downcast_ref() {
        // pass
    } else {
        panic!();
    }
}
1

There are 1 best solutions below

0
On

This can be done using Any::is method, eg:

assert!(value.is::<()>());