Skip to content

Instantly share code, notes, and snippets.

@durka
Forked from anonymous/playground.rs
Last active August 29, 2015 14:23
Show Gist options
  • Save durka/1bdcbeb00c5414f9bcdc to your computer and use it in GitHub Desktop.
Save durka/1bdcbeb00c5414f9bcdc to your computer and use it in GitHub Desktop.
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