Guest User

Untitled

a guest
Jan 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. trait VectorTrait {
  2. fn new(x: f64, y: f64, z: f64) -> Self;
  3. fn add<T: VectorTrait>(self, rhs: T) -> Self;
  4. fn x(&self) -> f64;
  5. fn y(&self) -> f64;
  6. fn z(&self) -> f64;
  7. }
  8.  
  9. #[derive(Debug, Clone, PartialEq)]
  10. struct Vec3d<T>(T);
  11.  
  12. impl<T: VectorTrait> Vec3d<T> {
  13. fn new(x: f64, y: f64, z: f64) -> Self {
  14. Vec3d(T::new(x, y, z))
  15. }
  16. }
  17.  
  18. impl<L: VectorTrait, R: VectorTrait> std::ops::Add<Vec3d<R>> for Vec3d<L> {
  19. type Output = Self;
  20. fn add(self, rhs: Vec3d<R>) -> Self::Output {
  21. Vec3d(self.0.add(rhs.0))
  22. }
  23. }
  24.  
  25. impl VectorTrait for [f64; 3] {
  26. fn new(x: f64, y: f64, z: f64) -> Self {
  27. [x, y, z]
  28. }
  29.  
  30. fn x(&self) -> f64 {self[0]}
  31. fn y(&self) -> f64 {self[1]}
  32. fn z(&self) -> f64 {self[2]}
  33.  
  34. fn add<T: VectorTrait>(self, rhs: T) -> Self {
  35. <Self as VectorTrait>::new(self.x() + rhs.x(), self.y() + rhs.y(), self.z() + rhs.z())
  36. }
  37. }
  38.  
  39. impl VectorTrait for Vec<f64> {
  40. fn new(x: f64, y: f64, z: f64) -> Self {
  41. vec![x, y, z]
  42. }
  43.  
  44. fn x(&self) -> f64 {self[0]}
  45. fn y(&self) -> f64 {self[1]}
  46. fn z(&self) -> f64 {self[2]}
  47.  
  48. fn add<T: VectorTrait>(self, rhs: T) -> Self {
  49. <Self as VectorTrait>::new(self.x() + rhs.x(), self.y() + rhs.y(), self.z() + rhs.z())
  50. }
  51. }
  52.  
  53. type ListVec3d = Vec3d<[f64; 3]>;
  54. type VecVec3d = Vec3d<Vec<f64>>;
  55.  
  56. fn main() {
  57. let x = ListVec3d::new(1.0, 2.0, 3.0);
  58. let y = VecVec3d::new(2.0, 5.0, 1.0);
  59. println!("{:?}", x + y);
  60. }
Add Comment
Please, Sign In to add comment