Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #![allow(unused)]
  2. extern crate either; // 1.5.1
  3. use either::Either;
  4. use std::fs;
  5.  
  6. trait Common {
  7. fn bounded_box(self) -> f32;
  8. }
  9.  
  10. trait B: Common {
  11. // B specific stuff here
  12. }
  13.  
  14. trait C: Common {
  15. // C specific stuff here
  16. }
  17.  
  18. impl Common for f64 {
  19. fn bounded_box(self) -> f32 {
  20. return 2.0;
  21. }
  22. }
  23.  
  24. impl B for f64 {}
  25.  
  26. impl Common for f32 {
  27. fn bounded_box(self) -> f32 {
  28. return 1.0;
  29. }
  30. }
  31.  
  32. impl C for f32 {}
  33.  
  34. fn interact<T: B, U: C>(x: Either<T, U>) -> f32 {
  35. match x {
  36. Either::Left(v) => v.bounded_box(),
  37. Either::Right(v) => v.bounded_box(),
  38. }
  39. }
  40.  
  41. // Wait! We can just impl Common for Either!
  42.  
  43. impl<T, U> Common for Either<T, U>
  44. where T: Common,
  45. U: Common {
  46. fn bounded_box(self) -> f32 {
  47. match self {
  48. Either::Left(v) => v.bounded_box(),
  49. Either::Right(v) => v.bounded_box(),
  50. }
  51. }
  52. }
  53.  
  54. fn main() {
  55. println!("{}", Either::<f64, f32>::Right(1.0f32).bounded_box());
  56. println!("{}", Either::<f64, f32>::Left(1.0f64).bounded_box());
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement