Macro is_impl

Source
macro_rules! is_impl {
    ($type:ty, $ex:expr) => { ... };
}
Expand description

Checks that an expression ex conforms to type type at compile time. This should have no runtime cost and can be used with trait types when adding dyn.

§Short example: check inline that a value implements the Debug trait

let x = is_impl!(dyn Debug, 2);

§More elaborate example: making sense of a complex future pipeline

extern crate is_impl;
use is_impl::*;

// assert (inline) the future type
let req = is_impl!(dyn Future01<Item=i32, Error=()>, request("something"));
// assert (inline) that we are in new futures and have successfully gotten rid of the unit error
let new_future = is_impl!(dyn Future03<Output = i32>, req.compat().into_future().map(|x| x.unwrap()));
// assert (inline) the transformed type
let transformed = is_impl!(dyn Future03<Output = i32>, transform(new_future));
// assert (inline) that we are back in futures 0.1 land with an error
let res = is_impl!(dyn Future01<Item=i32, Error=()>, transformed.unit_error().compat());
// do the thing!
spawn(res);

Usually I use is_impl! to figure out where the types don’t align during development, and then remove the assertions again once I am happy with the code. But since the macro does not have a runtime cost you can also leave them in.