1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
macro_rules! impl_element2 {
($s:ty, $t:ty) => {
impl $t {
/// Creates with given elements.
#[inline]
#[must_use]
pub const fn new(x: $s, y: $s) -> Self {
Self { x, y }
}
/// Fills all elements with the given values.
#[inline]
#[must_use]
pub const fn fill(val: $s) -> Self {
Self { x: val, y: val }
}
/// Creates with given array.
#[inline]
#[must_use]
pub const fn from_array(arr: [$s; 2]) -> Self {
Self { x: arr[0], y: arr[1] }
}
/// Convert to array.
#[inline]
#[must_use]
pub const fn to_array(self) -> [$s; 2] {
[self.x, self.y]
}
/// Creates with given slice.
///
/// # Panics
/// If the length of the given array is less than the number of elements in the vector,
/// an index out of range error occurs.
///
#[inline]
#[must_use]
pub fn from_slice(slice: &[$s]) -> Self {
Self { x: slice[0], y: slice[1] }
}
/// Creates with given tuple.
#[inline]
#[must_use]
pub const fn from_tuple(tuple: ($s, $s)) -> Self {
Self { x: tuple.0, y: tuple.1 }
}
/// Convert to tuple.
#[inline]
#[must_use]
pub const fn to_tuple(self) -> ($s, $s) {
(self.x, self.y)
}
/// Sets the value of the x element.
#[inline]
#[must_use]
pub fn set_x(mut self, x: $s) -> $t {
self.x = x;
self
}
/// Sets the value of the y element.
#[inline]
#[must_use]
pub fn set_y(mut self, y: $s) -> $t {
self.y = y;
self
}
}
impl From<[$s; 2]> for $t {
#[inline]
fn from(value: [$s; 2]) -> Self {
Self::from_array(value)
}
}
impl Into<[$s; 2]> for $t {
#[inline]
fn into(self) -> [$s; 2] {
self.to_array()
}
}
impl From<($s, $s)> for $t {
#[inline]
fn from(value: ($s, $s)) -> Self {
Self::from_tuple(value)
}
}
impl Into<($s, $s)> for $t {
#[inline]
fn into(self) -> ($s, $s) {
self.to_tuple()
}
}
impl AsRef<[$s; 2]> for $t {
#[inline]
fn as_ref(&self) -> &[$s; 2] {
unsafe { &*(self as *const $t as *const [$s; 2]) }
}
}
impl AsMut<[$s; 2]> for $t {
#[inline]
fn as_mut(&mut self) -> &mut [$s; 2] {
unsafe { &mut *(self as *mut $t as *mut [$s; 2]) }
}
}
impl core::ops::Index<usize> for $t {
type Output = $s;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
match index {
0 => &self.x,
1 => &self.y,
_ => panic!("index out of range!"),
}
}
}
impl core::ops::IndexMut<usize> for $t {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
match index {
0 => &mut self.x,
1 => &mut self.y,
_ => panic!("index out of range!")
}
}
}
impl core::fmt::Debug for $t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple(stringify!($t))
.field(&self.x)
.field(&self.y)
.finish()
}
}
impl core::fmt::Display for $t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{{ {}, {} }}", &self.x, &self.y)
}
}
};
}
pub(crate) use impl_element2;