Skip to content

Instantly share code, notes, and snippets.

@pnkfelix
Last active December 19, 2015 12:38
Show Gist options
  • Save pnkfelix/5956068 to your computer and use it in GitHub Desktop.
Save pnkfelix/5956068 to your computer and use it in GitHub Desktop.
trait A { fn foo(&self); }
trait B<X:A> { fn bar(&self, @X); }
trait C<X:A, Y:B<X>> { fn baz(&self, @X, @Y); }
// This doesn't work, as trait bounds are not allowed in structure defn's.x
// struct Cat<X:A, Y:B<X>> { x: @X, y: @Y }
// Workaround by putting the check elsewhere
struct Cat<X, Y> { x: @X, y: @Y }
impl<X:A, Y:B<X>> C<X,Y> for Cat<X,Y> {
fn baz(&self, x_arg: @X, y_arg: @Y) {
println("baz");
self.x.foo();
x_arg.foo();
self.y.bar(x_arg);
y_arg.bar(x_arg);
}
}
struct Alpha(int);
struct Beta(int);
impl A for Alpha { fn foo(&self) { println("Alpha foo"); } }
impl B<Alpha> for Beta { fn bar(&self, x:@Alpha) { println("Beta bar"); x.foo(); } }
fn main() {
let x = @Alpha(1);
let y = @Beta(2);
let cat : Cat<Alpha, Beta> = Cat::<Alpha, Beta> { x: @Alpha(3), y: @Beta(4) };
println(fmt!("Hello World %?", cat.baz(x, y)));
}
% rustc ~/Dev/Rust/foo.rs && ~/Dev/Rust/foo
baz
Alpha foo
Alpha foo
Beta bar
Alpha foo
Beta bar
Alpha foo
Hello World ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment