File tree 1 file changed +29
-0
lines changed
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -398,6 +398,11 @@ impl<T> Vec<T> {
398
398
self . insert ( 0 , element)
399
399
}
400
400
401
+ #[ inline]
402
+ pub fn shift ( & mut self ) -> Option < T > {
403
+ self . remove ( 0 )
404
+ }
405
+
401
406
pub fn insert ( & mut self , index : uint , element : T ) {
402
407
let len = self . len ( ) ;
403
408
assert ! ( index <= len) ;
@@ -420,6 +425,30 @@ impl<T> Vec<T> {
420
425
}
421
426
}
422
427
428
+ fn remove ( & mut self , index : uint ) -> Option < T > {
429
+ let len = self . len ( ) ;
430
+ if index < len {
431
+ unsafe { // infallible
432
+ let ret;
433
+ {
434
+ let slice = self . as_mut_slice ( ) ;
435
+ // the place we are taking from.
436
+ let ptr = slice. as_mut_ptr ( ) . offset ( index as int ) ;
437
+ // copy it out, unsafely having a copy of the value on
438
+ // the stack and in the vector at the same time.
439
+ ret = Some ( ptr:: read ( ptr as * T ) ) ;
440
+
441
+ // Shift everything down to fill in that spot.
442
+ ptr:: copy_memory ( ptr, & * ptr. offset ( 1 ) , len - index - 1 ) ;
443
+ }
444
+ self . set_len ( len - 1 ) ;
445
+ ret
446
+ }
447
+ } else {
448
+ None
449
+ }
450
+ }
451
+
423
452
#[ inline]
424
453
pub fn rev_iter < ' a > ( & ' a self ) -> RevItems < ' a , T > {
425
454
self . as_slice ( ) . rev_iter ( )
You can’t perform that action at this time.
0 commit comments