Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hack to have something like associated consts in stable
trait Associated<T> {
fn associated(&self) -> T;
}
trait Thing : Associated<&'static str> {
fn whatever(&self);
}
macro_rules! associated {
($t:ident, $val:expr => $typ:ty) => {
impl $crate::Associated<$typ> for $t {
fn associated(&self) -> $typ {
$val
}
}
}
}
struct Stuff;
impl Thing for Stuff {
fn whatever(&self) {}
}
associated!(Stuff, "data" => &'static str);
struct Muff;
impl Thing for Muff {
fn whatever(&self) {}
}
associated!(Muff, "mata" => &'static str);
// obviously with one value it doesn't scale, but there could be a HashMap or something instead of a str
fn main() {
println!("{}", Stuff.associated());
println!("{}", Muff.associated());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment