alloy/core/
aligned_array.rs

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::core::{Align, Alignment};

/// An array that is aligned to a particular boundary.
pub struct AlignedArray<T, const N: usize, const A: usize>(Align<A>, [T; N])
where
  Align<A>: Alignment;

impl<T, const N: usize, const A: usize> Default for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: Default,
{
  fn default() -> Self {
    Self(Align::default(), <[T; N]>::default())
  }
}

impl<T, const N: usize, const A: usize> Copy for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: Copy,
{
}

impl<T, const N: usize, const A: usize> Clone for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: Clone,
{
  fn clone(&self) -> Self {
    Self(self.0, self.1.clone())
  }
}

impl<T, const N: usize, const A: usize> std::fmt::Debug for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: std::fmt::Debug,
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_tuple("AlignedArray").field(&self.1).finish()
  }
}

impl<T, const N: usize, const A: usize> PartialEq for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: PartialEq,
{
  #[inline]
  fn eq(&self, other: &Self) -> bool {
    self.1 == other.1
  }
}

impl<T, const N: usize, const A: usize> Eq for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: Eq,
{
}

impl<T, const N: usize, const A: usize> std::hash::Hash for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: std::hash::Hash,
{
  #[inline]
  fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
    self.1.hash(state)
  }
}

impl<T, const N: usize, const A: usize> PartialOrd for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: PartialOrd,
{
  #[inline]
  fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
    self.1.partial_cmp(&other.1)
  }
}

impl<T, const N: usize, const A: usize> Ord for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
  [T; N]: Ord,
{
  #[inline]
  fn cmp(&self, other: &Self) -> std::cmp::Ordering {
    self.1.cmp(&other.1)
  }
}

impl<T, const N: usize, const A: usize> From<[T; N]> for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  #[inline]
  fn from(elements: [T; N]) -> Self {
    Self(Align::new(), elements)
  }
}

impl<T, const N: usize, const A: usize> AsRef<[T]> for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  #[inline]
  fn as_ref(&self) -> &[T] {
    &self.1
  }
}

impl<T, const N: usize, const A: usize> AsMut<[T]> for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  #[inline]
  fn as_mut(&mut self) -> &mut [T] {
    &mut self.1
  }
}

impl<T, const N: usize, const A: usize> std::ops::Deref for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  type Target = [T; N];

  #[inline]
  fn deref(&self) -> &Self::Target {
    &self.1
  }
}

impl<T, const N: usize, const A: usize> std::ops::DerefMut for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  #[inline]
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.1
  }
}

impl<T, const N: usize, const A: usize> std::ops::Index<usize> for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  type Output = T;

  #[inline]
  fn index(&self, index: usize) -> &Self::Output {
    &self.1[index]
  }
}

impl<T, const N: usize, const A: usize> std::ops::IndexMut<usize> for AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  #[inline]
  fn index_mut(&mut self, index: usize) -> &mut Self::Output {
    &mut self.1[index]
  }
}

impl<T, const N: usize, const A: usize> AlignedArray<T, N, A>
where
  Align<A>: Alignment,
{
  /// Create a new aligned array with the given elements.
  ///
  /// # Parameters
  ///
  /// * `elements` - the elements to store in the array
  pub const fn new(elements: [T; N]) -> Self {
    Self(Align::new(), elements)
  }

  /// Get a pointer to the first element in the array.
  pub const fn as_ptr(&self) -> *const T {
    self.1.as_ptr()
  }

  /// Get a mutable pointer to the first element in the array.
  pub fn as_mut_ptr(&mut self) -> *mut T {
    self.1.as_mut_ptr()
  }

  /// Get the inner array.
  pub fn into_inner(self) -> [T; N] {
    self.1
  }
}