#impl #traits #macro

is-impl

Rust macro to check that a type conforms to an impl

2 releases

0.1.1 Nov 6, 2019
0.1.0 Nov 6, 2019

#2148 in Rust patterns

MIT/Apache

7KB

Is-Impl   Build Status Latest Version

About

This is a tiny macro that just checks that an expression conforms to a trait type.

Motivation

When writing complex futures and streams code at the time when I had to use both futures 0.1 and futures 0.3 libraries, I often got lot with all the .compat() calls. One technique I typically use in other typed languages is to add types everywhere until I can precisely locate the problem.

The problem is that in rust the types are either sometimes several pages long, or not nameable at all. So ideally what you want to do is to check that a type conforms to a trait. Naively I tried something like this:

let f: impl Future<Output=i32> = something();

But that is not valid rust syntax. impl traits can not be used in these positions. Now with this trait you could write the above assertion as

let f = is_impl!(dyn Future<Output=i32>, something());

A larger example can be found in the examples and in the documentation.

There are similar crates such as assert_impl, but I find it useful to use the assertion inline during development.

No runtime deps