[][src]Struct array_tools::ArrayChunks

pub struct ArrayChunks<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> { /* fields omitted */ }

A consuming iterator over non-overlaping subarrays of equal size.

Consumes array upon creation and yields subarrays "chunks" of requested size.

If array can't be divided evenly into chunks, the last yielded item will be a "stump" - an array of length input_array_length % chunk_size_length, containing what remains at the end.

If array can be divided evenly into "chunks", there will be no "stump".

Because this iterator deals with arrays of constant size, it does not expect chunk size argument, instead it expects four generic parameters:

  • Element type.
  • Consumed array type.
  • Chunk array type.
  • Stump array type.

Element type of consumed, chunk and stump array types must match. Consumed array length could be anything, including zero. Chunk array length must be non-zero. In case input array can't be divided evenly into chunks, stump array length must be consumed_array_length % chunk_array_length. In case input array can be divided evenly, stump array length must be 0.

Does not perform any cloning operations, only moves values.

Examples

Case without "stump".

use array_tools::{ArrayChunk, ArrayChunks};
use core::marker::PhantomData;

let array = [1u64, 2, 3, 4, 5, 6, 7, 8];

// Divide array `[u64; 8]` into `[u64; 2]` chunks. It can be divided evenly,
// so there will be no stump.
let mut chunks: ArrayChunks<u64, [u64; 8], [u64; 2], [u64; 0]> = ArrayChunks::new(array);

assert_eq!(chunks.next(), Some(ArrayChunk::Chunk([1u64, 2], PhantomData)));
assert_eq!(chunks.next(), Some(ArrayChunk::Chunk([3u64, 4], PhantomData)));
assert_eq!(chunks.next(), Some(ArrayChunk::Chunk([5u64, 6], PhantomData)));
assert_eq!(chunks.next(), Some(ArrayChunk::Chunk([7u64, 8], PhantomData)));
assert_eq!(chunks.next(), None);

Case with "stump".

use array_tools::{ArrayChunk, ArrayChunks};
use core::marker::PhantomData;

let array = [1u64, 2, 3, 4, 5, 6, 7, 8];

// Divide array `[u64; 8]` into `[u64; 3]` chunks. It can't be divided evenly, so last item
// will be stump of type `[u64; 2]`.
let mut chunks: ArrayChunks<u64, [u64; 8], [u64; 3], [u64; 2]> = ArrayChunks::new(array);

assert_eq!(chunks.next(), Some(ArrayChunk::Chunk([1u64, 2, 3], PhantomData)));
assert_eq!(chunks.next(), Some(ArrayChunk::Chunk([4u64, 5, 6], PhantomData)));
assert_eq!(chunks.next(), Some(ArrayChunk::Stump([7u64, 8], PhantomData)));
assert_eq!(chunks.next(), None);

Actually, most generic parameters may be ommited:

use array_tools::ArrayChunks;

let array = [1, 2, 3, 4, 5, 6, 7, 8];
let _chunks: ArrayChunks<_, _, [_; 3], [_; 2]> = ArrayChunks::new(array);

Methods

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> ArrayChunks<T, A, CHUNK, STUMP>[src]

Important traits for ArrayChunks<T, A, CHUNK, STUMP>
pub fn new(array: A) -> ArrayChunks<T, A, CHUNK, STUMP>[src]

Creates a new instance of ArrayChunks iterator.

Panics

  • If chunk size is 0.
  • If stump size is not equal to consumed_array_length % chunk_array_length.

Note

Though currently this function panics if chunk size is 0 and/or stump size is not valid, this behavior will be changed to perform these checks at compile time, when this will become possible.

Trait Implementations

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> PartialEq<ArrayChunks<T, A, CHUNK, STUMP>> for ArrayChunks<T, A, CHUNK, STUMP> where
    T: PartialEq
[src]

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> Eq for ArrayChunks<T, A, CHUNK, STUMP> where
    T: Eq
[src]

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> Debug for ArrayChunks<T, A, CHUNK, STUMP> where
    T: Debug
[src]

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> Iterator for ArrayChunks<T, A, CHUNK, STUMP>[src]

type Item = ArrayChunk<T, CHUNK, STUMP>

The type of the elements being iterated over.

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> DoubleEndedIterator for ArrayChunks<T, A, CHUNK, STUMP>[src]

impl<T, A: FixedSizeArray<T>, CHUNK: FixedSizeArray<T>, STUMP: FixedSizeArray<T>> Clone for ArrayChunks<T, A, CHUNK, STUMP> where
    T: Clone
[src]

Auto Trait Implementations

impl<T, A, CHUNK, STUMP> Unpin for ArrayChunks<T, A, CHUNK, STUMP> where
    A: Unpin,
    CHUNK: Unpin,
    STUMP: Unpin,
    T: Unpin

impl<T, A, CHUNK, STUMP> Send for ArrayChunks<T, A, CHUNK, STUMP> where
    A: Send,
    CHUNK: Send,
    STUMP: Send,
    T: Send

impl<T, A, CHUNK, STUMP> Sync for ArrayChunks<T, A, CHUNK, STUMP> where
    A: Sync,
    CHUNK: Sync,
    STUMP: Sync,
    T: Sync

Blanket Implementations

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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