Created
June 25, 2015 02:55
-
-
Save anonymous/4702c2970449bc8b6595 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or 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
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