@@ -636,14 +636,34 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
636636 if self . len ( ) < self . capacity ( ) {
637637 Err ( self )
638638 } else {
639- unsafe {
640- let self_ = ManuallyDrop :: new ( self ) ;
641- let array = ptr:: read ( self_. as_ptr ( ) as * const [ T ; CAP ] ) ;
642- Ok ( array)
643- }
639+ unsafe { Ok ( self . into_inner_unchecked ( ) ) }
644640 }
645641 }
646642
643+ /// Return the inner fixed size array.
644+ ///
645+ /// Safety:
646+ /// This operation is safe if and only if length equals capacity.
647+ pub unsafe fn into_inner_unchecked ( self ) -> [ T ; CAP ] {
648+ debug_assert_eq ! ( self . len( ) , self . capacity( ) ) ;
649+ let self_ = ManuallyDrop :: new ( self ) ;
650+ let array = ptr:: read ( self_. as_ptr ( ) as * const [ T ; CAP ] ) ;
651+ array
652+ }
653+
654+ /// Returns the ArrayVec, replacing the original with a new empty ArrayVec.
655+ ///
656+ /// ```
657+ /// use arrayvec::ArrayVec;
658+ ///
659+ /// let mut v = ArrayVec::from([0, 1, 2, 3]);
660+ /// assert_eq!([0, 1, 2, 3], v.take().into_inner().unwrap());
661+ /// assert!(v.is_empty());
662+ /// ```
663+ pub fn take ( & mut self ) -> Self {
664+ mem:: replace ( self , Self :: new ( ) )
665+ }
666+
647667 /// Return a slice containing all elements of the vector.
648668 pub fn as_slice ( & self ) -> & [ T ] {
649669 ArrayVecImpl :: as_slice ( self )
0 commit comments