Skip to main content

IteratorExt

Trait IteratorExt 

Source
pub trait IteratorExt: Iterator + Sized {
    // Provided methods
    fn batching_split_before_with_header<F>(
        self,
        batch_starting: F,
    ) -> BatchesWithHeader<Self::Item, Self, F> 
       where F: FnMut(&Self::Item) -> bool { ... }
    fn batching_split_before_loose<F>(
        self,
        batch_starting: F,
    ) -> Batches<Self::Item, Self, F>
       where F: FnMut(&Self::Item) -> bool { ... }
}
Expand description

Extension trait providing batching_split_before

Provided Methods§

Source

fn batching_split_before_with_header<F>( self, batch_starting: F, ) -> BatchesWithHeader<Self::Item, Self, F>
where F: FnMut(&Self::Item) -> bool,

Splits the input into a header followed by batches started according to a predicate

The input is divided into:

  • A header, containing no batch-starting items
  • Zero or more subsequent batches, each with precisely one batch-starting item

The returned value from batching_split_before_with_header is an iterator, which yields the elements in the header - before the first batch-starting item.

After processing the header, call .subsequent() which will return a Batches, which is a meta-iterator-like-object which yields the subsequent batches.

Each subsequent batch is then returned by calling .next_batch() which yields a separate sub-iterator.

A new batch is recognised for each input item for which batch_starting returns true.

This method is named with_header because it separates out the header, using a typestate pattern, which is convenient for processing the header separately.

(You will want to iterate the first batch by reference, so that the iteration doesn’t consume the BatchesWithHeader, which is what you will need to call .subsequent(). The API insists that you process the batches sequentially: you can only be processing one batch at a time.)

§UNSTABLE

This method is UNSTABLE and not part of the semver guarantees. You’ll only see it if you ran rustdoc with --document-private-items.

§Example
use itertools::Itertools as _;
use tor_netdoc::batching_split_before::IteratorExt as _;

let mut batches = (1..10).peekable().batching_split_before_with_header(|v| v % 3 == 0);
assert_eq!(batches.by_ref().collect_vec(), [ 1, 2 ]);

let mut batches = batches.subsequent();
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 3, 4, 5 ]);
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 6, 7, 8 ]);
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 9 ]);
assert!(batches.next_batch().is_none());
Source

fn batching_split_before_loose<F>( self, batch_starting: F, ) -> Batches<Self::Item, Self, F>
where F: FnMut(&Self::Item) -> bool,

Splits the input into batches, with new batches started according to a predicate

The input is divided into batches, just before each batch-starting item. The batch-starting item is included as the first item of every batch, except the first batch if the input starts with a non-batch-starting-item.

If the input iterator is empty, there are no batches.

This method is named loose because it neither insists that the iterator start with a batch-starting item, nor returns batches which always start with a batch-starting item. It is up to the caller to handle a possible first batch with no batch-starting item.

Each batch is returned by calling .next_batch() which yields a separate sub-iterator.

A new batch is recognised for each input item for which batch_start returns true.

(The API insists that you process the batches sequentially: you can only be processing one batch at a time.)

§UNSTABLE

This method is UNSTABLE and not part of the semver guarantees. You’ll only see it if you ran rustdoc with --document-private-items.

§Example
use itertools::Itertools as _;
use tor_netdoc::batching_split_before::IteratorExt as _;

let mut batches = (1..10).peekable().batching_split_before_loose(|v| v % 3 == 0);
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 1, 2 ]);
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 3, 4, 5 ]);
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 6, 7, 8 ]);
assert_eq!(batches.next_batch().unwrap().collect_vec(), [ 9 ]);
assert!(batches.next_batch().is_none());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§