2 use core::mem::replace;
4 use crate::alloc::Allocator;
8 /// PairSlices pairs up equal length slice parts of two deques
10 /// For example, given deques "A" and "B" with the following division into slices:
12 /// A: [0 1 2] [3 4 5]
15 /// It produces the following sequence of matching slices:
21 /// and the uneven remainder of either A or B is skipped.
22 pub struct PairSlices<'a, 'b, T> {
29 impl<'a, 'b, T> PairSlices<'a, 'b, T> {
30 pub fn from<A: Allocator>(to: &'a mut VecDeque<T, A>, from: &'b VecDeque<T, A>) -> Self {
31 let (a0, a1) = to.as_mut_slices();
32 let (b0, b1) = from.as_slices();
33 PairSlices { a0, a1, b0, b1 }
36 pub fn has_remainder(&self) -> bool {
40 pub fn remainder(self) -> impl Iterator<Item = &'b [T]> {
41 IntoIterator::into_iter([self.b0, self.b1])
45 impl<'a, 'b, T> Iterator for PairSlices<'a, 'b, T> {
46 type Item = (&'a mut [T], &'b [T]);
47 fn next(&mut self) -> Option<Self::Item> {
48 // Get next part length
49 let part = cmp::min(self.a0.len(), self.b0.len());
53 let (p0, p1) = replace(&mut self.a0, &mut []).split_at_mut(part);
54 let (q0, q1) = self.b0.split_at(part);
56 // Move a1 into a0, if it's empty (and b1, b0 the same way).
59 if self.a0.is_empty() {
60 self.a0 = replace(&mut self.a1, &mut []);
62 if self.b0.is_empty() {
63 self.b0 = replace(&mut self.b1, &[]);