Created
January 31, 2022 12:53
-
-
Save danielSanchezQ/4586fcf2b5359dbc424697527b4e211f to your computer and use it in GitHub Desktop.
Compile time str eq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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