[][src]Struct iterators_collection::share::SingleLineIterator

pub struct SingleLineIterator<'a, T> { /* fields omitted */ }

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

+---+---+---+---+---+---+
|   |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

Methods

impl<'a, T> SingleLineIterator<'a, T>[src]

pub fn new(slice: &'a mut [T], index: usize) -> Self[src]

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 safe_for_each<F: Fn(&mut T, &mut T)>(self, callback: F)[src]

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

Trait Implementations

impl<'_, T> ResettableIterator for SingleLineIterator<'_, T>[src]

impl<'a, T> From<DoubleIterator<'a, T>> for SingleLineIterator<'a, T>[src]

impl<'a, T> Iterator for SingleLineIterator<'a, T>[src]

type Item = (*mut T, *mut T)

The type of the elements being iterated over.

Auto Trait Implementations

impl<'a, T> Send for SingleLineIterator<'a, T> where
    T: Send

impl<'a, T> Sync for SingleLineIterator<'a, T> where
    T: Sync

impl<'a, T> Unpin for SingleLineIterator<'a, T>

impl<'a, T> !UnwindSafe for SingleLineIterator<'a, T>

impl<'a, T> RefUnwindSafe for SingleLineIterator<'a, T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]