@@ -2051,7 +2051,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2051
2051
///
2052
2052
/// # Panics
2053
2053
///
2054
- /// Panics if `index` is strictly greater than deque's length
2054
+ /// Panics if `index` is strictly greater than the deque's length.
2055
2055
///
2056
2056
/// # Examples
2057
2057
///
@@ -2073,7 +2073,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2073
2073
#[ stable( feature = "deque_extras_15" , since = "1.5.0" ) ]
2074
2074
#[ track_caller]
2075
2075
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) ;
2077
2077
}
2078
2078
2079
2079
/// Inserts an element at `index` within the deque, shifting all elements
@@ -2082,7 +2082,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
2082
2082
///
2083
2083
/// Element at index 0 is the front of the queue.
2084
2084
///
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.
2086
2088
///
2087
2089
/// # Examples
2088
2090
///
@@ -2092,20 +2094,17 @@ impl<T, A: Allocator> VecDeque<T, A> {
2092
2094
///
2093
2095
/// let mut vec_deque = VecDeque::from([1, 2, 3]);
2094
2096
///
2095
- /// let x = vec_deque.insert_mut(1, 5).unwrap() ;
2097
+ /// let x = vec_deque.insert_mut(1, 5);
2096
2098
/// *x += 7;
2097
2099
/// assert_eq!(vec_deque, &[1, 12, 2, 3]);
2098
- ///
2099
- /// let y = vec_deque.insert_mut(7, 5);
2100
- /// assert!(y.is_none());
2101
2100
/// ```
2102
2101
#[ unstable( feature = "push_mut" , issue = "135974" ) ]
2103
2102
#[ 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
+ } ;
2109
2108
if self . is_full ( ) {
2110
2109
self . grow ( ) ;
2111
2110
}
@@ -2119,17 +2118,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
2119
2118
// see `remove()` for explanation why this wrap_copy() call is safe.
2120
2119
self . wrap_copy ( self . to_physical_idx ( index) , self . to_physical_idx ( index + 1 ) , k) ;
2121
2120
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)
2124
2122
}
2125
2123
} else {
2126
2124
let old_head = self . head ;
2127
2125
self . head = self . wrap_sub ( self . head , 1 ) ;
2128
2126
unsafe {
2129
2127
self . wrap_copy ( old_head, self . head , index) ;
2130
2128
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)
2133
2130
}
2134
2131
}
2135
2132
}
0 commit comments