pub fn insert(&mut self, i: usize, t: T) {
        assert!(i <= self.len(), "index out of bounds");
        if self.is_full() {
            self.reserve(1);
            debug_assert!(!self.is_full());
        }

        // Move the least number of elements in the ring buffer and insert
        // the given object
        //
        // At most len/2 - 1 elements will be moved. O(min(n, n-i))
        //
        // There are three main cases:
        //  Elements are contiguous
        //      - special case when tail is 0
        //  Elements are discontiguous and the insert is in the tail section
        //  Elements are discontiguous and the insert is in the head section
        //
        // For each of those there are two more cases:
        //  Insert is closer to tail
        //  Insert is closer to head
        //
        // Key: H - self.head
        //      T - self.tail
        //      o - Valid element
        //      I - Insertion element
        //      A - The element that should be after the insertion point
        //      M - Indicates element was moved

        let idx = self.wrap_add(self.tail, i);

        let distance_to_tail = i;
        let distance_to_head = self.len() - i;

        let contiguous = self.is_contiguous();

        match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) {
            (true, true, _) if i == 0 => {
                // push_front
                //
                //       T
                //       I             H
                //      [A o o o o o o . . . . . . . . .]
                //
                //                       H         T
                //      [A o o o o o o o . . . . . I]
                //

                self.tail = self.wrap_sub(self.tail, 1);
            },
            (true, true, _) => unsafe {
                // contiguous, insert closer to tail:
                //
                //             T   I         H
                //      [. . . o o A o o o o . . . . . .]
                //
                //           T               H
                //      [. . o o I A o o o o . . . . . .]
                //           M M
                //
                // contiguous, insert closer to tail and tail is 0:
                //
                //
                //       T   I         H
                //      [o o A o o o o . . . . . . . . .]
                //
                //                       H             T
                //      [o I A o o o o o . . . . . . . o]
                //       M                             M

                let new_tail = self.wrap_sub(self.tail, 1);

                self.copy(new_tail, self.tail, 1);
                // Already moved the tail, so we only copy `i - 1` elements.
                self.copy(self.tail, self.tail + 1, i - 1);

                self.tail = new_tail;
            },
            (true, false, _) => unsafe {
                //  contiguous, insert closer to head:
                //
                //             T       I     H
                //      [. . . o o o o A o o . . . . . .]
                //
                //             T               H
                //      [. . . o o o o I A o o . . . . .]
                //                       M M M

                self.copy(idx + 1, idx, self.head - idx);
                self.head = self.wrap_add(self.head, 1);
            },
            (false, true, true) => unsafe {
                // discontiguous, insert closer to tail, tail section:
                //
                //                   H         T   I
                //      [o o o o o o . . . . . o o A o o]
                //
                //                   H       T
                //      [o o o o o o . . . . o o I A o o]
                //                           M M

                self.copy(self.tail - 1, self.tail, i);
                self.tail -= 1;
            },
            (false, false, true) => unsafe {
                // discontiguous, insert closer to head, tail section:
                //
                //           H             T         I
                //      [o o . . . . . . . o o o o o A o]
                //
                //             H           T
                //      [o o o . . . . . . o o o o o I A]
                //       M M M                         M

                // copy elements up to new head
                self.copy(1, 0, self.head);

                // copy last element into empty spot at bottom of buffer
                self.copy(0, self.cap - 1, 1);

                // move elements from idx to end forward not including ^ element
                self.copy(idx + 1, idx, self.cap - 1 - idx);

                self.head += 1;
            },
            (false, true, false) if idx == 0 => unsafe {
                // discontiguous, insert is closer to tail, head section,
                // and is at index zero in the internal buffer:
                //
                //       I                   H     T
                //      [A o o o o o o o o o . . . o o o]
                //
                //                           H   T
                //      [A o o o o o o o o o . . o o o I]
                //                               M M M

                // copy elements up to new tail
                self.copy(self.tail - 1, self.tail, self.cap - self.tail);

                // copy last element into empty spot at bottom of buffer
                self.copy(self.cap - 1, 0, 1);

                self.tail -= 1;
            },
            (false, true, false) => unsafe {
                // discontiguous, insert closer to tail, head section:
                //
                //             I             H     T
                //      [o o o A o o o o o o . . . o o o]
                //
                //                           H   T
                //      [o o I A o o o o o o . . o o o o]
                //       M M                     M M M M

                // copy elements up to new tail
                self.copy(self.tail - 1, self.tail, self.cap - self.tail);

                // copy last element into empty spot at bottom of buffer
                self.copy(self.cap - 1, 0, 1);

                // move elements from idx-1 to end forward not including ^ element
                self.copy(0, 1, idx - 1);

                self.tail -= 1;
            },
            (false, false, false) => unsafe {
                // discontiguous, insert closer to head, head section:
                //
                //               I     H           T
                //      [o o o o A o o . . . . . . o o o]
                //
                //                     H           T
                //      [o o o o I A o o . . . . . o o o]
                //                 M M M

                self.copy(idx + 1, idx, self.head - idx);
                self.head += 1;
            }
        }

        // tail might've been changed so we need to recalculate
        let new_idx = self.wrap_add(self.tail, i);
        unsafe {
            self.buffer_write(new_idx, t);
        }
    }
