Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait VectorTrait {
- fn new(x: f64, y: f64, z: f64) -> Self;
- fn add<T: VectorTrait>(self, rhs: T) -> Self;
- fn x(&self) -> f64;
- fn y(&self) -> f64;
- fn z(&self) -> f64;
- }
- #[derive(Debug, Clone, PartialEq)]
- struct Vec3d<T>(T);
- impl<T: VectorTrait> Vec3d<T> {
- fn new(x: f64, y: f64, z: f64) -> Self {
- Vec3d(T::new(x, y, z))
- }
- }
- impl<L: VectorTrait, R: VectorTrait> std::ops::Add<Vec3d<R>> for Vec3d<L> {
- type Output = Self;
- fn add(self, rhs: Vec3d<R>) -> Self::Output {
- Vec3d(self.0.add(rhs.0))
- }
- }
- impl VectorTrait for [f64; 3] {
- fn new(x: f64, y: f64, z: f64) -> Self {
- [x, y, z]
- }
- fn x(&self) -> f64 {self[0]}
- fn y(&self) -> f64 {self[1]}
- fn z(&self) -> f64 {self[2]}
- fn add<T: VectorTrait>(self, rhs: T) -> Self {
- <Self as VectorTrait>::new(self.x() + rhs.x(), self.y() + rhs.y(), self.z() + rhs.z())
- }
- }
- impl VectorTrait for Vec<f64> {
- fn new(x: f64, y: f64, z: f64) -> Self {
- vec![x, y, z]
- }
- fn x(&self) -> f64 {self[0]}
- fn y(&self) -> f64 {self[1]}
- fn z(&self) -> f64 {self[2]}
- fn add<T: VectorTrait>(self, rhs: T) -> Self {
- <Self as VectorTrait>::new(self.x() + rhs.x(), self.y() + rhs.y(), self.z() + rhs.z())
- }
- }
- type ListVec3d = Vec3d<[f64; 3]>;
- type VecVec3d = Vec3d<Vec<f64>>;
- fn main() {
- let x = ListVec3d::new(1.0, 2.0, 3.0);
- let y = VecVec3d::new(2.0, 5.0, 1.0);
- println!("{:?}", x + y);
- }
Add Comment
Please, Sign In to add comment