Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. trait Trait1 {
  2. fn is_id(&self, id: usize) -> bool;
  3. }
  4. trait Trait2{}
  5.  
  6. struct Core {
  7. objs: Vec<obj1>,
  8. diff_objs: Vec<obj2>,
  9. }
  10.  
  11. struct obj1 {
  12. stuff: String,
  13. id: usize,
  14. }
  15.  
  16. impl Trait1 for obj1 {
  17. fn is_id(&self, id: usize) -> bool { self.id == id }
  18. }
  19.  
  20. impl Trait2 for obj1 {}
  21.  
  22. struct obj2 {
  23. other_stuff: String,
  24. id: usize,
  25. }
  26.  
  27. impl Trait1 for obj2 {
  28. fn is_id(&self, id: usize) -> bool { self.id == id }
  29. }
  30.  
  31. impl Trait2 for obj2 {}
  32.  
  33. // This could live in impl Core maybe?
  34. fn return_trait<T: Trait1 + Trait2>(core: &Core, id: usize) -> Result<T, String> {
  35. for obj in core.objs.iter() {
  36. if obj.is_id(id) { return Ok(obj); }
  37. }
  38. for obj in core.diff_objs.iter() {
  39. if obj.is_id(id) { return Ok(obj); }
  40. }
  41. Err("Couldn't find ID".to_string())
  42. }
  43.  
  44. fn main() {
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement