I have some example code, in where I tried to use static_assertions
crate. But I am not sure it is even possible.
use static_assertions::{const_assert_eq, const_assert_ne};
pub trait Foo {
const ID: &'static str;
}
struct A;
struct B;
impl Foo for A {
const ID: &'static str = "A";
}
impl Foo for B {
const ID: &'static str = "B";
}
const fn assert_ids() {
const_assert_ne!(A::ID, B::ID);
}
fn main() {
assert_ids();
println!("Compiles successfully!");
}
Fails compiling with:
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
--> src\main.rs:35:5
|
35 | const_assert_ne!(A::ID, B::ID);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `const_assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info)
I have been reading on some threads like:
but couldn't related it.
EDIT:
When changing the type to usize
in the trait:
pub trait Foo {
const ID: usize;
}
The above example works. So I guess it may be related to the type being &'static str
.