@@ -182,11 +182,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
182182 unsafe { ptr:: read ( self . ptr ( ) . add ( off) ) }
183183 }
184184
185- /// Writes an element into the buffer, moving it.
185+ /// Writes an element into the buffer, moving it and returning a pointer to it .
186186 #[ inline]
187- unsafe fn buffer_write ( & mut self , off : usize , value : T ) {
187+ unsafe fn buffer_write ( & mut self , off : usize , value : T ) -> * mut T {
188188 unsafe {
189- ptr:: write ( self . ptr ( ) . add ( off) , value) ;
189+ let ptr = self . ptr ( ) . add ( off) ;
190+ ptr:: write ( ptr, value) ;
191+ ptr
190192 }
191193 }
192194
@@ -1920,10 +1922,66 @@ impl<T, A: Allocator> VecDeque<T, A> {
19201922 self . grow ( ) ;
19211923 }
19221924
1923- unsafe { self . buffer_write ( self . to_physical_idx ( self . len ) , value) }
1925+ unsafe { self . buffer_write ( self . to_physical_idx ( self . len ) , value) } ;
19241926 self . len += 1 ;
19251927 }
19261928
1929+ /// Prepends an element to the deque, returning a reference to it.
1930+ ///
1931+ /// # Examples
1932+ ///
1933+ /// ```
1934+ /// #![feature(push_mut)]
1935+ /// use std::collections::VecDeque;
1936+ ///
1937+ /// let mut d = VecDeque::from([1, 2, 3]);
1938+ /// let x = d.push_front_mut(1);
1939+ /// *x -= 1;
1940+ /// assert_eq!(d.front(), Some(&0));
1941+ /// ```
1942+ #[ unstable( feature = "push_mut" , issue = "135974" ) ]
1943+ #[ track_caller]
1944+ #[ must_use = "if you don't need a reference to the value, use VecDeque::push_front instead" ]
1945+ pub fn push_front_mut ( & mut self , value : T ) -> & mut T {
1946+ if self . is_full ( ) {
1947+ self . grow ( ) ;
1948+ }
1949+
1950+ self . head = self . wrap_sub ( self . head , 1 ) ;
1951+ self . len += 1 ;
1952+
1953+ unsafe {
1954+ let ptr = self . buffer_write ( self . head , value) ;
1955+ & mut * ptr
1956+ }
1957+ }
1958+
1959+ /// Appends an element to the back of the deque, returning a reference to it.
1960+ ///
1961+ /// # Examples
1962+ ///
1963+ /// ```
1964+ /// #![feature(push_mut)]
1965+ /// use std::collections::VecDeque;
1966+ ///
1967+ /// let mut d = VecDeque::from([1, 2, 3]);
1968+ /// let x = d.push_back_mut(3);
1969+ /// *x += 1;
1970+ /// assert_eq!(d.back(), Some(&4));
1971+ /// ```
1972+ #[ unstable( feature = "push_mut" , issue = "135974" ) ]
1973+ #[ track_caller]
1974+ #[ must_use = "if you don't need a reference to the value, use VecDeque::push_back instead" ]
1975+ pub fn push_back_mut ( & mut self , value : T ) -> & mut T {
1976+ if self . is_full ( ) {
1977+ self . grow ( ) ;
1978+ }
1979+
1980+ let back_ptr = unsafe { self . buffer_write ( self . to_physical_idx ( self . len ) , value) } ;
1981+ self . len += 1 ;
1982+ unsafe { & mut * back_ptr }
1983+ }
1984+
19271985 #[ inline]
19281986 fn is_contiguous ( & self ) -> bool {
19291987 // Do the calculation like this to avoid overflowing if len + head > usize::MAX
@@ -2056,6 +2114,64 @@ impl<T, A: Allocator> VecDeque<T, A> {
20562114 }
20572115 }
20582116
2117+ /// Inserts an element at `index` within the deque, shifting all elements
2118+ /// with indices greater than or equal to `index` towards the back,
2119+ /// and returning a reference to it.
2120+ ///
2121+ /// Element at index 0 is the front of the queue.
2122+ ///
2123+ /// Returns [`None`] if `index` is strictly greater than deque's length.
2124+ ///
2125+ /// # Examples
2126+ ///
2127+ /// ```
2128+ /// #![feature(push_mut)]
2129+ /// use std::collections::VecDeque;
2130+ ///
2131+ /// let mut vec_deque = VecDeque::from([1, 2, 3]);
2132+ ///
2133+ /// let x = vec_deque.insert_mut(1, 5).unwrap();
2134+ /// *x += 7;
2135+ /// assert_eq!(vec_deque, &[1, 12, 2, 3]);
2136+ ///
2137+ /// let y = vec_deque.insert_mut(7, 5);
2138+ /// assert!(y.is_none());
2139+ /// ```
2140+ #[ unstable( feature = "push_mut" , issue = "135974" ) ]
2141+ #[ track_caller]
2142+ #[ must_use = "if you don't need a reference to the value or type-safe bound checking, use VecDeque::insert instead" ]
2143+ pub fn insert_mut ( & mut self , index : usize , value : T ) -> Option < & mut T > {
2144+ if index > self . len ( ) {
2145+ return None ;
2146+ }
2147+ if self . is_full ( ) {
2148+ self . grow ( ) ;
2149+ }
2150+
2151+ let k = self . len - index;
2152+ if k < index {
2153+ // `index + 1` can't overflow, because if index was usize::MAX, then either the
2154+ // assert would've failed, or the deque would've tried to grow past usize::MAX
2155+ // and panicked.
2156+ unsafe {
2157+ // see `remove()` for explanation why this wrap_copy() call is safe.
2158+ self . wrap_copy ( self . to_physical_idx ( index) , self . to_physical_idx ( index + 1 ) , k) ;
2159+ let ptr = self . buffer_write ( self . to_physical_idx ( index) , value) ;
2160+ self . len += 1 ;
2161+ Some ( & mut * ptr)
2162+ }
2163+ } else {
2164+ let old_head = self . head ;
2165+ self . head = self . wrap_sub ( self . head , 1 ) ;
2166+ unsafe {
2167+ self . wrap_copy ( old_head, self . head , index) ;
2168+ let ptr = self . buffer_write ( self . to_physical_idx ( index) , value) ;
2169+ self . len += 1 ;
2170+ Some ( & mut * ptr)
2171+ }
2172+ }
2173+ }
2174+
20592175 /// Removes and returns the element at `index` from the deque.
20602176 /// Whichever end is closer to the removal point will be moved to make
20612177 /// room, and all the affected elements will be moved to new positions.
0 commit comments