Guest User

Untitled

a guest
Mar 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. pub trait A {
  2. fn method_a(&self) -> f64;
  3. }
  4.  
  5. trait B<T> where T: A {
  6. fn method_b(&mut self) -> &Vec<T>;
  7. }
  8.  
  9. trait GenericB {
  10. fn method_generic(&mut self) -> Vec<&A>;
  11. }
  12.  
  13. impl<T> GenericB for B<T> where T: A {
  14. fn method_generic(&mut self) -> Vec<&A> {
  15. B::method_b(self).iter().map(
  16. |v| v as &A
  17. ).collect()
  18. }
  19. }
  20.  
  21. struct Anon_a {
  22. pub x: f64
  23. }
  24.  
  25. impl A for Anon_a {
  26. fn method_a(&self) -> f64 {
  27. self.x
  28. }
  29. }
  30.  
  31. struct Anon_b {
  32. pub a: Option<Vec<Anon_a>>
  33. }
  34.  
  35. impl B<Anon_a> for Anon_b {
  36. fn method_b(&mut self) -> &Vec<Anon_a> {
  37. self.a.as_ref().unwrap()
  38. }
  39. }
  40.  
  41. fn main() {
  42. let b = Anon_b {
  43. a: Some(vec![Anon_a{x: 32.3}, Anon_a{x: 64.0}])
  44. };
  45. let c: Box<GenericB> = Box::new(b);
  46. }
Add Comment
Please, Sign In to add comment