itertools_wild/lib.rs
1//! Itertools-wild — extra wild iterator adaptors, wild functions and wild macros.
2//!
3//! To use the iterator methods in this crate, import the [`ItertoolsWild` trait](./trait.ItertoolsWild.html):
4//!
5//! ```ignore
6//! use itertools_wild::ItertoolsWild;
7//! ```
8//!
9//! ## Rust Version
10//!
11//! This version of itertools-wild requires wild Rust
12//!
13#![doc(html_root_url="https://docs.rs/itertools-wild/")]
14
15extern crate either;
16extern crate itertools;
17
18pub mod adaptors;
19pub mod special;
20
21use adaptors::{
22 ClampToExactLength,
23 AssertExactSize,
24};
25
26pub trait ItertoolsWild : Iterator {
27 /// Assert that the iterator has an exact size hint; the result
28 /// is an iterator that produces the same sequence as usual but also
29 /// implements `ExactSizeIterator`
30 ///
31 /// Note that this is not anything like `unsafe`, since it's not related
32 /// to memory safety -- only logical consistency.
33 ///
34 /// The `AssertExactSize` adaptor will use **debug assertions** to check
35 /// the iterator's actual length.
36 fn assert_exact_size(self, size: usize) -> AssertExactSize<Self>
37 where Self: Sized,
38 {
39 AssertExactSize {
40 iter: self,
41 size: size,
42 }
43 }
44
45 /// Clamp an iterator to an exact length
46 ///
47 /// The adapted iterator never produces more than the `length` amount
48 /// of elements. If the underlying iterator would end short of that,
49 /// the closure `filler` is called to supply elements up until the
50 /// specific `length`.
51 ///
52 /// This iterator can be trusted to have the exact length, and it is fused.
53 fn clamp_to_exact_length<F>(self, length: usize, filler: F) -> ClampToExactLength<Self, F>
54 where Self: Sized,
55 F: FnMut(usize) -> Self::Item,
56 {
57 ClampToExactLength {
58 iter: self.fuse(),
59 index: 0,
60 size: length,
61 filler: filler,
62 }
63 }
64
65}
66
67
68impl<I> ItertoolsWild for I where I: Iterator { }