about summary refs log tree commit diff
path: root/library/alloc
diff options
context:
space:
mode:
authorDániel Buga <bugadani@gmail.com>2021-01-09 10:22:06 +0100
committerDániel Buga <bugadani@gmail.com>2021-01-14 19:31:56 +0100
commit744f885e2a505136203e3e0eef51f72dbe78b511 (patch)
tree10f01a6bc9609f989cd879d1d0c8ef2418241526 /library/alloc
parentc87ef0a2fcedaa865ff7713953824d0ea9734720 (diff)
downloadrust-744f885e2a505136203e3e0eef51f72dbe78b511.tar.gz
rust-744f885e2a505136203e3e0eef51f72dbe78b511.zip
Remove unreachable panics from VecDeque
Diffstat (limited to 'library/alloc')
-rw-r--r--library/alloc/src/collections/vec_deque/mod.rs9
1 files changed, 4 insertions, 5 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs
index f8fad6de1a3..5b61e8911a5 100644
--- a/library/alloc/src/collections/vec_deque/mod.rs
+++ b/library/alloc/src/collections/vec_deque/mod.rs
@@ -1292,7 +1292,7 @@ impl<T> VecDeque<T> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn front(&self) -> Option<&T> {
-        if !self.is_empty() { Some(&self[0]) } else { None }
+        self.get(0)
     }
 
     /// Provides a mutable reference to the front element, or `None` if the
@@ -1316,7 +1316,7 @@ impl<T> VecDeque<T> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn front_mut(&mut self) -> Option<&mut T> {
-        if !self.is_empty() { Some(&mut self[0]) } else { None }
+        self.get_mut(0)
     }
 
     /// Provides a reference to the back element, or `None` if the `VecDeque` is
@@ -1336,7 +1336,7 @@ impl<T> VecDeque<T> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn back(&self) -> Option<&T> {
-        if !self.is_empty() { Some(&self[self.len() - 1]) } else { None }
+        self.get(self.len().wrapping_sub(1))
     }
 
     /// Provides a mutable reference to the back element, or `None` if the
@@ -1360,8 +1360,7 @@ impl<T> VecDeque<T> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn back_mut(&mut self) -> Option<&mut T> {
-        let len = self.len();
-        if !self.is_empty() { Some(&mut self[len - 1]) } else { None }
+        self.get_mut(self.len().wrapping_sub(1))
     }
 
     /// Removes the first element and returns it, or `None` if the `VecDeque` is