coord/
vec1.rs

1//! Functionality pertaining to `Vec1`
2
3use core::ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
4use core::fmt;
5
6#[allow(unused_imports)]
7use num::{Num, Integer, Unsigned, Signed, Float};
8
9#[allow(unused_imports)]
10use super::{Vector, VecItem, VecNum, VecInt, VecUnsigned, VecSigned, VecFloat};
11
12#[cfg(feature = "serialize")]
13#[derive(Copy, Clone, Default, Hash, Eq, PartialEq, Serialize, Deserialize)]
14pub struct Vec1<T: VecItem> {
15    pub x: T,
16}
17
18#[cfg(not(feature = "serialize"))]
19#[derive(Copy, Clone, Default, Hash, Eq, PartialEq)]
20pub struct Vec1<T: VecItem> {
21    pub x: T,
22}
23
24impl<T: VecItem> Vec1<T> {
25    /// Creates a new Vec1 from a single component
26    pub fn new(x: T) -> Self { Self { x, ..Default::default() } }
27
28    /// Returns the elements of the vector as an array
29    pub fn elements(&self) -> [T; 1] { [self.x] }
30
31    /// Apply an operation to all elements of this vector, returning the result
32    pub fn map<U: VecItem, F: Fn(T) -> U>(&self, f: F) -> Vec1<U> {
33        Vec1 {
34            x: f(self.x),
35        }
36    }
37
38    pub fn convert_to<U: VecItem + From<T>>(&self) -> Vec1<U> {
39        Vec1 {
40            x: U::from(self.x),
41        }
42    }
43}
44
45impl<T: VecItem> Vector for Vec1<T> {
46    type Item = T;
47}
48
49// Debug and Display traits
50
51impl<T: VecItem + fmt::Debug> fmt::Debug for Vec1<T> {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        write!(f, "(x: {:?})", self.x)
54    }
55}
56
57impl<T: VecItem + fmt::Display> fmt::Display for Vec1<T> {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        write!(f, "({})", self.x)
60    }
61}
62
63// From traits
64
65impl<T: VecItem> From<T> for Vec1<T> {
66    fn from(item: T) -> Self { Self { x: item, ..Default::default() } }
67}
68
69impl<T: VecItem> From<[T; 1]> for Vec1<T> {
70    fn from(arr: [T; 1]) -> Self { Self { x: arr[0], ..Default::default() } }
71}
72
73// Op traits
74
75impl<T> Add for Vec1<T> where T: VecItem + Add, T::Output: VecItem + Add {
76    type Output = Vec1<T::Output>;
77    fn add(self, other: Self) -> Vec1<T::Output> {
78        Vec1 {
79            x: self.x + other.x,
80            ..Default::default()
81        }
82    }
83}
84
85impl<T> Sub for Vec1<T> where T: VecItem + Sub, T::Output: VecItem + Sub {
86    type Output = Vec1<T::Output>;
87    fn sub(self, other: Self) -> Vec1<T::Output> {
88        Vec1 {
89            x: self.x - other.x,
90            ..Default::default()
91        }
92    }
93}
94
95impl<T> Mul for Vec1<T> where T: VecItem + Mul, T::Output: VecItem + Mul {
96    type Output = Vec1<T::Output>;
97    fn mul(self, other: Self) -> Vec1<T::Output> {
98        Vec1 {
99            x: self.x * other.x,
100            ..Default::default()
101        }
102    }
103}
104
105impl<T> Div for Vec1<T> where T: VecItem + Div, T::Output: VecItem + Div {
106    type Output = Vec1<T::Output>;
107    fn div(self, other: Self) -> Vec1<T::Output> {
108        Vec1 {
109            x: self.x / other.x,
110            ..Default::default()
111        }
112    }
113}
114
115// Op primitive traits
116
117impl<T> Add<T> for Vec1<T> where T: VecItem + Add, T::Output: VecItem + Add {
118    type Output = Vec1<T::Output>;
119    fn add(self, other: T) -> Vec1<T::Output> {
120        Vec1 {
121            x: self.x + other,
122            ..Default::default()
123        }
124    }
125}
126
127impl<T> Sub<T> for Vec1<T> where T: VecItem + Sub, T::Output: VecItem + Sub {
128    type Output = Vec1<T::Output>;
129    fn sub(self, other: T) -> Vec1<T::Output> {
130        Vec1 {
131            x: self.x - other,
132            ..Default::default()
133        }
134    }
135}
136
137impl<T> Mul<T> for Vec1<T> where T: VecItem + Mul, T::Output: VecItem + Mul {
138    type Output = Vec1<T::Output>;
139    fn mul(self, other: T) -> Vec1<T::Output> {
140        Vec1 {
141            x: self.x * other,
142            ..Default::default()
143        }
144    }
145}
146
147impl<T> Div<T> for Vec1<T> where T: VecItem + Div, T::Output: VecItem + Div {
148    type Output = Vec1<T::Output>;
149    fn div(self, other: T) -> Vec1<T::Output> {
150        Vec1 {
151            x: self.x / other,
152            ..Default::default()
153        }
154    }
155}
156
157// Assign operators
158
159impl<T> AddAssign for Vec1<T> where T: VecItem + Add<Output=T> {
160    fn add_assign(&mut self, other: Self) {
161        *self = Vec1 {
162            x: self.x + other.x,
163            ..Default::default()
164        }
165    }
166}
167
168impl<T> SubAssign for Vec1<T> where T: VecItem + Sub<Output=T> {
169    fn sub_assign(&mut self, other: Self) {
170        *self = Vec1 {
171            x: self.x - other.x,
172            ..Default::default()
173        }
174    }
175}
176
177impl<T> MulAssign for Vec1<T> where T: VecItem + Mul<Output=T> {
178    fn mul_assign(&mut self, other: Self) {
179        *self = Vec1 {
180            x: self.x * other.x,
181            ..Default::default()
182        }
183    }
184}
185
186impl<T> DivAssign for Vec1<T> where T: VecItem + Div<Output=T> {
187    fn div_assign(&mut self, other: Self) {
188        *self = Vec1 {
189            x: self.x / other.x,
190            ..Default::default()
191        }
192    }
193}
194
195// Assign primitive operators
196
197impl<T> AddAssign<T> for Vec1<T> where T: VecItem + Add<Output=T> {
198    fn add_assign(&mut self, other: T) {
199        *self = Vec1 {
200            x: self.x + other,
201            ..Default::default()
202        }
203    }
204}
205
206impl<T> SubAssign<T> for Vec1<T> where T: VecItem + Sub<Output=T> {
207    fn sub_assign(&mut self, other: T) {
208        *self = Vec1 {
209            x: self.x - other,
210            ..Default::default()
211        }
212    }
213}
214
215impl<T> MulAssign<T> for Vec1<T> where T: VecItem + Mul<Output=T> {
216    fn mul_assign(&mut self, other: T) {
217        *self = Vec1 {
218            x: self.x * other,
219            ..Default::default()
220        }
221    }
222}
223
224impl<T> DivAssign<T> for Vec1<T> where T: VecItem + Div<Output=T> {
225    fn div_assign(&mut self, other: T) {
226        *self = Vec1 {
227            x: self.x / other,
228            ..Default::default()
229        }
230    }
231}
232
233// VecNum traits
234
235impl<T> VecNum for Vec1<T> where T: VecItem + Num {
236    fn sum(&self) -> Self::Item {
237        self.x
238    }
239
240    fn product(&self) -> Self::Item {
241        self.x
242    }
243}
244
245// VecInt traits
246
247impl<T> VecInt for Vec1<T> where T: VecItem + Integer {
248    fn div_euc(&self, other: Self) -> Self {
249        Self {
250            x: self.x.div_floor(&other.x),
251        }
252    }
253}
254
255// VecSigned traits
256
257impl<T> VecSigned for Vec1<T> where T: VecItem + Signed {
258    fn snake_length(&self) -> Self::Item {
259        self.x.abs()
260    }
261}
262
263// VecFloat traits
264
265impl<T> VecFloat for Vec1<T> where T: VecItem + Float {
266    fn length(&self) -> Self::Item {
267        self.x
268    }
269
270    fn norm(&self) -> Self {
271        Vec1::new(
272            self.x,
273        )
274    }
275
276    fn floor(&self) -> Self {
277        self.map(|e| e.floor())
278    }
279
280    fn ceil(&self) -> Self {
281        self.map(|e| e.ceil())
282    }
283
284    fn round(&self) -> Self {
285        self.map(|e| e.round())
286    }
287}