MCPcopy Index your code
hub / github.com/RustPython/RustPython / try_insert

Method try_insert

crates/common/src/boxvec.rs:107–132  ·  view source on GitHub ↗
(&mut self, index: usize, element: T)

Source from the content-addressed store, hash-verified

105 }
106
107 pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), CapacityError<T>> {
108 if index > self.len() {
109 panic_oob!("try_insert", index, self.len())
110 }
111 if self.len() == self.capacity() {
112 return Err(CapacityError::new(element));
113 }
114 let len = self.len();
115
116 // follows is just like Vec<T>
117 unsafe {
118 // infallible
119 // The spot to put the new value
120 {
121 let p: *mut _ = self.get_unchecked_ptr(index);
122 // Shift everything over to make space. (Duplicating the
123 // `index`th element into two consecutive places.)
124 ptr::copy(p, p.offset(1), len - index);
125 // Write it in, overwriting the first copy of the `index`th
126 // element.
127 ptr::write(p, element);
128 }
129 self.set_len(len + 1);
130 }
131 Ok(())
132 }
133
134 pub fn pop(&mut self) -> Option<T> {
135 if self.is_empty() {

Callers 1

insertMethod · 0.80

Calls 9

newFunction · 0.85
get_unchecked_ptrMethod · 0.80
set_lenMethod · 0.80
writeFunction · 0.70
ErrClass · 0.50
copyFunction · 0.50
lenMethod · 0.45
capacityMethod · 0.45
offsetMethod · 0.45

Tested by

no test coverage detected