gfxmath_vec2/impls/sub/sub.rs
1use core::ops::Sub;
2use crate::Vec2;
3
4///
5/// ```
6/// use gfxmath_vec2::Vec2;
7///
8/// let mut a = Vec2::<f32>::new(0.5, 2.5);
9/// let b = Vec2::<f32>::new(1.5, 2.5);
10/// let c = Vec2::from(&a - &b);
11///
12/// a.x = 1.0;
13///
14/// assert_eq!(-1.0, c.x);
15/// assert_eq!( 0.0, c.y);
16///
17/// let c2 = Vec2::from(a - b);
18///
19/// assert_eq!(-0.5, c2.x);
20/// assert_eq!( 0.0, c2.y);
21/// ```
22#[opimps::impl_ops(Sub)]
23#[inline]
24fn sub<T: Sub<Output = T> + Copy>(self: Vec2<T>, rhs: Vec2<T>) -> Vec2<T> {
25 let x = self.x - rhs.x;
26 let y = self.y - rhs.y;
27 Vec2 { x, y }
28}
29
30/// ```
31/// use gfxmath_vec2::Vec2;
32///
33/// let v = Vec2::new(1.0, 2.0);
34///
35/// let res: Vec2<f32> = (v - 3.0).into();
36///
37/// assert_eq!(-2.0, res.x);
38/// assert_eq!(-1.0, res.y);
39/// ```
40#[opimps::impl_ops_rprim(Sub)]
41#[inline]
42fn sub<T>(self: Vec2<T>, rhs: T) -> Vec2<T> where T: Sub<Output = T> + Copy {
43 Vec2 { x: self.x - rhs, y: self.y - rhs }
44}
45
46/// ```
47/// use gfxmath_vec2::Vec2;
48///
49/// let v = Vec2::<f32>::new(1.0, 2.0);
50///
51/// let res = (3.0 - v);
52///
53/// assert_eq!( 2.0, res.x);
54/// assert_eq!( 1.0, res.y);
55/// ```
56#[opimps::impl_ops_lprim(Sub)]
57#[inline]
58fn sub(self: f32, rhs: Vec2<f32>) -> Vec2<f32> {
59 let x = self - rhs.x;
60 let y = self - rhs.y;
61 Vec2 { x, y }
62}
63
64/// ```
65/// use gfxmath_vec2::Vec2;
66///
67/// let v = Vec2::<f32>::new(1.0, 2.0);
68///
69/// let res = (3.0 - v);
70///
71/// assert_eq!( 2.0, res.x);
72/// assert_eq!( 1.0, res.y);
73/// ```
74#[opimps::impl_ops_lprim(Sub)]
75#[inline]
76fn sub(self: f64, rhs: Vec2<f64>) -> Vec2<f64> {
77 let x = self - rhs.x;
78 let y = self - rhs.y;
79 Vec2 { x, y }
80}
81
82/// ```
83/// use gfxmath_vec2::Vec2;
84///
85/// let v = Vec2::<i32>::new(1, 2);
86///
87/// let res = (3 - v);
88///
89/// assert_eq!( 2, res.x);
90/// assert_eq!( 1, res.y);
91/// ```
92#[opimps::impl_ops_lprim(Sub)]
93#[inline]
94fn sub(self: i32, rhs: Vec2<i32>) -> Vec2<i32> {
95 let x = self - rhs.x;
96 let y = self - rhs.y;
97 Vec2 { x, y }
98}
99
100/// ```
101/// use gfxmath_vec2::Vec2;
102///
103/// let v = Vec2::<i64>::new(1, 2);
104///
105/// let res = (3 - v);
106///
107/// assert_eq!( 2, res.x);
108/// assert_eq!( 1, res.y);
109/// ```
110#[opimps::impl_ops_lprim(Sub)]
111#[inline]
112fn sub(self: i64, rhs: Vec2<i64>) -> Vec2<i64> {
113 let x = self - rhs.x;
114 let y = self - rhs.y;
115 Vec2 { x, y }
116}