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

//! Provides a mutable pointer type that is more restrictive that &mut T, in order
//! to protect tree invariants.
//! PMut is short for protected mutable reference.
//!
//! It prevents the user from violating the invariants of the tree.
//!
//! ```rust
//! extern crate dinotree_alg;
//! extern crate axgeom;
//! use dinotree_alg::prelude::*;
//!
//!
//! let mut bots=[BBox::new(axgeom::Rect::new(0,10,0,10),0)];
//! let mut tree=DinoTree::new(&mut bots);
//! 
//! tree.find_collisions_mut(|mut a,mut b|{
//!    //We cannot allow the user to swap these two
//!    //bots. They should be allowed to mutate 
//!    //whats inside each of them (aside from their aabb),
//!    //but not swap.
//!    
//!    //core::mem::swap(a,b); // We cannot allow this!!!!
//!
//!    //This is allowed.  
//!    core::mem::swap(a.inner_mut(),b.inner_mut());
//! })
//!
//! ```

use crate::inner_prelude::*;


///A protected mutable reference. 
///See the pmut module documentation for more explanation.
pub struct PMut<'a,T:?Sized>{
    inner:&'a mut T
}
impl<'a,T:?Sized> PMut<'a,T>{
    pub fn new(inner:&'a mut T)->PMut<'a,T>{
        PMut{inner}
    }
    pub fn as_mut(&mut self)->PMut<T>{
        PMut{inner:self.inner}
    }

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


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:HasInner> HasInner for PMut<'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>  PMut<'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<(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(self)->core::slice::Iter<'a,T>{
        self.inner.iter()
    }
    #[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()
    }
}


///Iterator produced by `PMut<[T]>` that generates `PMut<T>`
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})
    }
}