Crate context_iterators

Source
Expand description

Iterators adaptors with associated read-only data.

Useful for naming the types of wrapped iterators by using function pointers or non-capturing closures.

use context_iterators::*;
use std::ops::Range;

type MappedIterator = MapCtx<WithCtx<Range<u16>, u16>, usize>;

let iter: MappedIterator = (0..10)
    .with_context(42)
    .map_with_context(|item: u16, context: &u16| (item + *context) as usize);

assert!(iter.eq(42..52));

The MappedIterator type can be used in contexts where a concrete type is needed, for example as an associated type for a trait.

trait Iterable {
    type Iter: Iterator<Item = usize>;
}

struct MyIterable;

impl Iterable for MyIterable {
   type Iter = MappedIterator;
}

Structs§

CtxMap
Apply a function to the context of an iterator.
FilterCtx
Filter the elements of an iterator.
FilterMapCtx
Map a function over the elements of an iterator, simultaneously filtering elements.
MapCtx
Map a function over each element in the iterator.
WithCtx
Wrapper around an iterator adding context data.

Traits§

ContextIterator
Iterator carrying a context.
IntoContextIterator
Extended iterator trait to allow adding context data.

Type Aliases§

FilterMapWithCtx
Map a function over the elements of an iterator, simultaneously filtering elements. Passes a context to each function call.
FilterWithCtx
Filter the elements of an iterator, passing a context to each function call.
MapWithCtx
Map a function over each element in an iterator, passing a context to each function call.