1 use std::ops::{Add, AddAssign, Mul};
3 pub type Nanoseconds = u64;
7 ( $x:expr, $y:expr ) => {
8 Point2D { x: $x, y: $y }
12 #[derive(Debug, Default, Copy, Clone, PartialEq)]
13 pub struct Point2D<T> {
19 pub fn length(self) -> f64 {
20 ((self.x * self.x) + (self.y * self.y)).sqrt()
24 impl<T: Add<Output = T>> Add for Point2D<T> {
25 type Output = Point2D<T>;
27 fn add(self, rhs: Point2D<T>) -> Self::Output {
35 impl<T: AddAssign> AddAssign for Point2D<T> {
36 fn add_assign(&mut self, rhs: Point2D<T>) {
42 impl<T> From<(T, T)> for Point2D<T> {
43 fn from(item: (T, T)) -> Self {
53 ( $x:expr, $y:expr ) => {
64 impl<T: Mul<Output = T> + Copy> Rect<T> {
66 pub fn area(&self) -> T {
67 self.width * self.height
71 impl<T> From<(T, T)> for Rect<T> {
72 fn from(item: (T, T)) -> Self {
85 fn immutable_copy_of_point() {
87 let mut b = a; // Copy
88 assert_eq!(a, b); // PartialEq
90 assert_ne!(a, b); // PartialEq
95 let mut a = point!(1, 0);
96 assert_eq!(a + point!(2, 2), point!(3, 2)); // Add
97 a += point!(2, 2); // AddAssign
98 assert_eq!(a, point!(3, 2));
102 fn area_for_rect_of_multipliable_type() {
103 let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
104 assert_eq!(r.area(), 30 * 20);
105 // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String