How to generate a compilation error when pointer types differ in Rust?

We can guarantee that the two types are compatible, especially when writing macros. To verify that two arguments are of the same type, for example.

What is the best way to ensure type compatibility?


Like this question C, but for Rust.

+4
source share
1 answer

An easy way to ensure type matching is to assign them a dummy value within a block that never executes.

macro_rules! check_type_pair {
    ($a:expr, $b:expr) => {
        if false {
            let _type_check = if false {$a} else {$b};
        }
    }
}

Then in the macro you can simply add:

check_type_pair!($arg_1, $arg_2);

See usage example .

+5
source

Source: https://habr.com/ru/post/1659373/


All Articles