@@ -183,8 +183,11 @@ impl<T, A: Allocator> VecDeque<T, A> {
183
183
}
184
184
185
185
/// Writes an element into the buffer, moving it and returning a pointer to it.
186
+ /// # Safety
187
+ ///
188
+ /// May only be called if `off < self.capacity()`.
186
189
#[ inline]
187
- unsafe fn buffer_write ( & mut self , off : usize , value : T ) -> * mut T {
190
+ unsafe fn buffer_write ( & mut self , off : usize , value : T ) -> & mut T {
188
191
unsafe {
189
192
let ptr = self . ptr ( ) . add ( off) ;
190
193
ptr:: write ( ptr, value) ;
@@ -1901,59 +1904,57 @@ impl<T, A: Allocator> VecDeque<T, A> {
1901
1904
self . buffer_write ( self . head , value) ;
1902
1905
}
1903
1906
}
1904
-
1905
- /// Appends an element to the back of the deque .
1907
+
1908
+ /// Prepends an element to the deque, returning a reference to it .
1906
1909
///
1907
1910
/// # Examples
1908
1911
///
1909
1912
/// ```
1913
+ /// #![feature(push_mut)]
1910
1914
/// use std::collections::VecDeque;
1911
1915
///
1912
- /// let mut buf = VecDeque::new( );
1913
- /// buf.push_back(1 );
1914
- /// buf.push_back(3) ;
1915
- /// assert_eq!(3, *buf.back().unwrap( ));
1916
+ /// let mut d = VecDeque::from([1, 2, 3] );
1917
+ /// let x = d.push_front_mut(8 );
1918
+ /// *x -= 1 ;
1919
+ /// assert_eq!(d.front(), Some(&7 ));
1916
1920
/// ```
1917
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1918
- #[ rustc_confusables( "push" , "put" , "append" ) ]
1921
+ #[ unstable( feature = "push_mut" , issue = "135974" ) ]
1919
1922
#[ track_caller]
1920
- pub fn push_back ( & mut self , value : T ) {
1923
+ #[ must_use = "if you don't need a reference to the value, use VecDeque::push_front instead" ]
1924
+ pub fn push_front_mut ( & mut self , value : T ) -> & mut T {
1921
1925
if self . is_full ( ) {
1922
1926
self . grow ( ) ;
1923
1927
}
1924
1928
1925
- unsafe { self . buffer_write ( self . to_physical_idx ( self . len ) , value ) } ;
1929
+ self . head = self . wrap_sub ( self . head , 1 ) ;
1926
1930
self . len += 1 ;
1931
+ // SAFETY: We know that self.head is within range of the deque.
1932
+ unsafe { self . buffer_write ( self . head , value) }
1927
1933
}
1928
1934
1929
- /// Prepends an element to the deque, returning a reference to it .
1935
+ /// Appends an element to the back of the deque .
1930
1936
///
1931
1937
/// # Examples
1932
1938
///
1933
1939
/// ```
1934
- /// #![feature(push_mut)]
1935
1940
/// use std::collections::VecDeque;
1936
1941
///
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 ));
1942
+ /// let mut buf = VecDeque::new( );
1943
+ /// buf.push_back (1);
1944
+ /// buf.push_back(3) ;
1945
+ /// assert_eq!(3, *buf.back().unwrap( ));
1941
1946
/// ```
1942
- #[ unstable( feature = "push_mut" , issue = "135974" ) ]
1947
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1948
+ #[ rustc_confusables( "push" , "put" , "append" ) ]
1943
1949
#[ 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 {
1950
+ pub fn push_back ( & mut self , value : T ) {
1946
1951
if self . is_full ( ) {
1947
1952
self . grow ( ) ;
1948
1953
}
1949
-
1950
- self . head = self . wrap_sub ( self . head , 1 ) ;
1954
+
1955
+ // SAFETY: We know that self.head is within range of the deque.
1956
+ unsafe { self . buffer_write ( self . to_physical_idx ( self . len ) , value) } ;
1951
1957
self . len += 1 ;
1952
-
1953
- unsafe {
1954
- let ptr = self . buffer_write ( self . head , value) ;
1955
- & mut * ptr
1956
- }
1957
1958
}
1958
1959
1959
1960
/// Appends an element to the back of the deque, returning a reference to it.
@@ -1965,9 +1966,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
1965
1966
/// use std::collections::VecDeque;
1966
1967
///
1967
1968
/// let mut d = VecDeque::from([1, 2, 3]);
1968
- /// let x = d.push_back_mut(3 );
1969
+ /// let x = d.push_back_mut(9 );
1969
1970
/// *x += 1;
1970
- /// assert_eq!(d.back(), Some(&4 ));
1971
+ /// assert_eq!(d.back(), Some(&10 ));
1971
1972
/// ```
1972
1973
#[ unstable( feature = "push_mut" , issue = "135974" ) ]
1973
1974
#[ track_caller]
@@ -2158,7 +2159,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2158
2159
self . wrap_copy ( self . to_physical_idx ( index) , self . to_physical_idx ( index + 1 ) , k) ;
2159
2160
let ptr = self . buffer_write ( self . to_physical_idx ( index) , value) ;
2160
2161
self . len += 1 ;
2161
- Some ( & mut * ptr)
2162
+ Some ( ptr)
2162
2163
}
2163
2164
} else {
2164
2165
let old_head = self . head ;
@@ -2167,7 +2168,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2167
2168
self . wrap_copy ( old_head, self . head , index) ;
2168
2169
let ptr = self . buffer_write ( self . to_physical_idx ( index) , value) ;
2169
2170
self . len += 1 ;
2170
- Some ( & mut * ptr)
2171
+ Some ( ptr)
2171
2172
}
2172
2173
}
2173
2174
}
0 commit comments