graphene/
vec2.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{fmt, ops};
4
5use glib::translate::*;
6
7use crate::{ffi, Vec2};
8
9impl Vec2 {
10    #[doc(alias = "graphene_vec2_init")]
11    pub fn new(x: f32, y: f32) -> Self {
12        assert_initialized_main_thread!();
13        unsafe {
14            let mut vec = Self::uninitialized();
15            ffi::graphene_vec2_init(vec.to_glib_none_mut().0, x, y);
16            vec
17        }
18    }
19
20    #[doc(alias = "graphene_vec2_init_from_float")]
21    #[doc(alias = "init_from_float")]
22    pub fn from_float(src: [f32; 2]) -> Self {
23        assert_initialized_main_thread!();
24        unsafe {
25            let mut vec = Self::uninitialized();
26            ffi::graphene_vec2_init_from_float(vec.to_glib_none_mut().0, src.as_ptr() as *const _);
27            vec
28        }
29    }
30
31    #[doc(alias = "graphene_vec2_to_float")]
32    pub fn to_float(&self) -> [f32; 2] {
33        unsafe {
34            let mut out = std::mem::MaybeUninit::uninit();
35            ffi::graphene_vec2_to_float(self.to_glib_none().0, out.as_mut_ptr());
36            out.assume_init()
37        }
38    }
39}
40
41impl fmt::Debug for Vec2 {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.debug_struct("Vec2")
44            .field("x", &self.x())
45            .field("y", &self.y())
46            .finish()
47    }
48}
49
50impl Default for Vec2 {
51    fn default() -> Self {
52        Self::zero()
53    }
54}
55
56// addition/subtraction
57impl ops::Add<Vec2> for Vec2 {
58    type Output = Vec2;
59
60    fn add(self, rhs: Vec2) -> Self::Output {
61        Vec2::add(&self, &rhs)
62    }
63}
64impl ops::AddAssign<Vec2> for Vec2 {
65    fn add_assign(&mut self, rhs: Vec2) {
66        *self = *self + rhs;
67    }
68}
69impl ops::Sub<Vec2> for Vec2 {
70    type Output = Vec2;
71
72    fn sub(self, rhs: Vec2) -> Self::Output {
73        Vec2::subtract(&self, &rhs)
74    }
75}
76impl ops::SubAssign<Vec2> for Vec2 {
77    fn sub_assign(&mut self, rhs: Vec2) {
78        *self = *self - rhs;
79    }
80}
81impl ops::Neg for Vec2 {
82    type Output = Vec2;
83
84    fn neg(self) -> Self::Output {
85        Vec2::negate(&self)
86    }
87}
88
89// scalar multiplication
90impl ops::Mul<f32> for Vec2 {
91    type Output = Vec2;
92
93    fn mul(self, rhs: f32) -> Self::Output {
94        Vec2::scale(&self, rhs)
95    }
96}
97impl ops::MulAssign<f32> for Vec2 {
98    fn mul_assign(&mut self, rhs: f32) {
99        *self = *self * rhs;
100    }
101}
102impl ops::Mul<Vec2> for f32 {
103    type Output = Vec2;
104
105    fn mul(self, rhs: Vec2) -> Self::Output {
106        rhs * self
107    }
108}
109
110// Component-wise multiplication/division
111impl ops::Mul<Vec2> for Vec2 {
112    type Output = Vec2;
113
114    fn mul(self, rhs: Vec2) -> Self::Output {
115        Vec2::multiply(&self, &rhs)
116    }
117}
118impl ops::MulAssign<Vec2> for Vec2 {
119    fn mul_assign(&mut self, rhs: Vec2) {
120        *self = *self * rhs;
121    }
122}
123impl ops::Div<Vec2> for Vec2 {
124    type Output = Vec2;
125
126    fn div(self, rhs: Vec2) -> Self::Output {
127        Vec2::divide(&self, &rhs)
128    }
129}
130impl ops::DivAssign<Vec2> for Vec2 {
131    fn div_assign(&mut self, rhs: Vec2) {
132        *self = *self / rhs;
133    }
134}