/// Inserts an element at position `i` within the ringbuf. Whichever
/// end is closer to the insertion point will be moved to make room,
/// and all the affected elements will be moved to new positions.
///
/// # Panics
///
/// Panics if `i` is greater than ringbuf's length
///
/// # Examples
/// ```
/// # #![feature(collections)]
/// use std::collections::VecDeque;
///
/// let mut buf = VecDeque::new();
/// buf.push_back(10);
/// buf.push_back(12);
/// buf.insert(1,11);
/// assert_eq!(Some(&11), buf.get(1));
/// ```
Inserts an element at position `i` within the ringbuf. Whichever
end is closer to the insertion point will be moved to make room,
and all the affected elements will be moved to new positions.
Inserts an element at position ` i ` within the ringbuf . Whichever end is closer to the insertion point will be moved to make room and all the affected elements will be moved to new positions .
pub fn insert(&mut self, i: usize, t: T) {
        assert!(i <= self.len(), "index out of bounds");
        if self.is_full() {
            self.reserve(1);
            debug_assert!(!self.is_full());
        }
        // Move the least number of elements in the ring buffer and insert
        // the given object
        //
        // At most len/2 - 1 elements will be moved. O(min(n, n-i))
        //
        // There are three main cases:
        //  Elements are contiguous
        //      - special case when tail is 0
        //  Elements are discontiguous and the insert is in the tail section
        //  Elements are discontiguous and the insert is in the head section
        //
        // For each of those there are two more cases:
        //  Insert is closer to tail
        //  Insert is closer to head
        //
        // Key: H - self.head
        //      T - self.tail
        //      o - Valid element
        //      I - Insertion element
        //      A - The element that should be after the insertion point
        //      M - Indicates element was moved
        let idx = self.wrap_add(self.tail, i);
        let distance_to_tail = i;
        let distance_to_head = self.len() - i;
        let contiguous = self.is_contiguous();
        match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) {
            (true, true, _) if i == 0 => {
                // push_front
                //
                //       T
                //       I             H
                //      [A o o o o o o . . . . . . . . .]
                //
                //                       H         T
                //      [A o o o o o o o . . . . . I]
                //
                self.tail = self.wrap_sub(self.tail, 1);
            },
            (true, true, _) => unsafe {
                // contiguous, insert closer to tail:
                //
                //             T   I         H
                //      [. . . o o A o o o o . . . . . .]
                //
                //           T               H
                //      [. . o o I A o o o o . . . . . .]
                //           M M
                //
                // contiguous, insert closer to tail and tail is 0:
                //
                //
                //       T   I         H
                //      [o o A o o o o . . . . . . . . .]
                //
                //                       H             T
                //      [o I A o o o o o . . . . . . . o]
                //       M                             M
                let new_tail = self.wrap_sub(self.tail, 1);
                self.copy(new_tail, self.tail, 1);
                // Already moved the tail, so we only copy `i - 1` elements.
                self.copy(self.tail, self.tail + 1, i - 1);
                self.tail = new_tail;
            },
            (true, false, _) => unsafe {
                //  contiguous, insert closer to head:
                //
                //             T       I     H
                //      [. . . o o o o A o o . . . . . .]
                //
                //             T               H
                //      [. . . o o o o I A o o . . . . .]
                //                       M M M
                self.copy(idx + 1, idx, self.head - idx);
                self.head = self.wrap_add(self.head, 1);
            },
            (false, true, true) => unsafe {
                // discontiguous, insert closer to tail, tail section:
                //
                //                   H         T   I
                //      [o o o o o o . . . . . o o A o o]
                //
                //                   H       T
                //      [o o o o o o . . . . o o I A o o]
                //                           M M
                self.copy(self.tail - 1, self.tail, i);
                self.tail -= 1;
            },
            (false, false, true) => unsafe {
                // discontiguous, insert closer to head, tail section:
                //
                //           H             T         I
                //      [o o . . . . . . . o o o o o A o]
                //
                //             H           T
                //      [o o o . . . . . . o o o o o I A]
                //       M M M                         M
                // copy elements up to new head
                self.copy(1, 0, self.head);
                // copy last element into empty spot at bottom of buffer
                self.copy(0, self.cap - 1, 1);
                // move elements from idx to end forward not including ^ element
                self.copy(idx + 1, idx, self.cap - 1 - idx);
                self.head += 1;
            },
            (false, true, false) if idx == 0 => unsafe {
                // discontiguous, insert is closer to tail, head section,
                // and is at index zero in the internal buffer:
                //
                //       I                   H     T
                //      [A o o o o o o o o o . . . o o o]
                //
                //                           H   T
                //      [A o o o o o o o o o . . o o o I]
                //                               M M M
                // copy elements up to new tail
                self.copy(self.tail - 1, self.tail, self.cap - self.tail);
                // copy last element into empty spot at bottom of buffer
                self.copy(self.cap - 1, 0, 1);
                self.tail -= 1;
            },
            (false, true, false) => unsafe {
                // discontiguous, insert closer to tail, head section:
                //
                //             I             H     T
                //      [o o o A o o o o o o . . . o o o]
                //
                //                           H   T
                //      [o o I A o o o o o o . . o o o o]
                //       M M                     M M M M
                // copy elements up to new tail
                self.copy(self.tail - 1, self.tail, self.cap - self.tail);
                // copy last element into empty spot at bottom of buffer
                self.copy(self.cap - 1, 0, 1);
                // move elements from idx-1 to end forward not including ^ element
                self.copy(0, 1, idx - 1);
                self.tail -= 1;
            },
            (false, false, false) => unsafe {
                // discontiguous, insert closer to head, head section:
                //
                //               I     H           T
                //      [o o o o A o o . . . . . . o o o]
                //
                //                     H           T
                //      [o o o o I A o o . . . . . o o o]
                //                 M M M
                self.copy(idx + 1, idx, self.head - idx);
                self.head += 1;
            }
        }
        // tail might've been changed so we need to recalculate
        let new_idx = self.wrap_add(self.tail, i);
        unsafe {
            self.buffer_write(new_idx, t);
        }
    }
