Enable bi-directional iter on `UArray`

next
Sayan Nandan 2 years ago
parent 86585ff864
commit 89888483e2
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -251,4 +251,24 @@ mod uarray {
let b = a.clone();
assert_eq!(a, b);
}
#[test]
fn into_iter_rev() {
let a: UArray<CAP, String> = (0u8..8).into_iter().map(|v| v.to_string()).collect();
let r: Vec<String> = a.into_iter().rev().collect();
(0..8)
.rev()
.into_iter()
.zip(r.into_iter())
.for_each(|(x, y)| assert_eq!(x.to_string(), y));
}
#[test]
fn into_iter_rev_partial() {
let a: UArray<CAP, String> = (0u8..8).into_iter().map(|v| v.to_string()).collect();
let r: Vec<String> = a.into_iter().rev().take(4).collect();
(4..8)
.rev()
.into_iter()
.zip(r.into_iter())
.for_each(|(x, y)| assert_eq!(x.to_string(), y));
}
}

@ -181,6 +181,7 @@ pub struct IntoIter<const N: usize, T> {
}
impl<const N: usize, T> IntoIter<N, T> {
#[inline(always)]
fn _next(&mut self) -> Option<T> {
if self.i == self.l {
return None;
@ -194,6 +195,17 @@ impl<const N: usize, T> IntoIter<N, T> {
Some(ret)
}
}
#[inline(always)]
fn _next_back(&mut self) -> Option<T> {
if self.i == self.l {
return None;
}
unsafe {
self.l -= 1;
// UNSAFE(@ohsayan): we always ensure EOA condition
Some(ptr::read(self.d.a.as_ptr().add(self.l).cast()))
}
}
}
impl<const N: usize, T> Drop for IntoIter<N, T> {
@ -218,6 +230,11 @@ impl<const N: usize, T> Iterator for IntoIter<N, T> {
}
impl<const N: usize, T> ExactSizeIterator for IntoIter<N, T> {}
impl<const N: usize, T> FusedIterator for IntoIter<N, T> {}
impl<const N: usize, T> DoubleEndedIterator for IntoIter<N, T> {
fn next_back(&mut self) -> Option<Self::Item> {
self._next_back()
}
}
impl<const N: usize, T> IntoIterator for UArray<N, T> {
type Item = T;

Loading…
Cancel
Save