@@ -29,7 +29,6 @@ extern mod rustrt {
29
29
#[ abi = "rust-intrinsic" ]
30
30
extern mod rusti {
31
31
fn move_val_init < T > ( dst : & mut T , -src : T ) ;
32
- fn init < T > ( ) -> T ;
33
32
}
34
33
35
34
@@ -399,51 +398,28 @@ pub fn rsplitn<T: Copy>(v: &[T], n: uint, f: fn(t: &T) -> bool) -> ~[~[T]] {
399
398
// Mutators
400
399
401
400
/// Removes the first element from a vector and return it
402
- pub fn shift < T > ( v : & mut ~[ T ] ) -> T unsafe {
403
-
404
- assert v. is_not_empty ( ) ;
405
-
406
- if v. len ( ) == 1 { return v. pop ( ) }
407
-
408
- if v. len ( ) == 2 {
409
- let last = v. pop ( ) ;
410
- let first = v. pop ( ) ;
411
- v. push ( last) ;
412
- return first;
413
- }
414
-
401
+ pub fn shift < T > ( v : & mut ~[ T ] ) -> T {
415
402
let ln = v. len ( ) ;
416
- let next_ln = v. len ( ) - 1 ;
417
-
418
- // Save the last element. We're going to overwrite its position
419
- let mut work_elt = v. pop ( ) ;
420
- // We still should have room to work where what last element was
421
- assert capacity( v) >= ln;
422
- // Pretend like we have the original length so we can use
423
- // the vector memcpy to overwrite the hole we just made
424
- raw:: set_len ( v, ln) ;
425
-
426
- // Memcopy the head element (the one we want) to the location we just
427
- // popped. For the moment it unsafely exists at both the head and last
428
- // positions
429
- let first_slice = view ( * v, 0 , 1 ) ;
430
- let last_slice = mut_view ( * v, next_ln, ln) ;
431
- raw:: memcpy ( last_slice, first_slice, 1 ) ;
403
+ assert ( ln > 0 ) ;
432
404
433
- // Memcopy everything to the left one element
434
- let init_slice = mut_view ( * v, 0 , next_ln) ;
435
- let tail_slice = view ( * v, 1 , ln) ;
436
- raw:: memcpy ( init_slice, tail_slice, next_ln) ;
437
-
438
- // Set the new length. Now the vector is back to normal
439
- raw:: set_len ( v, next_ln) ;
405
+ let mut vv = ~[ ] ;
406
+ * v <-> vv;
440
407
441
- // Swap out the element we want from the end
442
- let vp = raw:: to_mut_ptr ( * v) ;
443
- let vp = ptr:: mut_offset ( vp, next_ln - 1 ) ;
444
- * vp <-> work_elt;
408
+ unsafe {
409
+ let mut rr;
410
+ {
411
+ let vv = raw:: to_ptr ( vv) ;
412
+ rr = move * vv;
413
+
414
+ for uint:: range( 1 , ln) |i| {
415
+ let r = move * ptr:: offset ( vv, i) ;
416
+ v. push ( r) ;
417
+ }
418
+ }
419
+ raw:: set_len ( & mut vv, 0 ) ;
445
420
446
- return work_elt;
421
+ rr
422
+ }
447
423
}
448
424
449
425
/// Prepend an element to the vector
@@ -484,15 +460,9 @@ pub fn remove<T>(v: &mut ~[T], i: uint) -> T {
484
460
pub fn consume< T > ( v : ~[ T ] , f : fn ( uint , v : T ) ) unsafe {
485
461
let mut v = v; // FIXME(#3488)
486
462
487
- do as_mut_buf ( v) |p, ln| {
463
+ do as_imm_buf ( v) |p, ln| {
488
464
for uint:: range( 0 , ln) |i| {
489
- // NB: This unsafe operation counts on init writing 0s to the
490
- // holes we create in the vector. That ensures that, if the
491
- // iterator fails then we won't try to clean up the consumed
492
- // elements during unwinding
493
- let mut x = rusti:: init ( ) ;
494
- let p = ptr:: mut_offset ( p, i) ;
495
- x <-> * p;
465
+ let x = move * ptr:: offset ( p, i) ;
496
466
f ( i, x) ;
497
467
}
498
468
}
@@ -512,9 +482,7 @@ pub fn pop<T>(v: &mut ~[T]) -> T {
512
482
}
513
483
let valptr = ptr:: to_mut_unsafe_ptr ( & mut v[ ln - 1 u] ) ;
514
484
unsafe {
515
- // XXX: Should be rusti::uninit() - we don't need this zeroed
516
- let mut val = rusti:: init ( ) ;
517
- val <-> * valptr;
485
+ let val = move * valptr;
518
486
raw:: set_len ( v, ln - 1 u) ;
519
487
val
520
488
}
@@ -583,11 +551,9 @@ pub fn push_all_move<T>(v: &mut ~[T], rhs: ~[T]) {
583
551
let mut rhs = rhs; // FIXME(#3488)
584
552
reserve ( v, v. len ( ) + rhs. len ( ) ) ;
585
553
unsafe {
586
- do as_mut_buf ( rhs) |p, len| {
554
+ do as_imm_buf ( rhs) |p, len| {
587
555
for uint:: range( 0 , len) |i| {
588
- // XXX Should be rusti::uninit() - don't need to zero
589
- let mut x = rusti:: init ( ) ;
590
- x <-> * ptr:: mut_offset ( p, i) ;
556
+ let x = move * ptr:: offset ( p, i) ;
591
557
push ( v, x) ;
592
558
}
593
559
}
@@ -597,14 +563,12 @@ pub fn push_all_move<T>(v: &mut ~[T], rhs: ~[T]) {
597
563
598
564
/// Shorten a vector, dropping excess elements.
599
565
pub fn truncate < T > ( v : & mut ~[ T ] , newlen : uint ) {
600
- do as_mut_buf ( * v) |p, oldlen| {
566
+ do as_imm_buf ( * v) |p, oldlen| {
601
567
assert ( newlen <= oldlen) ;
602
568
unsafe {
603
569
// This loop is optimized out for non-drop types.
604
570
for uint:: range( newlen, oldlen) |i| {
605
- // XXX Should be rusti::uninit() - don't need to zero
606
- let mut dropped = rusti:: init ( ) ;
607
- dropped <-> * ptr:: mut_offset ( p, i) ;
571
+ let _dropped = move * ptr:: offset ( p, i) ;
608
572
}
609
573
raw:: set_len ( v, newlen) ;
610
574
}
@@ -627,14 +591,12 @@ pub fn dedup<T: Eq>(v: &mut ~[T]) unsafe {
627
591
// last_written < next_to_read < ln
628
592
if * ptr:: mut_offset ( p, next_to_read) ==
629
593
* ptr:: mut_offset ( p, last_written) {
630
- // XXX Should be rusti::uninit() - don't need to zero
631
- let mut dropped = rusti:: init ( ) ;
632
- dropped <-> * ptr:: mut_offset ( p, next_to_read) ;
594
+ let _dropped = move * ptr:: mut_offset ( p, next_to_read) ;
633
595
} else {
634
596
last_written += 1 ;
635
597
// last_written <= next_to_read < ln
636
598
if next_to_read != last_written {
637
- * ptr:: mut_offset ( p, last_written) <->
599
+ * ptr:: mut_offset ( p, last_written) = move
638
600
* ptr:: mut_offset ( p, next_to_read) ;
639
601
}
640
602
}
@@ -1798,7 +1760,7 @@ pub struct UnboxedVecRepr {
1798
1760
}
1799
1761
1800
1762
/// Unsafe operations
1801
- pub mod raw {
1763
+ mod raw {
1802
1764
1803
1765
/// The internal representation of a (boxed) vector
1804
1766
pub struct VecRepr {
0 commit comments