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
use crate::inner_prelude::*;


///Forbids the user from swapping two nodes around.
#[repr(transparent)]
pub struct ProtectedNode<'a,T>{
    inner:&'a mut T
}
impl<'a,T:NodeTrait> ProtectedNode<'a,T>{
    pub fn new(inner:&'a mut T)->Self{
        ProtectedNode{inner}
    }

    pub fn get(self)->NodeRef<'a,T::T>{
        self.inner.get()
    }
    pub fn get_mut(self)->NodeRefMut<'a,T::T>{
        self.inner.get_mut()
    }
    pub fn as_ref(&mut self)->ProtectedNode<T>{
        ProtectedNode{inner:self.inner}
    }
}


///Forbids the user from swapping aabb's around.
#[repr(transparent)]
pub struct ProtectedBBox<'a,T>{
    inner:&'a mut T
}


impl<'a,T> ProtectedBBox<'a,T>{
    #[inline(always)]
    pub fn as_mut(&mut self)->ProtectedBBox<T>{
        ProtectedBBox{inner:self.inner}
    }

}





unsafe impl<'a,T:HasAabb> HasAabb for ProtectedBBox<'a,T>{
    type Num=T::Num;
    #[inline(always)]
    fn get(&self)->&Rect<Self::Num>{
        self.inner.get()
    }
}
impl<'a,T:HasInner> HasInner for ProtectedBBox<'a,T>{
    type Inner=T::Inner;
    #[inline(always)]
    fn get_inner(&self)->(&Rect<T::Num>,&Self::Inner){
        self.inner.get_inner()
    }

    #[inline(always)]
    fn get_inner_mut(&mut self)->(&Rect<T::Num>,&mut Self::Inner){
        self.inner.get_inner_mut()
    }
}




impl<'a,T> core::borrow::Borrow<T> for ProtectedBBox<'a,T>{
    #[inline(always)]
    fn borrow(&self)->&T{
        self.inner
    }
}

impl<'a,T> AsRef<T> for ProtectedBBox<'a,T>{
    #[inline(always)]
    fn as_ref(&self)->&T{
        self.inner
    }
}









impl<'a,T> core::borrow::Borrow<[T]> for ProtectedBBoxSlice<'a,T>{
    #[inline(always)]
    fn borrow(&self)->&[T]{
        self.inner
    }
}




impl<'a, T> core::iter::IntoIterator for ProtectedBBoxSlice<'a,T> {
    type Item = ProtectedBBox<'a,T>;
    type IntoIter = ProtectedBBoxIter<'a,T>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<'a,T> AsRef<[T]> for ProtectedBBoxSlice<'a,T>{
    #[inline(always)]
    fn as_ref(&self)->&[T]{
        self.inner
    }
}


///Forbids the user from swapping mutable slices in the nodes around.
#[repr(transparent)]
pub struct ProtectedBBoxSlice<'a,T>{
    inner:&'a mut [T]
}

impl<'a,T> ProtectedBBoxSlice<'a,T>{
    
    #[inline(always)]
    pub fn len(&self)->usize{
        self.inner.len()
    }

    #[inline(always)]
    pub fn is_empty(&self)->bool{
        self.inner.is_empty()
    }

    #[inline(always)]
    pub fn split_first_mut(self)->Option<(ProtectedBBox<'a,T>,ProtectedBBoxSlice<'a,T>)>{
        self.inner.split_first_mut().map(|(first,inner)|(ProtectedBBox{inner:first},ProtectedBBoxSlice::new(inner)))
    }


    #[inline(always)]
    pub fn truncate_to(self,a:core::ops::RangeTo<usize>)->Self{
        ProtectedBBoxSlice{inner:&mut self.inner[a]}
    }
    #[inline(always)]
    pub fn truncate_from(self,a:core::ops::RangeFrom<usize>)->Self{
        ProtectedBBoxSlice{inner:&mut self.inner[a]} 
    }


    #[inline(always)]
    pub fn truncate(self,a:core::ops::Range<usize>)->Self{
        ProtectedBBoxSlice{inner:&mut self.inner[a]}
    }

    #[inline(always)]
    pub fn as_mut(&mut self)->ProtectedBBoxSlice<T>{
        ProtectedBBoxSlice{inner:self.inner}
    }

    #[inline(always)]
    pub fn new(inner:&'a mut [T])->Self{
        ProtectedBBoxSlice{inner}
    }

    #[inline(always)]
    pub fn iter(self)->core::slice::Iter<'a,T>{
        self.inner.iter()
    }
    #[inline(always)]
    pub fn iter_mut(self)->ProtectedBBoxIter<'a,T>{
        ProtectedBBoxIter{inner:self.inner.iter_mut()}
    }
}

///Iterator produced by `ProtectedBBoxSlice<T>` that generates `ProtectedBBox<T>`
pub struct ProtectedBBoxIter<'a,T>{
    inner:core::slice::IterMut<'a,T>
}
impl<'a,T> Iterator for ProtectedBBoxIter<'a,T>{
    type Item=ProtectedBBox<'a,T>;

    #[inline(always)]
    fn next(&mut self)->Option<ProtectedBBox<'a,T>>{
        self.inner.next().map(|inner|ProtectedBBox{inner})
    }

    #[inline(always)]
    fn size_hint(&self)->(usize,Option<usize>){
        self.inner.size_hint()
    }
}

impl<'a,T> core::iter::FusedIterator for ProtectedBBoxIter<'a,T>{}
impl<'a,T> core::iter::ExactSizeIterator for ProtectedBBoxIter<'a,T>{}

impl<'a, T> DoubleEndedIterator for ProtectedBBoxIter<'a, T> {
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back().map(|inner|ProtectedBBox{inner})
    }
}



use alloc::vec::Vec;

//They are always send and sync because the only time the vec is used
//is when it is borrowed for the lifetime.
unsafe impl<T> core::marker::Send for PreVecMut<T> {}
unsafe impl<T> core::marker::Sync for PreVecMut<T> {}

///An vec api to avoid excessive dynamic allocation by reusing a Vec
#[derive(Default)]
pub struct PreVecMut<T> {
    vec:Vec<core::ptr::NonNull<T>>
}

impl<T> PreVecMut<T> {
    
    #[inline(always)]
    pub fn new() -> PreVecMut<T> {

        debug_assert_eq!(core::mem::size_of::<core::ptr::NonNull<T>>() ,core::mem::size_of::<&mut T>() );

        PreVecMut {
            vec:Vec::new()
        }
    }


    ///Clears the vec and returns a mutable reference to a vec.
    #[inline(always)]
    pub fn get_empty_vec_mut<'a,'b:'a>(&'a mut self) -> &'a mut Vec<ProtectedBBox<'b,T>> {
        self.vec.clear();
        let v: &mut Vec<_> = &mut self.vec;
        unsafe{&mut *(v as *mut _ as *mut Vec<_>)}
    }    
}