Skip to content

Commit e269c85

Browse files
authored
Update mod.rs
1 parent 0328b2a commit e269c85

File tree

1 file changed

+13
-16
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+13
-16
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
20512051
///
20522052
/// # Panics
20532053
///
2054-
/// Panics if `index` is strictly greater than deque's length
2054+
/// Panics if `index` is strictly greater than the deque's length.
20552055
///
20562056
/// # Examples
20572057
///
@@ -2073,7 +2073,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
20732073
#[stable(feature = "deque_extras_15", since = "1.5.0")]
20742074
#[track_caller]
20752075
pub fn insert(&mut self, index: usize, value: T) {
2076-
assert!(self.insert_mut(index, value).is_some(), "index out of bounds");
2076+
let _ = self.insert_mut(index, value);
20772077
}
20782078

20792079
/// Inserts an element at `index` within the deque, shifting all elements
@@ -2082,7 +2082,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
20822082
///
20832083
/// Element at index 0 is the front of the queue.
20842084
///
2085-
/// Returns [`None`] if `index` is strictly greater than deque's length.
2085+
/// # Panics
2086+
///
2087+
/// Panics if `index` is strictly greater than the deque's length.
20862088
///
20872089
/// # Examples
20882090
///
@@ -2092,20 +2094,17 @@ impl<T, A: Allocator> VecDeque<T, A> {
20922094
///
20932095
/// let mut vec_deque = VecDeque::from([1, 2, 3]);
20942096
///
2095-
/// let x = vec_deque.insert_mut(1, 5).unwrap();
2097+
/// let x = vec_deque.insert_mut(1, 5);
20962098
/// *x += 7;
20972099
/// assert_eq!(vec_deque, &[1, 12, 2, 3]);
2098-
///
2099-
/// let y = vec_deque.insert_mut(7, 5);
2100-
/// assert!(y.is_none());
21012100
/// ```
21022101
#[unstable(feature = "push_mut", issue = "135974")]
21032102
#[track_caller]
2104-
#[must_use = "if you don't need a reference to the value or type-safe bound checking, use VecDeque::insert instead"]
2105-
pub fn insert_mut(&mut self, index: usize, value: T) -> Option<&mut T> {
2106-
if index > self.len() {
2107-
return None;
2108-
}
2103+
#[must_use = "if you don't need a reference to the value, use VecDeque::insert instead"]
2104+
pub fn insert_mut(&mut self, index: usize, value: T) -> &mut T {
2105+
if intrinsics::unlikely(index > self.len) {
2106+
panic!("insertion index (is {index}) should be <= len (is {})", self.len())
2107+
};
21092108
if self.is_full() {
21102109
self.grow();
21112110
}
@@ -2119,17 +2118,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
21192118
// see `remove()` for explanation why this wrap_copy() call is safe.
21202119
self.wrap_copy(self.to_physical_idx(index), self.to_physical_idx(index + 1), k);
21212120
self.len += 1;
2122-
let ptr = self.buffer_write(self.to_physical_idx(index), value);
2123-
Some(ptr)
2121+
self.buffer_write(self.to_physical_idx(index), value)
21242122
}
21252123
} else {
21262124
let old_head = self.head;
21272125
self.head = self.wrap_sub(self.head, 1);
21282126
unsafe {
21292127
self.wrap_copy(old_head, self.head, index);
21302128
self.len += 1;
2131-
let ptr = self.buffer_write(self.to_physical_idx(index), value);
2132-
Some(ptr)
2129+
self.buffer_write(self.to_physical_idx(index), value)
21332130
}
21342131
}
21352132
}

0 commit comments

Comments
 (0)