pub fn insert(&mut self, index: usize, element: T, j: &Journal<A>) {
        let len = self.len();
        assert!(index <= len);

        // space for the new element
        if len == self.buf.capacity() {
            self.reserve(1, j);
        }

        unsafe {
            // infallible
            // The spot to put the new value
            {
                let p = self.buf.as_mut_ptr().add(index);
                // Shift everything over to make space. (Duplicating the
                // `index`th element into two consecutive places.)
                ptr::copy(p, p.offset(1), len - index);
                // Write it in, overwriting the first copy of the `index`th
                // element.
                ptr::write(p, element);
            }
            self.set_len(len + 1);
        }
    }
/// Inserts an element at position `index` within the vector, shifting all
/// elements after it to the right.
///
/// # Panics
///
/// Panics if `index > len`.
///
/// # Examples
///
/// ```
/// # use corundum::vec::Vec;
/// # use corundum::alloc::heap::*;
/// Heap::transaction(|j| {
///     let mut vec = Vec::from_slice(&[1, 2, 3], j);
///     vec.insert(1, 4, j);
///     assert_eq!(vec, [1, 4, 2, 3]);
///     vec.insert(4, 5, j);
///     assert_eq!(vec, [1, 4, 2, 3, 5]);
/// }).unwrap();
/// ```
Inserts an element at position `index` within the vector, shifting all
elements after it to the right.
Inserts an element at position ` index ` within the vector shifting all elements after it to the right .
pub fn insert(&mut self, index: usize, element: T, j: &Journal<A>) {
        let len = self.len();
        assert!(index <= len);
        // space for the new element
        if len == self.buf.capacity() {
            self.reserve(1, j);
        }
        unsafe {
            // infallible
            // The spot to put the new value
            {
                let p = self.buf.as_mut_ptr().add(index);
                // Shift everything over to make space. (Duplicating the
                // `index`th element into two consecutive places.)
                ptr::copy(p, p.offset(1), len - index);
                // Write it in, overwriting the first copy of the `index`th
                // element.
                ptr::write(p, element);
            }
            self.set_len(len + 1);
        }
    }
pub fn insert ( & mut self , index : usize , element : T , j : & Journal < A > ) { let len = self . len ( ) ; assert ! ( index len ) ; // space for the new element if len == self . buf . capacity ( ) { self . reserve ( 1 , j ) ; } unsafe { // infallible // The spot to put the new value { let p = self . buf . as_mut_ptr ( ) . add ( index ) ; // Shift everything over to make space. (Duplicating the // `index`th element into two consecutive places.) ptr :: copy ( p , p . offset ( 1 ) , len - index ) ; // Write it in, overwriting the first copy of the `index`th // element. ptr :: write ( p , element ) ; } self . set_len ( len + 1 ) ; } }
Inserts an element at position `index` within the vector, shifting all
elements after it to the right.
Inserts an element at position ` index ` within the vector shifting all elements after it to the right .
&mut self
index
usize
element
T
j
&Journal<A>
panics
Panics if `index > len`.
Panics if ` index > len ` .
examples
None
use corundum::vec::vec;
None
use corundum::alloc::heap::*;
None