pub fn insert ( & mut self , i : usize , t : T ) { assert ! ( i self len ( ) " " ) ; if self . is_full ( ) { self . reserve ( 1 ) ; debug_assert ! ( self is_full ( ) ) ; } // Move the least number of elements in the ring buffer and insert // the given object // // At most len/2 - 1 elements will be moved. O(min(n, n-i)) // // There are three main cases: //  Elements are contiguous //      - special case when tail is 0 //  Elements are discontiguous and the insert is in the tail section //  Elements are discontiguous and the insert is in the head section // // For each of those there are two more cases: //  Insert is closer to tail //  Insert is closer to head // // Key: H - self.head //      T - self.tail //      o - Valid element //      I - Insertion element //      A - The element that should be after the insertion point //      M - Indicates element was moved let idx = self . wrap_add ( self . tail , i ) ; let distance_to_tail = i ; let distance_to_head = self . len ( ) - i ; let contiguous = self . is_contiguous ( ) ; match ( contiguous , distance_to_tail <= distance_to_head , idx >= self . tail ) { ( true , true , _ ) if i == 0 => { // push_front // //       T //       I             H //      [A o o o o o o . . . . . . . . .] // //                       H         T //      [A o o o o o o o . . . . . I] // self . tail = self . wrap_sub ( self . tail , 1 ) ; } , ( true , true , _ ) => unsafe { // contiguous, insert closer to tail: // //             T   I         H //      [. . . o o A o o o o . . . . . .] // //           T               H //      [. . o o I A o o o o . . . . . .] //           M M // // contiguous, insert closer to tail and tail is 0: // // //       T   I         H //      [o o A o o o o . . . . . . . . .] // //                       H             T //      [o I A o o o o o . . . . . . . o] //       M                             M let new_tail = self . wrap_sub ( self . tail , 1 ) ; self . copy ( new_tail , self . tail , 1 ) ; // Already moved the tail, so we only copy `i - 1` elements. self . copy ( self . tail , self . tail + 1 , i - 1 ) ; self . tail = new_tail ; } , ( true , false , _ ) => unsafe { //  contiguous, insert closer to head: // //             T       I     H //      [. . . o o o o A o o . . . . . .] // //             T               H //      [. . . o o o o I A o o . . . . .] //                       M M M self . copy ( idx + 1 , idx , self . head - idx ) ; self . head = self . wrap_add ( self . head , 1 ) ; } , ( false , true , true ) => unsafe { // discontiguous, insert closer to tail, tail section: // //                   H         T   I //      [o o o o o o . . . . . o o A o o] // //                   H       T //      [o o o o o o . . . . o o I A o o] //                           M M self . copy ( self . tail - 1 , self . tail , i ) ; self . tail -= 1 ; } , ( false , false , true ) => unsafe { // discontiguous, insert closer to head, tail section: // //           H             T         I //      [o o . . . . . . . o o o o o A o] // //             H           T //      [o o o . . . . . . o o o o o I A] //       M M M                         M // copy elements up to new head self . copy ( 1 , 0 , self . head ) ; // copy last element into empty spot at bottom of buffer self . copy ( 0 , self . cap - 1 , 1 ) ; // move elements from idx to end forward not including ^ element self . copy ( idx + 1 , idx , self . cap - 1 - idx ) ; self . head += 1 ; } , ( false , true , false ) if idx == 0 => unsafe { // discontiguous, insert is closer to tail, head section, // and is at index zero in the internal buffer: // //       I                   H     T //      [A o o o o o o o o o . . . o o o] // //                           H   T //      [A o o o o o o o o o . . o o o I] //                               M M M // copy elements up to new tail self . copy ( self . tail - 1 , self . tail , self . cap - self . tail ) ; // copy last element into empty spot at bottom of buffer self . copy ( self . cap - 1 , 0 , 1 ) ; self . tail -= 1 ; } , ( false , true , false ) => unsafe { // discontiguous, insert closer to tail, head section: // //             I             H     T //      [o o o A o o o o o o . . . o o o] // //                           H   T //      [o o I A o o o o o o . . o o o o] //       M M                     M M M M // copy elements up to new tail self . copy ( self . tail - 1 , self . tail , self . cap - self . tail ) ; // copy last element into empty spot at bottom of buffer self . copy ( self . cap - 1 , 0 , 1 ) ; // move elements from idx-1 to end forward not including ^ element self . copy ( 0 , 1 , idx - 1 ) ; self . tail -= 1 ; } , ( false , false , false ) => unsafe { // discontiguous, insert closer to head, head section: // //               I     H           T //      [o o o o A o o . . . . . . o o o] // //                     H           T //      [o o o o I A o o . . . . . o o o] //                 M M M self . copy ( idx + 1 , idx , self . head - idx ) ; self . head += 1 ; } } // tail might've been changed so we need to recalculate let new_idx = self . wrap_add ( self . tail , i ) ; unsafe { self . buffer_write ( new_idx , t ) ; } }
Inserts an element at position `i` within the ringbuf.
Inserts an element at position ` i ` within the ringbuf .
&mut self
i
usize
t
T
panics
Panics if `i` is greater than ringbuf's length
Panics if ` i ` is greater than ringbuf ' s length
examples
None
#![feature(collections)]
None