make mut iterator pointer based

Change the ColumnIterMut back to the original impl and manually implement Send
This commit is contained in:
Geo 2022-10-23 13:40:03 +02:00 committed by Sébastien Crozet
parent 42ab3f6903
commit f6461d3862
1 changed files with 28 additions and 25 deletions

View File

@ -396,18 +396,19 @@ where
/// An iterator through the mutable columns of a matrix. /// An iterator through the mutable columns of a matrix.
#[derive(Debug)] #[derive(Debug)]
pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> { pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> {
mat: &'a mut Matrix<T, R, C, S>, mat: *mut Matrix<T, R, C, S>,
phantom: PhantomData<&'a mut Matrix<T, R, C, S>>,
range: Range<usize>, range: Range<usize>,
} }
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ColumnIterMut<'a, T, R, C, S> { impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ColumnIterMut<'a, T, R, C, S> {
pub(crate) fn new(mat: &'a mut Matrix<T, R, C, S>) -> Self { pub(crate) fn new(mat: &'a mut Matrix<T, R, C, S>) -> Self {
let range = 0..mat.ncols(); let range = 0..mat.ncols();
ColumnIterMut { mat, range } ColumnIterMut { mat, range, phantom: Default::default() }
} }
fn ncols(&self) -> usize { fn ncols(&self) -> usize {
self.mat.ncols() unsafe { (*self.mat).ncols() }
} }
} }
@ -419,8 +420,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator
#[inline] #[inline]
fn next(&'_ mut self) -> Option<Self::Item> { fn next(&'_ mut self) -> Option<Self::Item> {
if self.range.start < self.ncols() { if self.range.start < self.ncols() {
let pmat: *mut _ = self.mat; let res = unsafe { (*self.mat).column_mut(self.range.start) };
let res = unsafe { (*pmat).column_mut(self.range.start) };
self.range.start += 1; self.range.start += 1;
Some(res) Some(res)
} else { } else {
@ -456,10 +456,9 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> DoubleEndedI
debug_assert!(self.range.start <= self.range.end); debug_assert!(self.range.start <= self.range.end);
if !self.range.is_empty() { if !self.range.is_empty() {
self.range.end -= 1; self.range.end -= 1;
debug_assert!(self.range.end < self.mat.ncols()); debug_assert!(self.range.end < self.ncols());
debug_assert!(self.range.end >= self.range.start); debug_assert!(self.range.end >= self.range.start);
let pmat: *mut _ = self.mat; Some(unsafe { (*self.mat).column_mut(self.range.end) })
Some(unsafe { (*pmat).column_mut(self.range.end) })
} else { } else {
None None
} }
@ -482,17 +481,21 @@ where
fn split_at(self, index: usize) -> (Self, Self) { fn split_at(self, index: usize) -> (Self, Self) {
// the index is relative to the size of this current iterator // the index is relative to the size of this current iterator
// it will always start at zero so it serves as an offset // it will always start at zero so it serves as an offset
let pmat: *mut _ = self.mat;
let left = Self { let left = Self {
mat: unsafe { &mut *pmat }, mat: self.mat,
range: self.range.start..(self.range.start + index), range: self.range.start..(self.range.start + index),
phantom: Default::default(),
}; };
let right = Self { let right = Self {
mat: self.mat, mat: self.mat,
range: (self.range.start + index)..self.range.end, range: (self.range.start + index)..self.range.end,
phantom: Default::default(),
}; };
(left, right) (left, right)
} }
} }
unsafe impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Send
for ColumnIterMut<'a, T, R, C, S> {}