@@ -47,7 +47,7 @@ pub use self::iter::Iter;
4747
4848mod iter;
4949
50- use self :: spec_extend:: SpecExtend ;
50+ use self :: spec_extend:: { SpecExtend , SpecExtendFront } ;
5151
5252mod spec_extend;
5353
@@ -176,6 +176,21 @@ impl<T, A: Allocator> VecDeque<T, A> {
176176 self . len += 1 ;
177177 }
178178
179+ /// Prepends an element to the buffer.
180+ ///
181+ /// # Safety
182+ ///
183+ /// May only be called if `deque.len() < deque.capacity()`
184+ #[ inline]
185+ unsafe fn push_front_unchecked ( & mut self , element : T ) {
186+ self . head = self . wrap_sub ( self . head , 1 ) ;
187+ // SAFETY: Because of the precondition, it's guaranteed that there is space
188+ // in the logical array before the first element (where self.head is now).
189+ unsafe { self . buffer_write ( self . head , element) } ;
190+ // This can't overflow because `deque.len() < deque.capacity() <= usize::MAX`.
191+ self . len += 1 ;
192+ }
193+
179194 /// Moves an element out of the buffer
180195 #[ inline]
181196 unsafe fn buffer_read ( & mut self , off : usize ) -> T {
@@ -1968,6 +1983,24 @@ impl<T, A: Allocator> VecDeque<T, A> {
19681983 unsafe { self . buffer_write ( self . to_physical_idx ( len) , value) }
19691984 }
19701985
1986+ /// Prepends all elements from the iterator to the front of the deque, as if [`push_front`][VecDeque::push_front] was called with the elements of the iterator.
1987+ ///
1988+ /// # Examples
1989+ ///
1990+ /// ```
1991+ /// #![feature(deque_extend_front)]
1992+ /// use std::collections::VecDeque;
1993+ ///
1994+ /// let mut deque = VecDeque::from([4, 5, 6]);
1995+ /// deque.extend_front([3, 2, 1]);
1996+ /// assert_eq!(deque, [1, 2, 3, 4, 5, 6]);
1997+ /// ```
1998+ #[ unstable( feature = "deque_extend_front" , issue = "none" ) ]
1999+ #[ track_caller]
2000+ pub fn extend_front < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
2001+ <Self as SpecExtendFront < T , I :: IntoIter > >:: spec_extend_front ( self , iter. into_iter ( ) ) ;
2002+ }
2003+
19712004 #[ inline]
19722005 fn is_contiguous ( & self ) -> bool {
19732006 // Do the calculation like this to avoid overflowing if len + head > usize::MAX
0 commit comments