Skip to content

Instantly share code, notes, and snippets.

@pmsanford
Created November 12, 2014 00:13
Show Gist options
  • Save pmsanford/6aaae4865e3b93f59ea2 to your computer and use it in GitHub Desktop.
Save pmsanford/6aaae4865e3b93f59ea2 to your computer and use it in GitHub Desktop.
trait Fooable { fn foo(&mut self); }
trait Barable { fn bar(&mut self); }
struct StructA;
struct StructB;
impl Fooable for StructA { fn foo(&mut self) {} }
impl Barable for StructA { fn bar(&mut self) {} }
impl Fooable for StructB { fn foo(&mut self) {} }
impl Barable for StructB { fn bar(&mut self) {} }
fn main() {
let mut a1 = StructA;
let mut a2 = StructA;
let mut b1 = StructB;
let mut b2 = StructB;
let mut a_structs: Vec<&mut StructA> = Vec::new();
a_structs.push(&mut a1);
a_structs.push(&mut a2);
let mut b_structs: Vec<&mut StructB> = Vec::new();
b_structs.push(&mut b1);
b_structs.push(&mut b2);
foo_iter(&mut a_structs);
foo_iter(&mut b_structs);
bar_iter(&mut a_structs);
bar_iter(&mut b_structs);
}
fn foo_iter<T>(fooable: &mut Vec<&mut T>) where T: Fooable {
for fa in fooable.iter_mut() {
fa.foo();
}
}
fn bar_iter<T>(barable: &mut Vec<&mut T>) where T: Barable {
for ba in barable.iter_mut() {
ba.bar();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment