diff --git a/src/array_queue.rs b/src/array_queue.rs index 4aadc8b..f890b4b 100644 --- a/src/array_queue.rs +++ b/src/array_queue.rs @@ -99,7 +99,7 @@ impl ArrayQueue { if self.is_empty() { None } else { - let x = replace(&mut self.array[self.start], MaybeUninit::uninit()); + let x = replace(&mut self.array[self.index(0)], MaybeUninit::uninit()); self.start = self.index(1); self.length -= 1; @@ -113,7 +113,10 @@ impl ArrayQueue { if self.is_empty() { None } else { - let x = replace(&mut self.array[self.length - 1], MaybeUninit::uninit()); + let x = replace( + &mut self.array[self.index(self.length - 1)], + MaybeUninit::uninit(), + ); self.length -= 1; // SAFETY: An element exists at the last index. @@ -594,4 +597,24 @@ mod test { assert_eq!(unsafe { BAR_SUM }, 32); } + + #[test] + fn pop_back_from_middle() { + const LENGTH: usize = 42; + const MIDDLE: usize = 17; + + let mut xs = ArrayQueue::, LENGTH>::new(); + + for x in 0..LENGTH { + assert!(xs.push_back(Box::new(x)).is_ok()); + } + + for _ in 0..MIDDLE { + xs.pop_front(); + } + + for x in LENGTH..MIDDLE { + assert_eq!(xs.pop_back(), Some(Box::new(x - 1))); + } + } }