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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
//! A module about advanced memory sharing during iteration /// Iterates twice over the same collection /// /// # Example /// The following code /// /// ``` /// use iterators_collection::share::DoubleIterator; /// /// let mut array = [1, 2, 3, 4, 5]; /// let iter = DoubleIterator::new(&mut array); /// /// for (i, j) in iter { /// // Some code here /// } /// ``` /// /// Means the same as /// /// ``` /// let array = [1, 2, 3, 4, 5]; /// for i in array.iter() { /// for j in array.iter() { /// // Some code here /// } /// } /// ``` /// /// with some differences: /// - i and j will never be the same with `DoubleIterator` /// /// - you can safely iterate on a mutable slice with `DoubleIterator` /// /// - i and j CANNOT be shared across threads because it is unsafe to increment the iterator in one thread while accessing one of these references from the other one. It may lead to a data race /// /// - i and j are raw pointers and not references because the correct lifetime for the borrowed values is not known at compile time since a simple call to the `next` method may lead to a data race because two mutable references to the same object may exist /// /// Since version 0.3.0, the preferred way to do it is to use the `safe_for_each` method because you can use this iterator without writting unsafe code /// ``` /// use iterators_collection::share::DoubleIterator; /// /// let mut array = [1, 2, 3, 4, 5]; /// let iter = DoubleIterator::new(&mut array); /// /// iter.safe_for_each(|i, j| { /// // Some code here /// }); /// ``` pub struct DoubleIterator<'a, T> { slice: &'a mut [T], first: usize, second: usize, } impl<'a, T> DoubleIterator<'a, T> { /// Creates a `DoubleIterator` from a slice /// /// # Panics /// Panics if `slice.len() < 2` pub fn new(slice: &'a mut [T]) -> Self { assert!(slice.len() >= 2); Self { slice, first: 0, second: 1, } } /// Returns a mutable pointer to the `index`th element of the borrowed slice /// /// # Unsafety /// Indexes are not checked if the `debug_assert!`s are disabled /// /// This pointer is unsafe to use unsafe fn nth_ptr(&mut self, index: usize) -> *mut T { debug_assert!(index < self.slice.len()); self.slice.get_unchecked_mut(index) as *mut T } /// Increments the indexes `first` and `second` or returns Err fn increment(&mut self) -> Result<(), ()> { loop { // Increment self.second += 1; // Check for overflow if self.second == self.slice.len() { self.second = 0; self.first += 1; if self.first >= self.slice.len() { return Err(()); } } if self.first != self.second { return Ok(()); } } } /// Runs the given closure in a safe context /// /// # Example /// ``` /// use iterators_collection::share::DoubleIterator; /// /// let mut array = [1, 2, 3, 4, 5]; /// let iter = DoubleIterator::new(&mut array); /// /// iter.safe_for_each(|i, j| { /// println!("Got i = {} and j = {}", i, j); /// assert_ne!(i, j); /// }); /// ``` /// /// # Notes /// Not like a legacy iteration using a `for` loop, i and j are references because it's safe to use in this context pub fn safe_for_each<F: Fn(&mut T, &mut T)>(self, callback: F) { for (i, j) in self { unsafe { callback(&mut *i, &mut *j); } } } /// Sets the position of the iterator /// /// # Parameters /// `i` the position of the first pointer of the tuple returned by the `Iterator` trait's implementation /// /// `j` the position of the second one /// /// # Panics /// Panics if either `i` or `j` are out of range (greater or equal to `slice.len()`) /// /// Panics if `i == j` pub fn set(&mut self, i: usize, j: usize) { assert_ne!(i, j); assert!(i < self.slice.len() && j < self.slice.len()); self.first = i; self.second = j; } } impl<T> crate::ResettableIterator for DoubleIterator<'_, T> { fn reset(&mut self) { self.first = 0; self.second = 1; } } impl<T> Iterator for DoubleIterator<'_, T> { type Item = (*mut T, *mut T); fn next(&mut self) -> Option<Self::Item> { if self.first == self.slice.len() { return None; } let returned = Some(unsafe { (self.nth_ptr(self.first), self.nth_ptr(self.second)) }); std::mem::drop(self.increment()); // Dropping is a way to ignore the error which doesn't matter here returned } } /// A `DoubleIterator` iterating on one single "line" (see explanation below) /// /// # Introduction /// The iteration cycle of a `DoubleIterator` can be seen as a matrix with `i` as the value of the line and `j` as the value of the collumn. For example, in an array as `[0, 1, 2, 3, 4]`, a `DoubleIterator` will return the numbered cells and discard the blanks /// ```text /// +---+---+---+---+---+---+ /// | |i=0|i=1|i=2|i=3|i=4| /// +---+---+---+---+---+---+ /// |j=0| | 1 | 2 | 3 | 4 | /// +---+---+---+---+---+---+ /// |j=1| 5 | | 6 | 7 | 8 | /// +---+---+---+---+---+---+ /// |j=2| 9 |10 | |11 |12 | /// +---+---+---+---+---+---+ /// |j=3|13 |14 |15 | |16 | /// +---+---+---+---+---+---+ /// |j=4|17 |18 |19 |20 | | /// +---+---+---+---+---+---+ /// ``` /// /// In this example, the first iterator tuple returned (once the two members dereferenced) is `(0, 1)`, then `(0, 2)`, `(0, 3)`, `(0, 4)`, and at the end of line, `i` is reset and `j` is incrememented, returning `(1, 0)`, `(1, 2)` because there is a blank in the cell (`i=1`; `j=1`), `(1, 3)`... /// /// The blanks are here because there can't be two mutable references on the same object. /// /// But in some cases, you need to iterate only on one single line and not the whole grid, that's why `SingleLineIterator` exists. /// /// # Hey, wait! Why using that iterator and not simply iterating manually? /// Just like `DoubleIterator` does, this iterator returns two raw pointers to a member of the slice to iterate /// /// Since version 0.3.3, the prefered way to do this is to use the `safe_for_each` method pub struct SingleLineIterator<'a, T> { slice: &'a mut [T], index: usize, cur: usize, } impl<'a, T> SingleLineIterator<'a, T> { /// Returns a new `SingleLineIterator` which returns a tuple of a mutable reference to `slice[index]` and to another member of `slice` at each iteration /// /// # Panics /// Panics if `index` is greater or equal to `slice.len()` pub fn new(slice: &'a mut [T], index: usize) -> Self { assert!(index < slice.len()); Self { slice, index, cur: if index == 0 { 1 } else { 0 }, } } /// Runs the given closure in a safe context /// /// # Example /// ``` /// use iterators_collection::share::SingleLineIterator; /// /// let mut array = [1, 2, 3, 4, 5]; /// let iter = SingleLineIterator::new(&mut array, 0); /// /// iter.safe_for_each(|i, j| { /// println!("Got i = {} and j = {}", i, j); /// assert_ne!(i, j); /// }); /// ``` /// /// # Notes /// Not like a legacy iteration using a `for` loop, i and j are references because it's safe to use in this context pub fn safe_for_each<F: Fn(&mut T, &mut T)>(self, callback: F) { for (i, j) in self { unsafe { callback(&mut *i, &mut *j); } } } } impl<T> crate::ResettableIterator for SingleLineIterator<'_, T> { fn reset(&mut self) { self.cur = 0; } } impl<'a, T> Iterator for SingleLineIterator<'a, T> { type Item = (*mut T, *mut T); fn next(&mut self) -> Option<Self::Item> { let returned = if self.cur >= self.slice.len() { None } else { unsafe { let ptr1 = self.slice.get_unchecked_mut(self.index) as *mut T; let ptr2 = self.slice.get_unchecked_mut(self.cur) as *mut T; Some((ptr1, ptr2)) } }; self.cur += 1; if self.cur == self.index { self.cur += 1; } returned } } impl<'a, T> From<DoubleIterator<'a, T>> for SingleLineIterator<'a, T> { fn from(src: DoubleIterator<'a, T>) -> Self { Self { cur: src.second, index: src.first, slice: src.slice, } } } #[cfg(test)] mod tests;