Skip to content

Commit fc5df6f

Browse files
authored
Rollup merge of rust-lang#80834 - bugadani:vecdeque, r=oli-obk
Remove unreachable panics from VecDeque::{front/back}[_mut] `VecDeque`'s `front`, `front_mut`, `back` and `back_mut` methods are implemented in terms of the index operator, which causes these functions to contain [unreachable panic calls](https://rust.godbolt.org/z/MTnq1o). This PR reimplements these methods in terms of `get[_mut]` instead.
2 parents 520206b + 02f7b36 commit fc5df6f

File tree

1 file changed

+4
-5
lines changed
  • alloc/src/collections/vec_deque

1 file changed

+4
-5
lines changed

alloc/src/collections/vec_deque/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ impl<T> VecDeque<T> {
12921292
/// ```
12931293
#[stable(feature = "rust1", since = "1.0.0")]
12941294
pub fn front(&self) -> Option<&T> {
1295-
if !self.is_empty() { Some(&self[0]) } else { None }
1295+
self.get(0)
12961296
}
12971297

12981298
/// Provides a mutable reference to the front element, or `None` if the
@@ -1316,7 +1316,7 @@ impl<T> VecDeque<T> {
13161316
/// ```
13171317
#[stable(feature = "rust1", since = "1.0.0")]
13181318
pub fn front_mut(&mut self) -> Option<&mut T> {
1319-
if !self.is_empty() { Some(&mut self[0]) } else { None }
1319+
self.get_mut(0)
13201320
}
13211321

13221322
/// Provides a reference to the back element, or `None` if the `VecDeque` is
@@ -1336,7 +1336,7 @@ impl<T> VecDeque<T> {
13361336
/// ```
13371337
#[stable(feature = "rust1", since = "1.0.0")]
13381338
pub fn back(&self) -> Option<&T> {
1339-
if !self.is_empty() { Some(&self[self.len() - 1]) } else { None }
1339+
self.get(self.len().wrapping_sub(1))
13401340
}
13411341

13421342
/// Provides a mutable reference to the back element, or `None` if the
@@ -1360,8 +1360,7 @@ impl<T> VecDeque<T> {
13601360
/// ```
13611361
#[stable(feature = "rust1", since = "1.0.0")]
13621362
pub fn back_mut(&mut self) -> Option<&mut T> {
1363-
let len = self.len();
1364-
if !self.is_empty() { Some(&mut self[len - 1]) } else { None }
1363+
self.get_mut(self.len().wrapping_sub(1))
13651364
}
13661365

13671366
/// Removes the first element and returns it, or `None` if the `VecDeque` is

0 commit comments

Comments
 (0)