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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use crate::*;
pub unsafe trait HasInner: Aabb {
type Inner;
fn get_inner_mut(&mut self) -> (&Rect<Self::Num>, &mut Self::Inner);
}
#[repr(transparent)]
pub(crate) struct PMutPtr<T: ?Sized> {
_inner: *mut T,
}
unsafe impl<T: ?Sized> Send for PMutPtr<T> {}
unsafe impl<T: ?Sized> Sync for PMutPtr<T> {}
#[repr(transparent)]
#[derive(Debug)]
pub struct PMut<'a, T: ?Sized> {
inner: &'a mut T,
}
pub fn combine_slice<'a, T>(a: PMut<'a, [T]>, b: PMut<'a, [T]>) -> PMut<'a, [T]> {
let alen = a.len();
let blen = b.len();
unsafe {
assert_eq!(
a.inner.as_ptr().add(a.len()),
b.inner.as_ptr(),
"Slices are not continuous"
);
PMut {
inner: core::slice::from_raw_parts_mut(a.inner.as_mut_ptr(), alen + blen),
}
}
}
impl<'a, T: ?Sized> core::ops::Deref for PMut<'a, T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &T {
self.inner
}
}
impl<'a, 'b: 'a, T> PMut<'a, PMut<'b, T>> {
#[inline(always)]
pub fn flatten(self) -> PMut<'a, T> {
PMut::new(self.inner.inner)
}
}
impl<'a, T> PMut<'a, T> {
#[inline(always)]
pub fn into_slice(self) -> PMut<'a, [T]> {
PMut {
inner: core::slice::from_mut(self.inner),
}
}
}
impl<'a, T: ?Sized> PMut<'a, T> {
#[inline(always)]
pub fn new(inner: &'a mut T) -> PMut<'a, T> {
PMut { inner }
}
#[inline(always)]
pub fn shorten<'c>(self) -> PMut<'c, T>
where
'a: 'c,
{
PMut { inner: self.inner }
}
#[inline(always)]
pub fn borrow_mut(&mut self) -> PMut<T> {
PMut { inner: self.inner }
}
#[inline(always)]
pub fn into_ref(self) -> &'a T {
self.inner
}
#[inline(always)]
pub unsafe fn into_inner(self) -> &'a mut T {
self.inner
}
}
pub struct NodeRef<'a, T: Aabb> {
pub div: &'a Option<T::Num>,
pub cont: &'a Range<T::Num>,
pub range: PMut<'a, [T]>,
}
impl<'a, 'b: 'a, T: Aabb> PMut<'a, Node<'b, T>> {
#[inline(always)]
pub fn into_node_ref(self) -> NodeRef<'a, T> {
NodeRef {
div: &self.inner.div,
cont: &self.inner.cont,
range: self.inner.range.borrow_mut(),
}
}
#[inline(always)]
pub fn into_range(self) -> PMut<'a, [T]> {
self.inner.range.borrow_mut()
}
}
impl<'a, T: Aabb> PMut<'a, T> {
#[inline(always)]
pub fn unpack_rect(self) -> &'a Rect<T::Num> {
self.inner.get()
}
}
impl<'a, T: HasInner> PMut<'a, T> {
#[inline(always)]
pub fn unpack(self) -> (&'a Rect<T::Num>, &'a mut T::Inner) {
self.inner.get_inner_mut()
}
#[inline(always)]
pub fn unpack_inner(self) -> &'a mut T::Inner {
self.inner.get_inner_mut().1
}
}
unsafe impl<'a, T: Aabb> Aabb for PMut<'a, T> {
type Num = T::Num;
#[inline(always)]
fn get(&self) -> &Rect<Self::Num> {
self.inner.get()
}
}
impl<'a, T> PMut<'a, [T]> {
#[inline(always)]
pub fn get_index_mut(self, ind: usize) -> PMut<'a, T> {
PMut::new(&mut self.inner[ind])
}
#[inline(always)]
pub fn split_first_mut(self) -> Option<(PMut<'a, T>, PMut<'a, [T]>)> {
self.inner
.split_first_mut()
.map(|(first, inner)| (PMut { inner: first }, PMut { inner }))
}
#[inline(always)]
pub fn truncate_to(self, a: core::ops::RangeTo<usize>) -> Self {
PMut {
inner: &mut self.inner[a],
}
}
#[inline(always)]
pub fn truncate_from(self, a: core::ops::RangeFrom<usize>) -> Self {
PMut {
inner: &mut self.inner[a],
}
}
#[inline(always)]
pub fn truncate(self, a: core::ops::Range<usize>) -> Self {
PMut {
inner: &mut self.inner[a],
}
}
#[inline(always)]
pub fn iter_mut(self) -> PMutIter<'a, T> {
PMutIter {
inner: self.inner.iter_mut(),
}
}
}
impl<'a, T> core::iter::IntoIterator for PMut<'a, [T]> {
type Item = PMut<'a, T>;
type IntoIter = PMutIter<'a, T>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
pub struct PMutIter<'a, T> {
inner: core::slice::IterMut<'a, T>,
}
impl<'a, T> Iterator for PMutIter<'a, T> {
type Item = PMut<'a, T>;
#[inline(always)]
fn next(&mut self) -> Option<PMut<'a, T>> {
self.inner.next().map(|inner| PMut { inner })
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, T> core::iter::FusedIterator for PMutIter<'a, T> {}
impl<'a, T> core::iter::ExactSizeIterator for PMutIter<'a, T> {}
impl<'a, T> DoubleEndedIterator for PMutIter<'a, T> {
#[inline(always)]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(|inner| PMut { inner })
}
}