Skip to content

Instantly share code, notes, and snippets.

@danielSanchezQ
Created January 31, 2022 12:53
Show Gist options
  • Save danielSanchezQ/4586fcf2b5359dbc424697527b4e211f to your computer and use it in GitHub Desktop.
Save danielSanchezQ/4586fcf2b5359dbc424697527b4e211f to your computer and use it in GitHub Desktop.
Compile time str eq
use const_str;
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";
}
pub const fn unique_ids(to_check: &[&'static str]) -> bool {
let mut i: usize = 0;
let mut j: usize = 1;
while i < to_check.len() - 1 {
if const_str::equal!(to_check[i], to_check[j]) {
return false;
}
j += 1;
if j >= to_check.len() {
i += 1;
}
}
true
}
const fn assert_ids() {
const _: () = assert!(unique_ids(&[A::ID, B::ID]));
}
fn main() {
assert_ids();
println!("Compiles successfully!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment