//! A bidirectional iterator that only checks its direction once. use core::iter::FusedIterator; /** An iterator that conditionally reverses itself upon creation. This acts as a conditional `.rev()` adapter: it reverses the direction of iteration, swapping `.next()` and `.next_back()`, but only if the provided condition is true. If the condition is false, then iteration proceeds normally. The condition is only evaluated when the adapter is constructed, and all calls to drive the iterator are branchless. ## Usage This can be constructed directly with `Bidi::new(some_iterator)`, but it is more conveniently accessed as an extension method on double-ended iterators. Import `wyz::BidiIterator` or `wyz::bidi::*;` and then call `.bidi()` in your iterator adapter sequence. ## Examples This can be used to hand-roll a `memmove` implementation that correctly handles the case where the destination begins in the source region: ```rust use wyz::bidi::*; unsafe fn memmove(from: *const T, to: *mut T, count: usize) { let src = from .. from.add(count); let rev = src.contains(&(to as *const T)); for idx in (0 .. count).bidi(rev) { to.add(idx).write(from.add(idx).read()); } } ``` This example detects if `to` is between `from` and `from.add(count)` and uses that to determine whether to iterate forward from `0` to `count - 1` or backward from `count - 1` to `0`. **/ pub struct Bidi where I: DoubleEndedIterator { /// The iterator being governed. inner: I, /// A pointer to either `I::next` or `I::next_back`. next: fn(&mut I) -> Option<::Item>, /// A pointer to either `I::next_back` or `I::next`. next_back: fn(&mut I) -> Option<::Item>, /// A pointer to either `I::nth` or `I::nth_back`. nth: fn(&mut I, usize) -> Option<::Item>, /// A pointer to either `I::nth_back` or `I::nth`. nth_back: fn(&mut I, usize) -> Option<::Item>, } impl Bidi where I: DoubleEndedIterator { /// Applies the `Bidi` adapter to a double-ended iterator and selects the /// direction of traversal. /// /// ## Parameters /// /// - `iter`: anything that can be made into a double-ended iterator /// - `cond`: determines whether iteration proceeds ordinarily or reversed pub fn new(iter: II, cond: bool) -> Self where II: IntoIterator { let inner = iter.into_iter(); if cond { Self { inner, next: ::next_back, next_back: ::next, nth: ::nth_back, nth_back: ::nth, } } else { Self { inner, next: ::next, next_back: ::next_back, nth: ::nth, nth_back: ::nth_back, } } } } impl Iterator for Bidi where I: DoubleEndedIterator { type Item = ::Item; #[inline] fn next(&mut self) -> Option { (&mut self.next)(&mut self.inner) } #[inline] fn nth(&mut self, n: usize) -> Option { (&mut self.nth)(&mut self.inner, n) } #[inline] #[cfg(not(tarpaulin_include))] fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } #[inline] #[cfg(not(tarpaulin_include))] fn count(self) -> usize { self.inner.count() } #[inline] #[cfg(not(tarpaulin_include))] fn last(mut self) -> Option { self.next_back() } } impl DoubleEndedIterator for Bidi where I: DoubleEndedIterator { #[inline] fn next_back(&mut self) -> Option { (&mut self.next_back)(&mut self.inner) } #[inline] fn nth_back(&mut self, n: usize) -> Option { (&mut self.nth_back)(&mut self.inner, n) } } impl ExactSizeIterator for Bidi where I: DoubleEndedIterator + ExactSizeIterator { #[inline] #[cfg(not(tarpaulin_include))] fn len(&self) -> usize { self.inner.len() } } impl FusedIterator for Bidi where I: DoubleEndedIterator + FusedIterator { } /// Extension trait that provides `.bidi()` for all double-ended iterators. pub trait BidiIterator where Self: Sized + IntoIterator, ::IntoIter: DoubleEndedIterator, { /// Conditionally reverses the direction of iteration. /// /// When `cond` is true, this adapter swaps the `next` and `nth` methods /// with `next_back` and `nth_back`. The resulting iterator is equivalent to /// `if cond { self.rev() } else { self }`. /// /// ## Examples /// /// ```rust /// use wyz::BidiIterator; /// /// let data = [1, 2, 3]; /// let mut iter = data.iter().copied().bidi(false); /// assert_eq!(iter.next(), Some(1)); /// assert_eq!(iter.next_back(), Some(3)); /// /// let mut iter = data.iter().copied().bidi(true); /// assert_eq!(iter.next(), Some(3)); /// assert_eq!(iter.next_back(), Some(1)); /// ``` fn bidi(self, cond: bool) -> Bidi { Bidi::new(self, cond) } } impl BidiIterator for I where I: Sized + IntoIterator, ::IntoIter: DoubleEndedIterator, { } #[cfg(test)] mod tests { use super::*; #[test] fn forward() { let mut iter = (0 .. 6).bidi(false); assert_eq!(iter.next(), Some(0)); assert_eq!(iter.next_back(), Some(5)); assert_eq!(iter.nth(1), Some(2)); assert_eq!(iter.nth_back(1), Some(3)); assert!(iter.next().is_none()); } #[test] fn reverse() { let mut iter = (0 .. 6).bidi(true); assert_eq!(iter.next(), Some(5)); assert_eq!(iter.next_back(), Some(0)); assert_eq!(iter.nth(1), Some(3)); assert_eq!(iter.nth_back(1), Some(2)); assert!(iter.next().is_none()); } }