-
-
Save durka/1bdcbeb00c5414f9bcdc to your computer and use it in GitHub Desktop.
Hack to have something like associated consts in stable
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); | |
// 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