Skip to content

Commit d974f6c

Browse files
authored
Adds into-iter methods on views (#1510)
These allow the iterators to act on the lifetime of the data, not the lifetime of the view.
1 parent e5a8d23 commit d974f6c

File tree

5 files changed

+112
-22
lines changed

5 files changed

+112
-22
lines changed

src/array_serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::marker::PhantomData;
1818
use crate::imp_prelude::*;
1919

2020
use super::arraytraits::ARRAY_FORMAT_VERSION;
21-
use super::Iter;
21+
use crate::iterators::Iter;
2222
use crate::IntoDimension;
2323

2424
/// Verifies that the version of the deserialized array matches the current

src/arraytraits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ where D: Dimension
357357

358358
fn into_iter(self) -> Self::IntoIter
359359
{
360-
self.into_iter_()
360+
Iter::new(self)
361361
}
362362
}
363363

@@ -369,7 +369,7 @@ where D: Dimension
369369

370370
fn into_iter(self) -> Self::IntoIter
371371
{
372-
self.into_iter_()
372+
IterMut::new(self)
373373
}
374374
}
375375

src/impl_methods.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl<A, D: Dimension> ArrayRef<A, D>
463463
pub fn iter(&self) -> Iter<'_, A, D>
464464
{
465465
// debug_assert!(self.pointer_is_inbounds());
466-
self.view().into_iter_()
466+
self.view().into_iter()
467467
}
468468

469469
/// Return an iterator of mutable references to the elements of the array.
@@ -474,7 +474,7 @@ impl<A, D: Dimension> ArrayRef<A, D>
474474
/// Iterator element type is `&mut A`.
475475
pub fn iter_mut(&mut self) -> IterMut<'_, A, D>
476476
{
477-
self.view_mut().into_iter_()
477+
self.view_mut().into_iter()
478478
}
479479

480480
/// Return an iterator of indexes and references to the elements of the array.
@@ -1290,7 +1290,7 @@ impl<A, D: Dimension> ArrayRef<A, D>
12901290
pub fn outer_iter_mut(&mut self) -> AxisIterMut<'_, A, D::Smaller>
12911291
where D: RemoveAxis
12921292
{
1293-
self.view_mut().into_outer_iter()
1293+
self.view_mut().into_outer_iter_mut()
12941294
}
12951295

12961296
/// Return an iterator that traverses over `axis`

src/impl_views/conversions.rs

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::mem::MaybeUninit;
1313

1414
use crate::imp_prelude::*;
1515

16-
use crate::{Baseiter, ElementsBase, ElementsBaseMut, Iter, IterMut};
16+
use crate::{Baseiter, ElementsBase, ElementsBaseMut};
1717

1818
use crate::dimension::offset_from_low_addr_ptr_to_logical_ptr;
1919
use crate::iter::{self, AxisIter, AxisIterMut};
@@ -213,7 +213,7 @@ where D: Dimension
213213
}
214214
}
215215

216-
/// Private array view methods
216+
/// Methods for iterating over array views.
217217
impl<'a, A, D> ArrayView<'a, A, D>
218218
where D: Dimension
219219
{
@@ -229,21 +229,47 @@ where D: Dimension
229229
ElementsBase::new(self)
230230
}
231231

232-
pub(crate) fn into_iter_(self) -> Iter<'a, A, D>
232+
/// Convert into an outer iterator for this view.
233+
///
234+
/// Unlike [ArrayRef::outer_iter], this methods preserves the lifetime of the data,
235+
/// not the view itself.
236+
pub fn into_outer_iter(self) -> iter::AxisIter<'a, A, D::Smaller>
237+
where D: RemoveAxis
233238
{
234-
Iter::new(self)
239+
AxisIter::new(self, Axis(0))
235240
}
236241

237-
/// Return an outer iterator for this view.
238-
#[doc(hidden)] // not official
239-
#[deprecated(note = "This method will be replaced.")]
240-
pub fn into_outer_iter(self) -> iter::AxisIter<'a, A, D::Smaller>
242+
/// Convert into an indexed iterator.
243+
///
244+
/// Unlike [ArrayRef::indexed_iter], this methods preserves the lifetime of the data,
245+
/// not the view itself.
246+
pub fn into_indexed_iter(self) -> iter::IndexedIter<'a, A, D>
247+
{
248+
iter::IndexedIter::new(self.into_elements_base())
249+
}
250+
251+
/// Convert into an iterator over an `axis`.
252+
///
253+
/// Unlike [ArrayRef::axis_iter], this methods preserves the lifetime of the data,
254+
/// not the view itself.
255+
pub fn into_axis_iter(self, axis: Axis) -> iter::AxisIter<'a, A, D::Smaller>
241256
where D: RemoveAxis
242257
{
243-
AxisIter::new(self, Axis(0))
258+
AxisIter::new(self, axis)
259+
}
260+
261+
/// Convert into an iterator over an `axis` by chunks.
262+
///
263+
/// Unlike [`ArrayRef::axis_chunks_iter`], this methods preserves the lifetime of the data,
264+
/// not the view itself.
265+
pub fn into_axis_chunks_iter(self, axis: Axis, chunk_size: usize) -> iter::AxisChunksIter<'a, A, D>
266+
where D: RemoveAxis
267+
{
268+
iter::AxisChunksIter::new(self, axis, chunk_size)
244269
}
245270
}
246271

