[][src]Struct iterators_collection::share::DoubleIterator

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

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
});

Methods

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

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

Creates a DoubleIterator from a slice

Panics

Panics if slice.len() < 2

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::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 set(&mut self, i: usize, j: usize)[src]

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

Trait Implementations

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

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

impl<'_, T> Iterator for DoubleIterator<'_, T>[src]

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

The type of the elements being iterated over.

Auto Trait Implementations

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

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

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

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

impl<'a, T> RefUnwindSafe for DoubleIterator<'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]