pub fn shrink_to_fit(&mut self) {
        // +1 since the ringbuffer always leaves one space empty
        // len + 1 can't overflow for an existing, well-formed ringbuf.
        let target_cap = cmp::max(self.len() + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
        if target_cap < self.cap {
            // There are three cases of interest:
            //   All elements are out of desired bounds
            //   Elements are contiguous, and head is out of desired bounds
            //   Elements are discontiguous, and tail is out of desired bounds
            //
            // At all other times, element positions are unaffected.
            //
            // Indicates that elements at the head should be moved.
            let head_outside = self.head == 0 || self.head >= target_cap;
            // Move elements from out of desired bounds (positions after target_cap)
            if self.tail >= target_cap && head_outside {
                //                    T             H
                //   [. . . . . . . . o o o o o o o . ]
                //    T             H
                //   [o o o o o o o . ]
                unsafe {
                    self.copy_nonoverlapping(0, self.tail, self.len());
                }
                self.head = self.len();
                self.tail = 0;
            } else if self.tail != 0 && self.tail < target_cap && head_outside {
                //          T             H
                //   [. . . o o o o o o o . . . . . . ]
                //        H T
                //   [o o . o o o o o ]
                let len = self.wrap_sub(self.head, target_cap);
                unsafe {
                    self.copy_nonoverlapping(0, target_cap, len);
                }
                self.head = len;
                debug_assert!(self.head < self.tail);
            } else if self.tail >= target_cap {
                //              H                 T
                //   [o o o o o . . . . . . . . . o o ]
                //              H T
                //   [o o o o o . o o ]
                debug_assert!(self.wrap_sub(self.head, 1) < target_cap);
                let len = self.cap - self.tail;
                let new_tail = target_cap - len;
                unsafe {
                    self.copy_nonoverlapping(new_tail, self.tail, len);
                }
                self.tail = new_tail;
                debug_assert!(self.head < self.tail);
            }

            if mem::size_of::<T>() != 0 {
                let old = self.cap * mem::size_of::<T>();
                let new_size = target_cap * mem::size_of::<T>();
                unsafe {
                    let ptr = heap::reallocate(*self.ptr as *mut u8,
                                               old,
                                               new_size,
                                               mem::min_align_of::<T>()) as *mut T;
                    if ptr.is_null() { ::alloc::oom() }
                    self.ptr = Unique::new(ptr);
                }
            }
            self.cap = target_cap;
            debug_assert!(self.head < self.cap);
            debug_assert!(self.tail < self.cap);
            debug_assert!(self.cap.count_ones() == 1);
        }
    }
/// Shrinks the capacity of the ringbuf as much as possible.
///
/// It will drop down as close as possible to the length but the allocator may still inform the
/// ringbuf that there is space for a few more elements.
///
/// # Examples
///
/// ```
/// # #![feature(collections)]
/// use std::collections::VecDeque;
///
/// let mut buf = VecDeque::with_capacity(15);
/// buf.extend(0..4);
/// assert_eq!(buf.capacity(), 15);
/// buf.shrink_to_fit();
/// assert!(buf.capacity() >= 4);
/// ```
Shrinks the capacity of the ringbuf as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the
ringbuf that there is space for a few more elements.
Shrinks the capacity of the ringbuf as much as possible . It will drop down as close as possible to the length but the allocator may still inform the ringbuf that there is space for a few more elements .
pub fn shrink_to_fit(&mut self) {
        // +1 since the ringbuffer always leaves one space empty
        // len + 1 can't overflow for an existing, well-formed ringbuf.
        let target_cap = cmp::max(self.len() + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
        if target_cap < self.cap {
            // There are three cases of interest:
            //   All elements are out of desired bounds
            //   Elements are contiguous, and head is out of desired bounds
            //   Elements are discontiguous, and tail is out of desired bounds
            //
            // At all other times, element positions are unaffected.
            //
            // Indicates that elements at the head should be moved.
            let head_outside = self.head == 0 || self.head >= target_cap;
            // Move elements from out of desired bounds (positions after target_cap)
            if self.tail >= target_cap && head_outside {
                //                    T             H
                //   [. . . . . . . . o o o o o o o . ]
                //    T             H
                //   [o o o o o o o . ]
                unsafe {
                    self.copy_nonoverlapping(0, self.tail, self.len());
                }
                self.head = self.len();
                self.tail = 0;
            } else if self.tail != 0 && self.tail < target_cap && head_outside {
                //          T             H
                //   [. . . o o o o o o o . . . . . . ]
                //        H T
                //   [o o . o o o o o ]
                let len = self.wrap_sub(self.head, target_cap);
                unsafe {
                    self.copy_nonoverlapping(0, target_cap, len);
                }
                self.head = len;
                debug_assert!(self.head < self.tail);
            } else if self.tail >= target_cap {
                //              H                 T
                //   [o o o o o . . . . . . . . . o o ]
                //              H T
                //   [o o o o o . o o ]
                debug_assert!(self.wrap_sub(self.head, 1) < target_cap);
                let len = self.cap - self.tail;
                let new_tail = target_cap - len;
                unsafe {
                    self.copy_nonoverlapping(new_tail, self.tail, len);
                }
                self.tail = new_tail;
                debug_assert!(self.head < self.tail);
            }
            if mem::size_of::<T>() != 0 {
                let old = self.cap * mem::size_of::<T>();
                let new_size = target_cap * mem::size_of::<T>();
                unsafe {
                    let ptr = heap::reallocate(*self.ptr as *mut u8,
                                               old,
                                               new_size,
                                               mem::min_align_of::<T>()) as *mut T;
                    if ptr.is_null() { ::alloc::oom() }
                    self.ptr = Unique::new(ptr);
                }
            }
            self.cap = target_cap;
            debug_assert!(self.head < self.cap);
            debug_assert!(self.tail < self.cap);
            debug_assert!(self.cap.count_ones() == 1);
        }
    }
pub fn shrink_to_fit ( & mut self ) { // +1 since the ringbuffer always leaves one space empty // len + 1 can't overflow for an existing, well-formed ringbuf. let target_cap = cmp :: max ( self . len ( ) + 1 , MINIMUM_CAPACITY + 1 ) . next_power_of_two ( ) ; if target_cap < self . cap { // There are three cases of interest: //   All elements are out of desired bounds //   Elements are contiguous, and head is out of desired bounds //   Elements are discontiguous, and tail is out of desired bounds // // At all other times, element positions are unaffected. // // Indicates that elements at the head should be moved. let head_outside = self . head == 0 || self . head >= target_cap ; // Move elements from out of desired bounds (positions after target_cap) if self . tail >= target_cap && head_outside { //                    T             H //   [. . . . . . . . o o o o o o o . ] //    T             H //   [o o o o o o o . ] unsafe { self . copy_nonoverlapping ( 0 , self . tail , self . len ( ) ) ; } self . head = self . len ( ) ; self . tail = 0 ; } else if self . tail != 0 && self . tail < target_cap && head_outside { //          T             H //   [. . . o o o o o o o . . . . . . ] //        H T //   [o o . o o o o o ] let len = self . wrap_sub ( self . head , target_cap ) ; unsafe { self . copy_nonoverlapping ( 0 , target_cap , len ) ; } self . head = len ; debug_assert ! ( self head self tail ) ; } else if self . tail >= target_cap { //              H                 T //   [o o o o o . . . . . . . . . o o ] //              H T //   [o o o o o . o o ] debug_assert ! ( self wrap_sub ( self head 1 ) target_cap ) ; let len = self . cap - self . tail ; let new_tail = target_cap - len ; unsafe { self . copy_nonoverlapping ( new_tail , self . tail , len ) ; } self . tail = new_tail ; debug_assert ! ( self head self tail ) ; } if mem :: size_of :: < T > ( ) != 0 { let old = self . cap * mem :: size_of :: < T > ( ) ; let new_size = target_cap * mem :: size_of :: < T > ( ) ; unsafe { let ptr = heap :: reallocate ( * self . ptr as * mut u8 , old , new_size , mem :: min_align_of :: < T > ( ) ) as * mut T ; if ptr . is_null ( ) { :: alloc :: oom ( ) } self . ptr = Unique :: new ( ptr ) ; } } self . cap = target_cap ; debug_assert ! ( self head self cap ) ; debug_assert ! ( self tail self cap ) ; debug_assert ! ( self cap count_ones ( ) 1 ) ; } }
Shrinks the capacity of the ringbuf as much as possible.
Shrinks the capacity of the ringbuf as much as possible .
&mut self
examples
None
#![feature(collections)]
use std::collections::VecDeque.

let mut buf = VecDeque::with_capacity(15);
buf.extend(0..4);
assert_eq!(buf.capacity(), 15);
buf.shrink_to_fit();
assert!(buf.capacity() >= 4);
```
use std :: collections :: VecDeque . let mut buf = VecDeque :: with_capacity ( 15 ) ; buf . extend ( 0 .. 4 ) ; assert_eq! ( buf . capacity () 15 ) ; buf . shrink_to_fit () ; assert! ( buf . capacity () > = 4 ) ; ` ` `