From e261e7c388bd8744a88f1c37b8900b4998fad851 Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Tue, 22 Dec 2020 11:01:50 +0100 Subject: [PATCH] Remove use of unsafe for CsLaneIterMut --- nalgebra-sparse/src/cs.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index 7a38055b..09320da7 100644 --- a/nalgebra-sparse/src/cs.rs +++ b/nalgebra-sparse/src/cs.rs @@ -3,7 +3,7 @@ use crate::{SparseEntry, SparseEntryMut}; use std::sync::Arc; use std::ops::Range; -use std::ptr::slice_from_raw_parts_mut; +use std::mem::replace; /// An abstract compressed matrix. /// @@ -238,7 +238,7 @@ pub struct CsLaneIterMut<'a, T> { // The index of the lane that will be returned on the next iteration current_lane_idx: usize, pattern: &'a SparsityPattern, - remaining_values: *mut T, + remaining_values: &'a mut [T], } impl<'a, T> CsLaneIterMut<'a, T> { @@ -246,7 +246,7 @@ impl<'a, T> CsLaneIterMut<'a, T> { Self { current_lane_idx: 0, pattern, - remaining_values: values.as_mut_ptr() + remaining_values: values } } } @@ -264,12 +264,9 @@ impl<'a, T> Iterator for CsLaneIterMut<'a, T> if let Some(minor_indices) = lane { let count = minor_indices.len(); - // Note: I can't think of any way to construct this iterator without unsafe. - let values_in_lane; - unsafe { - values_in_lane = &mut *slice_from_raw_parts_mut(self.remaining_values, count); - self.remaining_values = self.remaining_values.add(count); - } + let remaining = replace(&mut self.remaining_values, &mut []); + let (values_in_lane, remaining) = remaining.split_at_mut(count); + self.remaining_values = remaining; self.current_lane_idx += 1; Some(CsLaneMut {