Skip to content

Commit e292ca4

Browse files
committed
deque: Add front, front_mut, back, back_mut.
1 parent a11ed8c commit e292ca4

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/deque.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,44 @@ impl<T, const N: usize> Deque<T, N> {
191191
}
192192
}
193193

194+
/// Provides a reference to the front element, or None if the `Deque` is empty.
195+
pub fn front(&self) -> Option<&T> {
196+
if self.is_empty() {
197+
None
198+
} else {
199+
Some(unsafe { &*self.buffer.get_unchecked(self.front).as_ptr() })
200+
}
201+
}
202+
203+
/// Provides a mutable reference to the front element, or None if the `Deque` is empty.
204+
pub fn front_mut(&mut self) -> Option<&mut T> {
205+
if self.is_empty() {
206+
None
207+
} else {
208+
Some(unsafe { &mut *self.buffer.get_unchecked_mut(self.front).as_mut_ptr() })
209+
}
210+
}
211+
212+
/// Provides a reference to the back element, or None if the `Deque` is empty.
213+
pub fn back(&self) -> Option<&T> {
214+
if self.is_empty() {
215+
None
216+
} else {
217+
let index = Self::decrement(self.back);
218+
Some(unsafe { &*self.buffer.get_unchecked(index).as_ptr() })
219+
}
220+
}
221+
222+
/// Provides a mutable reference to the back element, or None if the `Deque` is empty.
223+
pub fn back_mut(&mut self) -> Option<&mut T> {
224+
if self.is_empty() {
225+
None
226+
} else {
227+
let index = Self::decrement(self.back);
228+
Some(unsafe { &mut *self.buffer.get_unchecked_mut(index).as_mut_ptr() })
229+
}
230+
}
231+
194232
/// Removes the item from the front of the deque and returns it, or `None` if it's empty
195233
pub fn pop_front(&mut self) -> Option<T> {
196234
if self.is_empty() {
@@ -606,6 +644,39 @@ mod tests {
606644
assert!(v.is_empty());
607645
}
608646

647+
#[test]
648+
fn front_back() {
649+
let mut v: Deque<i32, 4> = Deque::new();
650+
assert_eq!(v.front(), None);
651+
assert_eq!(v.front_mut(), None);
652+
assert_eq!(v.back(), None);
653+
assert_eq!(v.back_mut(), None);
654+
655+
v.push_back(4).unwrap();
656+
assert_eq!(v.front(), Some(&4));
657+
assert_eq!(v.front_mut(), Some(&mut 4));
658+
assert_eq!(v.back(), Some(&4));
659+
assert_eq!(v.back_mut(), Some(&mut 4));
660+
661+
v.push_front(3).unwrap();
662+
assert_eq!(v.front(), Some(&3));
663+
assert_eq!(v.front_mut(), Some(&mut 3));
664+
assert_eq!(v.back(), Some(&4));
665+
assert_eq!(v.back_mut(), Some(&mut 4));
666+
667+
v.pop_back().unwrap();
668+
assert_eq!(v.front(), Some(&3));
669+
assert_eq!(v.front_mut(), Some(&mut 3));
670+
assert_eq!(v.back(), Some(&3));
671+
assert_eq!(v.back_mut(), Some(&mut 3));
672+
673+
v.pop_front().unwrap();
674+
assert_eq!(v.front(), None);
675+
assert_eq!(v.front_mut(), None);
676+
assert_eq!(v.back(), None);
677+
assert_eq!(v.back_mut(), None);
678+
}
679+
609680
#[test]
610681
fn iter() {
611682
let mut v: Deque<i32, 4> = Deque::new();

0 commit comments

Comments
 (0)