272+
/// Methods for iterating over mutable array views.
247273
impl<'a, A, D> ArrayViewMut<'a, A, D>
248274
where D: Dimension
249275
{
@@ -294,17 +320,81 @@ where D: Dimension
294320
}
295321
}
296322

297-
pub(crate) fn into_iter_(self) -> IterMut<'a, A, D>
323+
/// Convert into an outer iterator for this view.
324+
///
325+
/// Unlike [ArrayRef::outer_iter], this methods preserves the lifetime of the data,
326+
/// not the view itself.
327+
pub fn into_outer_iter(self) -> iter::AxisIter<'a, A, D::Smaller>
328+
where D: RemoveAxis
329+
{
330+
AxisIter::new(self.into_view(), Axis(0))
331+
}
332+
333+
/// Convert into an indexed iterator.
334+
///
335+
/// Unlike [ArrayRef::indexed_iter], this methods preserves the lifetime of the data,
336+
/// not the view itself.
337+
pub fn into_indexed_iter(self) -> iter::IndexedIter<'a, A, D>
338+
{
339+
iter::IndexedIter::new(self.into_view().into_elements_base())
340+
}
341+
342+
/// Convert into an iterator over an `axis`.
343+
///
344+
/// Unlike [ArrayRef::axis_iter], this methods preserves the lifetime of the data,
345+
/// not the view itself.
346+
pub fn into_axis_iter(self, axis: Axis) -> iter::AxisIter<'a, A, D::Smaller>
347+
where D: RemoveAxis
348+
{
349+
AxisIter::new(self.into_view(), axis)
350+
}
351+
352+
/// Convert into an iterator over an `axis` by chunks.
353+
///
354+
/// Unlike [`ArrayRef::axis_chunks_iter`], this methods preserves the lifetime of the data,
355+
/// not the view itself.
356+
pub fn into_axis_chunks_iter(self, axis: Axis, chunk_size: usize) -> iter::AxisChunksIter<'a, A, D>
357+
where D: RemoveAxis
298358
{
299-
IterMut::new(self)
359+
iter::AxisChunksIter::new(self.into_view(), axis, chunk_size)
300360
}
301361

302-
/// Return an outer iterator for this view.
303-
#[doc(hidden)] // not official
304-
#[deprecated(note = "This method will be replaced.")]
305-
pub fn into_outer_iter(self) -> iter::AxisIterMut<'a, A, D::Smaller>
362+
/// Convert into an outer iterator for this view.
363+
///
364+
/// Unlike [ArrayRef::outer_iter_mut], this methods preserves the lifetime of the data,
365+
/// not the view itself.
366+
pub fn into_outer_iter_mut(self) -> iter::AxisIterMut<'a, A, D::Smaller>
306367
where D: RemoveAxis
307368
{
308369
AxisIterMut::new(self, Axis(0))
309370
}
371+
372+
/// Convert into an indexed iterator.
373+
///
374+
/// Unlike [ArrayRef::indexed_iter_mut], this methods preserves the lifetime of the data,
375+
/// not the view itself.
376+
pub fn into_indexed_iter_mut(self) -> iter::IndexedIterMut<'a, A, D>
377+
{
378+
iter::IndexedIterMut::new(self.into_elements_base())
379+
}
380+
381+
/// Convert into an iterator over an `axis`.
382+
///
383+
/// Unlike [ArrayRef::axis_iter_mut], this methods preserves the lifetime of the data,
384+
/// not the view itself.
385+
pub fn into_axis_iter_mut(self, axis: Axis) -> iter::AxisIterMut<'a, A, D::Smaller>
386+
where D: RemoveAxis
387+
{
388+
AxisIterMut::new(self, axis)
389+
}
390+
391+
/// Convert into an iterator over an `axis` by chunks.
392+
///
393+
/// Unlike [`ArrayRef::axis_chunks_iter_mut`], this methods preserves the lifetime of the data,
394+
/// not the view itself.
395+
pub fn into_axis_chunks_iter_mut(self, axis: Axis, chunk_size: usize) -> iter::AxisChunksIterMut<'a, A, D>
396+
where D: RemoveAxis
397+
{
398+
iter::AxisChunksIterMut::new(self, axis, chunk_size)
399+
}
310400
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub use crate::order::Order;
150150
pub use crate::slice::{MultiSliceArg, NewAxis, Slice, SliceArg, SliceInfo, SliceInfoElem, SliceNextDim};
151151

152152
use crate::iterators::Baseiter;
153-
use crate::iterators::{ElementsBase, ElementsBaseMut, Iter, IterMut};
153+
use crate::iterators::{ElementsBase, ElementsBaseMut};
154154

155155
pub use crate::arraytraits::AsArray;
156156
pub use crate::linalg_traits::LinalgScalar;

0 commit comments

Comments
 (0)