Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2016 12:56
Show Gist options
  • Save anonymous/888cbd1ac4be542bce24a1fadf472566 to your computer and use it in GitHub Desktop.
Save anonymous/888cbd1ac4be542bce24a1fadf472566 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
trait Hello { fn hello(&self){}}
struct A{_x:f32}
struct B{_y:u32}
impl Hello for A {}
impl Hello for B {}
// #[derive(EnumAsTrait(Hello))]
enum Foo {
A(A),
B(B)
}
pub trait AsTrait {
type T: ?Sized;
fn as_trait(&self) -> &Self::T;
}
// The following impl could be autoderived
impl AsTrait for Foo {
type T=Hello;
fn as_trait(&self) -> &Self::T {
match self {
&Foo::A(ref a) => a as &Self::T,
&Foo::B(ref b) => b as &Self::T,
}
}
}
fn main() {
let a = Foo::A(A {_x:1.0});
let b = Foo::B(B {_y:1});
a.as_trait().hello();
b.as_trait().hello();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment