#type #assert #typemap #compile-time #run-time #error

assert-type-eq

Macro to assert types across potentially different crate versions are compatible

1 unstable release

Uses old Rust 2015

0.1.0 Aug 31, 2017

#2783 in Rust patterns

Download history 476/week @ 2023-10-18 529/week @ 2023-10-25 182/week @ 2023-11-01 166/week @ 2023-11-08 259/week @ 2023-11-15 333/week @ 2023-11-22 182/week @ 2023-11-29 144/week @ 2023-12-06 292/week @ 2023-12-13 132/week @ 2023-12-20 170/week @ 2023-12-27 299/week @ 2024-01-03 392/week @ 2024-01-10 702/week @ 2024-01-17 909/week @ 2024-01-24 1651/week @ 2024-01-31

3,731 downloads per month
Used in 2 crates

Apache-2.0

6KB

assert-type-eq

Build Status crates.io

This Rust crate adds a macro to assert at compile time that certain types are the same. This is useful when using the same crate via different dependencies, which may bring in different versions of the same crate, which Rust considers to have incompatible types. In most circumstances this will by itself lead to a compile-time error, however when using runtime structures such as Any or TypeMap, this will not cause compile-time issues, but rather runtime problems with missing or unexpected data. By using this crate, the different versions of the same crate can be asserted to be compatible, turning a runtime error into a compile-time error.


lib.rs:

Statically assert that types from potentially different crate versions via different dependencies are identical.

Until RFC 1977 (public dependencies) is accepted, the situation where multiple different versions of the same crate are present is possible. In most situations this will simply cause code to not compile, as types mismatch, however with runtime structures like TypeMap this leads to runtime errors (often silent!) instead. This macro allows compile-time assertion that types via different dependencies are identical, and will interoperate, which is easier to debug than runtime errors.

Usage:

#[macro_use]
extern crate assert_type_eq;

pub mod my_crate {
    pub struct MyStruct;
}

mod a {
    pub use super::my_crate;
}

mod b {
    pub use super::my_crate;
}

assert_type_eq!(
    my_crate::MyStruct,
    a::my_crate::MyStruct,
    b::my_crate::MyStruct,
);

fn main() {
    // ...
}

Specify all versions of the same type via different dependencies. Any types that do not match the first type in the macro will cause a compile-time error.

No runtime deps