File tree 2 files changed +9
-3
lines changed
2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -1378,6 +1378,11 @@ fn test_stable_pointers() {
1378
1378
v. remove ( 1 ) ;
1379
1379
v. pop ( ) . unwrap ( ) ;
1380
1380
assert_eq ! ( * v0, 13 ) ;
1381
+ v. push ( 1 ) ;
1382
+ v. swap_remove ( 1 ) ;
1383
+ assert_eq ! ( v. len( ) , 2 ) ;
1384
+ v. swap_remove ( 1 ) ; // swap_remove the last element
1385
+ assert_eq ! ( * v0, 13 ) ;
1381
1386
1382
1387
// Appending
1383
1388
v. append ( & mut vec ! [ 27 , 19 ] ) ;
Original file line number Diff line number Diff line change @@ -963,12 +963,13 @@ impl<T> Vec<T> {
963
963
#[ inline]
964
964
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
965
965
pub fn swap_remove ( & mut self , index : usize ) -> T {
966
+ assert ! ( index < self . len) ;
966
967
unsafe {
967
968
// We replace self[index] with the last element. Note that if the
968
- // bounds check on hole succeeds there must be a last element (which
969
+ // bounds check above succeeds there must be a last element (which
969
970
// can be self[index] itself).
970
- let hole : * mut T = & mut self [ index ] ;
971
- let last = ptr :: read ( self . get_unchecked ( self . len - 1 ) ) ;
971
+ let last = ptr :: read ( self . as_ptr ( ) . add ( self . len - 1 ) ) ;
972
+ let hole : * mut T = self . as_mut_ptr ( ) . add ( index ) ;
972
973
self . len -= 1 ;
973
974
ptr:: replace ( hole, last)
974
975
}
You can’t perform that action at this time.
0 commit comments