bivec/core/
insert.rs

1use super::*; 
2
3impl <F, S> BiVec<F, S> {
4    pub fn push(&mut self, f: F, s: S) {
5        let len = self.len(); 
6        self.reserve(1); 
7        unsafe {
8            let first_ptr = self.first.as_ptr().add(len); 
9            let second_ptr = self.second.as_ptr().add(len); 
10            first_ptr.write(f); 
11            second_ptr.write(s); 
12        }
13        self.len += 1;  
14    }
15    pub fn insert(&mut self, index: usize, f: F, s: S) {
16        let len = self.len(); 
17        assert!(index <= len, "index out of bounds"); 
18        self.reserve(1); 
19        unsafe {
20            let first_ptr = self.first.as_ptr().add(index); 
21            let second_ptr = self.second.as_ptr().add(index); 
22            if index != len {
23                std::ptr::copy(first_ptr, first_ptr.add(1), len - index); 
24                std::ptr::copy(second_ptr, second_ptr.add(1), len - index); 
25            }
26            first_ptr.write(f); 
27            second_ptr.write(s); 
28        } 
29        self.len += 1; 
30    }
31    pub fn push_within_capacity(&mut self, f: F, s: S) -> Result<(), (F, S)> {
32        if self.len() < self.capacity() {
33            self.push(f, s); 
34            Ok(())
35        } else {
36            Err((f, s))
37        } 
38    }
39}