Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2016 13:36
Show Gist options
  • Save anonymous/4a8a0275d985e975cdeb8b438db5ff5f to your computer and use it in GitHub Desktop.
Save anonymous/4a8a0275d985e975cdeb8b438db5ff5f to your computer and use it in GitHub Desktop.
Shared via Rust Playground
struct A{_x:f32}
struct B{_y:u32}
// #[derive(EnumKey(FooKey))]
enum Foo {
A(A),
B(B)
}
fn main() {
let _a = Foo::A(A {_x:1.0});
let _b = Foo::B(B {_y:1});
assert!(_a.key() == FooKey::A);
assert!(_b.key() == FooKey::B);
}
// The following impl could be autoderived
pub trait Key {
type Key;
fn key(&self) -> Self::Key;
}
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
enum FooKey {A,B}
impl Key for Foo {
type Key=FooKey;
fn key(&self) -> Self::Key {
match self {
&Foo::A(..) => FooKey::A,
&Foo::B(..) => FooKey::B,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment