devela/num/geom/linear/vector/array/
impl_traits.rs

1// devela::num::geom::linear::vector::array:impl_traits
2//
3//!
4//
5
6use crate::{array_init, ConstDefault, Hash, Hasher, Vector, _core::fmt};
7
8/* Clone, Copy */
9
10// T:Clone
11impl<T: Clone, const D: usize> Clone for Vector<T, D> {
12    fn clone(&self) -> Self {
13        Self { coords: self.coords.clone() }
14    }
15}
16
17// T:Copy
18impl<T: Copy, const D: usize> Copy for Vector<T, D> {}
19
20/* Default, ConstDefault */
21
22impl<T: Default, const D: usize> Default for Vector<T, D> {
23    /// Returns a `Vector`, allocated in the stack,
24    /// using the default value to fill the data.
25    fn default() -> Self {
26        let coords = array_init![default [T; D], "safe_num", "unsafe_array"];
27        Self { coords }
28    }
29}
30
31// S:Bare + T:ConstDefault
32impl<T: ConstDefault, const D: usize> ConstDefault for Vector<T, D> {
33    /// Returns a Vector, allocated in the stack,
34    /// using the default value to fill the data.
35    const DEFAULT: Self = {
36        let coords = array_init![const_default [T; D]];
37        Self { coords }
38    };
39}
40
41// T:Debug
42impl<T: fmt::Debug, const D: usize> fmt::Debug for Vector<T, D> {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        f.debug_struct("Vector").field("D", &D).field("coords", &self.coords).finish()
45    }
46}
47
48// T:PartialEq
49impl<T: PartialEq, const D: usize> PartialEq for Vector<T, D> {
50    fn eq(&self, other: &Self) -> bool {
51        self.coords == other.coords
52    }
53}
54// T:Eq
55impl<T: Eq, const D: usize> Eq for Vector<T, D> {}
56
57impl<T: Hash, const D: usize> Hash for Vector<T, D> {
58    fn hash<HR: Hasher>(&self, state: &mut HR) {
59        self.coords.hash(state);
60    }
61}