Skip to content

Instantly share code, notes, and snippets.

Created June 25, 2015 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4702c2970449bc8b6595 to your computer and use it in GitHub Desktop.
Save anonymous/4702c2970449bc8b6595 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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);
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