Function carry

Source
pub fn carry<I, J, F>(
    high: &mut Peekable<I>,
    low: &mut Peekable<J>,
    factory: F,
) -> bool
where I: Iterator, J: Iterator, F: FnOnce() -> Peekable<J>,
Expand description

Perform “carry” within a pair of peekable iterators.

That is, if low has no more elements, advance high, and replace low with a fresh iterator from the factory function.

Returns whether a carry occurred.

§Example

use exhaust::{Exhaust, iteration::carry};

// Setup
let new_iter = || u8::exhaust().peekable();
let mut high = new_iter();
let mut low = new_iter();
for _ in &mut low {} // advance to the end

// Perform a carry.
assert_eq!([high.peek(), low.peek()], [Some(&0), None]);
let carried = carry(&mut high, &mut low, new_iter);
assert!(carried);
assert_eq!([high.peek(), low.peek()], [Some(&1), Some(&0)]);

// If the low iterator has an element already, the iterators are unchanged.
let carried = carry(&mut high, &mut low, new_iter);
assert!(!carried);
assert_eq!([high.peek(), low.peek()], [Some(&1), Some(&0)]);