#zip #iterator #check #length #input #equals #lazy-evaluation

nightly zip_eq

Zip iterator that check that its inputs have the same length

1 unstable release

0.1.0 Mar 21, 2022

#1852 in Algorithms

MIT license

25KB
633 lines

zip-eq

A zip iterator that checks that its inputs have the same lengths, either eagerly at the moment of construction, or lazily.

Examples

Eager check

use zip_eq::ZipEq;
                                         
let a = [1, 2];
let b = [3, 4];
let mut zipped = a.zip_eq_eager(b); // length check happens here
                                         
assert_eq!(zipped.next(), Some((1, 3)));
assert_eq!(zipped.next(), Some((2, 4)));
assert_eq!(zipped.next(), None);

Lazy check

use zip_eq::ZipEq;
                                         
let a = [1, 2];
let b = [3, 4];
let mut zipped = a.zip_eq_lazy(b);
                                         
assert_eq!(zipped.next(), Some((1, 3)));
assert_eq!(zipped.next(), Some((2, 4)));
assert_eq!(zipped.next(), None); // length check happens here

lib.rs:

Zip iterator that check that its inputs have the same length.

Two types of iterators are provided. The first one that checks that the sizes are equal eagerly at the moment it's constructed. This can be checked when the iterators' lengths can be known and trusted to be exact (See core::iter::TrustedLen for more details). This is done using ZipEq::zip_eq_eager.
Or in the case where the user knows for certain that the lengths are equal, the check can be avoided with the unsafe method ZipEq::zip_eq_unchecked.
The second type of iterator is one that checks that the sizes are equal while it's being iterated over. It can be constructed with ZipEq::zip_eq_lazy.

Examples:

use zip_eq::ZipEq;

let a = [1, 2];
let b = [3, 4];
let mut zipped = a.zip_eq_lazy(b);

assert_eq!(zipped.next(), Some((1, 3)));
assert_eq!(zipped.next(), Some((2, 4)));
assert_eq!(zipped.next(), None);
use zip_eq::ZipEq;

let a = [1, 2];
let b = [3, 4];
let mut zipped = a.zip_eq_eager(b);

assert_eq!(zipped.next(), Some((1, 3)));
assert_eq!(zipped.next(), Some((2, 4)));
assert_eq!(zipped.next(), None);
use zip_eq::ZipEq;

let a = [1, 2, 3];
let b = [3, 4];
let mut zipped = a.zip_eq_eager(b); // length equality check happens here.
use zip_eq::ZipEq;

let a = [1, 2, 3];
let b = [3, 4];
let mut zipped = a.zip_eq_lazy(b);

assert_eq!(zipped.next(), Some((1, 3)));
assert_eq!(zipped.next(), Some((2, 4)));
zipped.next(); // length equality check happens here.

No runtime deps