@@ -280,7 +280,7 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
280
280
/// The item cannot be inserted at an index greater than the
281
281
/// vector's length or greater or equal to the maximum capacity `N`.
282
282
///
283
- /// # Exmaples
283
+ /// # Examples
284
284
///
285
285
/// When the vector's not full, [`ArrayVec::force_insert`] acts like
286
286
/// [`ArrayVec::insert`]:
@@ -313,18 +313,13 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
313
313
pub fn force_insert ( & mut self , index : usize , item : T ) -> Option < T > {
314
314
let len = self . len ( ) ;
315
315
316
- // bound checks
317
- if index > len {
318
- out_of_bounds ! ( "force_insert" , index, len) ;
319
- }
320
-
321
316
let result;
322
317
323
- if self . is_full ( ) {
324
- // bound checks (here: N == len)
325
- if index >= N {
326
- out_of_bounds ! ( "force_insert" , index , len ) ;
327
- }
318
+ if index > len || index == N {
319
+ // Failed bound checks.
320
+ out_of_bounds ! ( "force_insert" , index, len ) ;
321
+ } else if self . is_full ( ) {
322
+ // The last item must be removed to perform the insertion.
328
323
329
324
unsafe {
330
325
// Store the last item before it's removed.
@@ -335,17 +330,12 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
335
330
self . insert_unchecked_keep_len ( index, item, len - 1 ) ;
336
331
}
337
332
} else {
338
- // bound checks
339
- if index > len {
340
- out_of_bounds ! ( "force_insert" , index, len) ;
341
- }
342
-
343
333
// Since nothing's going to be removed, the vector's size
344
334
// is going to be increased and nothing will be returned.
345
335
unsafe {
346
336
self . insert_unchecked ( index, item, len) ;
347
- result = None ;
348
337
}
338
+ result = None ;
349
339
}
350
340
351
341
result
@@ -380,20 +370,20 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
380
370
/// 3 previously mentioned conditions yourself before calling this
381
371
/// method. You also need to increment the vector's length afterward
382
372
/// yourself.
383
- pub unsafe fn insert_unchecked_keep_len (
373
+ unsafe fn insert_unchecked_keep_len (
384
374
& mut self ,
385
375
index : usize ,
386
376
item : T ,
387
377
len : usize ,
388
378
) {
389
- // The spot to put the Vec:: new(new value);
390
- let p = self . as_mut_ptr ( ) . add ( index) ;
379
+ // The spot to put the new value at.
380
+ let ptr_index = self . as_mut_ptr ( ) . add ( index) ;
391
381
// Shift everything over to make space. (Duplicating the
392
382
// `index`th element into two consecutive places.)
393
- ptr:: copy ( p , p . add ( 1 ) , len - index) ;
383
+ ptr:: copy ( ptr_index , ptr_index . add ( 1 ) , len - index) ;
394
384
// Write it in, overwriting the first copy of the `index`th
395
385
// element.
396
- ptr:: write ( p , item) ;
386
+ ptr:: write ( ptr_index , item) ;
397
387
}
398
388
399
389
/// Remove the value contained at `index` and return it.
@@ -414,10 +404,9 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
414
404
/// assert_eq!(&vector, [4, 2].as_ref());
415
405
/// ```
416
406
pub fn remove ( & mut self , index : usize ) -> T {
417
- if let Some ( item) = self . try_remove ( index) {
418
- item
419
- } else {
420
- out_of_bounds ! ( "remove" , index, self . len( ) ) ;
407
+ match self . try_remove ( index) {
408
+ Some ( item) => item,
409
+ None => out_of_bounds ! ( "remove" , index, self . len( ) ) ,
421
410
}
422
411
}
423
412
@@ -455,11 +444,11 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
455
444
let len = self . len ( ) ;
456
445
457
446
// Where the value to remove is.
458
- let p = self . as_mut_ptr ( ) . add ( index) ;
447
+ let ptr_index = self . as_mut_ptr ( ) . add ( index) ;
459
448
// Read the value before sending it to the other world.
460
- let item = ptr:: read ( p ) ;
449
+ let item = ptr:: read ( ptr_index ) ;
461
450
// Shift every value after the removed one to the left.
462
- ptr:: copy ( p . add ( 1 ) , p , len - index - 1 ) ;
451
+ ptr:: copy ( ptr_index . add ( 1 ) , ptr_index , len - index - 1 ) ;
463
452
// We removed an item, so the length should be decremented.
464
453
self . set_len ( len - 1 ) ;
465
454
@@ -492,10 +481,9 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
492
481
/// assert_eq!(vector.len(), 0);
493
482
/// ```
494
483
pub fn swap_remove ( & mut self , index : usize ) -> T {
495
- if let Some ( item) = self . try_swap_remove ( index) {
496
- item
497
- } else {
498
- out_of_bounds ! ( "swap_remove" , index, self . len( ) ) ;
484
+ match self . try_swap_remove ( index) {
485
+ Some ( item) => item,
486
+ None => out_of_bounds ! ( "swap_remove" , index, self . len( ) ) ,
499
487
}
500
488
}
501
489
@@ -537,18 +525,16 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
537
525
/// The index must be in bounds.
538
526
pub unsafe fn swap_remove_unchecked ( & mut self , index : usize ) -> T {
539
527
let new_len = self . len ( ) - 1 ;
540
- let p = self . as_mut_ptr ( ) ;
541
- let p_item = p . add ( index) ;
528
+ let ptr_vec_start = self . as_mut_ptr ( ) ;
529
+ let ptr_index = ptr_vec_start . add ( index) ;
542
530
543
531
// Read the item from its pointer.
544
- let item = ptr:: read ( p_item) ;
545
-
546
- // Check `index` isn't the last item's index.
547
- if new_len != index {
548
- // Replace the item at `index` with the last item.
549
- ptr:: copy_nonoverlapping ( p. add ( new_len) , p_item, 1 ) ;
550
- }
551
-
532
+ let item = ptr:: read ( ptr_index) ;
533
+ // Read the last item from its pointer.
534
+ let last_item = ptr:: read ( ptr_vec_start. add ( new_len) ) ;
535
+ // Replace the item at `index` with the last item without calling
536
+ // `drop`.
537
+ ptr:: write ( ptr_index, last_item) ;
552
538
// Resize the vector so that the last item gets ignored.
553
539
self . set_len ( new_len) ;
554
540
0 commit comments