@@ -196,7 +196,7 @@ pub fn derive(input: &Input) -> TokenStream {
196196 /// ::insert()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.insert).
197197 #[ allow( clippy:: forget_non_drop) ]
198198 pub fn insert( & mut self , index: usize , element: #name) {
199- if index > self . len( ) {
199+ if index >= self . len( ) {
200200 panic!( "index out of bounds: the len is {} but the index is {}" , self . len( ) , index) ;
201201 }
202202
@@ -213,7 +213,7 @@ pub fn derive(input: &Input) -> TokenStream {
213213 /// Similar to [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html).
214214 #[ allow( clippy:: forget_non_drop) ]
215215 pub fn replace( & mut self , index: usize , element: #name) -> #name {
216- if index > self . len( ) {
216+ if index >= self . len( ) {
217217 panic!( "index out of bounds: the len is {} but the index is {}" , self . len( ) , index) ;
218218 }
219219
@@ -317,43 +317,43 @@ pub fn derive(input: &Input) -> TokenStream {
317317 #[ doc = #vec_name_str]
318318 /// ::retain()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain).
319319 pub fn retain<F >( & mut self , mut f: F ) where F : FnMut ( #ref_name) -> bool {
320- let len = self . len( ) ;
321- let mut del = 0 ;
322-
323- {
324- let mut slice = self . as_mut_slice( ) ;
325- for i in 0 ..len {
326- if !f( slice. get( i) . unwrap( ) ) {
327- del += 1 ;
328- } else if del > 0 {
329- slice. swap( i - del, i) ;
320+ let mut slice = self . as_mut_slice( ) ;
321+ let len = slice. len( ) ;
322+ let mut write_idx = 0 ;
323+
324+ for read_idx in 0 ..len {
325+ if f( slice. get( read_idx) . unwrap( ) ) {
326+ if write_idx != read_idx {
327+ slice. swap( write_idx, read_idx) ;
330328 }
329+ write_idx += 1 ;
331330 }
332331 }
333- if del > 0 {
334- self . truncate( len - del) ;
332+
333+ if write_idx < len {
334+ self . truncate( write_idx) ;
335335 }
336336 }
337337
338338 /// Similar to [`
339339 #[ doc = #vec_name_str]
340340 /// ::retain_mut()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain_mut).
341341 pub fn retain_mut<F >( & mut self , mut f: F ) where F : FnMut ( #ref_mut_name) -> bool {
342- let len = self . len( ) ;
343- let mut del = 0 ;
344-
345- {
346- let mut slice = self . as_mut_slice( ) ;
347- for i in 0 ..len {
348- if !f( slice. get_mut( i) . unwrap( ) ) {
349- del += 1 ;
350- } else if del > 0 {
351- slice. swap( i - del, i) ;
342+ let mut slice = self . as_mut_slice( ) ;
343+ let len = slice. len( ) ;
344+ let mut write_idx = 0 ;
345+
346+ for read_idx in 0 ..len {
347+ if f( slice. get_mut( read_idx) . unwrap( ) ) {
348+ if write_idx != read_idx {
349+ slice. swap( write_idx, read_idx) ;
352350 }
351+ write_idx += 1 ;
353352 }
354353 }
355- if del > 0 {
356- self . truncate( len - del) ;
354+
355+ if write_idx < len {
356+ self . truncate( write_idx) ;
357357 }
358358 }
359359
0 commit comments