@@ -689,6 +689,8 @@ impl<'self,T:Clone> CopyableVector<T> for &'self [T] {
689
689
#[ allow( missing_doc) ]
690
690
pub trait ImmutableVector < ' self , T > {
691
691
fn slice( & self , start: uint, end: uint) -> & ' self [ T ] ;
692
+ fn slice_from( & self , start: uint) -> & ' self [ T ] ;
693
+ fn slice_to( & self , end: uint) -> & ' self [ T ] ;
692
694
fn iter( self ) -> VecIterator < ' self , T > ;
693
695
fn rev_iter( self ) -> VecRevIterator < ' self , T > ;
694
696
fn split_iter( self , pred: & ' self fn ( & T ) -> bool) -> VecSplitIterator < ' self , T > ;
@@ -720,11 +722,17 @@ pub trait ImmutableVector<'self, T> {
720
722
721
723
/// Extension methods for vectors
722
724
impl <' self , T > ImmutableVector < ' self , T > for & ' self [ T ] {
723
- /// Return a slice that points into another slice.
725
+
726
+ /**
727
+ * Returns a slice of self between `start` and `end`.
728
+ *
729
+ * Fails when `start` or `end` point outside the bounds of self,
730
+ * or when `start` > `end`.
731
+ */
724
732
#[ inline]
725
733
fn slice( & self , start: uint, end: uint) -> & ' self [ T ] {
726
- assert ! ( start <= end) ;
727
- assert ! ( end <= self . len( ) ) ;
734
+ assert ! ( start <= end) ;
735
+ assert ! ( end <= self . len( ) ) ;
728
736
do self. as_imm_buf |p, _len| {
729
737
unsafe {
730
738
transmute( ( ptr:: offset( p, start) ,
@@ -733,6 +741,26 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
733
741
}
734
742
}
735
743
744
+ /**
745
+ * Returns a slice of self from `start` to the end of the vec.
746
+ *
747
+ * Fails when `start` points outside the bounds of self.
748
+ */
749
+ #[ inline]
750
+ fn slice_from( & self , start: uint) -> & ' self [ T ] {
751
+ self . slice( start, self . len( ) )
752
+ }
753
+
754
+ /**
755
+ * Returns a slice of self from the start of the vec to `end`.
756
+ *
757
+ * Fails when `end` points outside the bounds of self.
758
+ */
759
+ #[ inline]
760
+ fn slice_to( & self , end: uint) -> & ' self [ T ] {
761
+ self . slice( 0 , end)
762
+ }
763
+
736
764
#[ inline]
737
765
fn iter ( self ) -> VecIterator < ' self , T > {
738
766
unsafe {
@@ -2453,6 +2481,22 @@ mod tests {
2453
2481
assert_eq!( v_d[ 4 ] , 6 ) ;
2454
2482
}
2455
2483
2484
+ #[ test]
2485
+ fn test_slice_from( ) {
2486
+ let vec = & [ 1 , 2 , 3 , 4 ] ;
2487
+ assert_eq!( vec. slice_from( 0 ) , vec) ;
2488
+ assert_eq!( vec. slice_from( 2 ) , & [ 3 , 4 ] ) ;
2489
+ assert_eq!( vec. slice_from( 4 ) , & [ ] ) ;
2490
+ }
2491
+
2492
+ #[ test]
2493
+ fn test_slice_to( ) {
2494
+ let vec = & [ 1 , 2 , 3 , 4 ] ;
2495
+ assert_eq!( vec. slice_to( 4 ) , vec) ;
2496
+ assert_eq!( vec. slice_to( 2 ) , & [ 1 , 2 ] ) ;
2497
+ assert_eq!( vec. slice_to( 0 ) , & [ ] ) ;
2498
+ }
2499
+
2456
2500
#[ test]
2457
2501
fn test_pop( ) {
2458
2502
// Test on-heap pop.
0 commit comments