@@ -951,6 +951,79 @@ impl<T> Vec<T> {
951
951
self . as_mut_slice ( ) . mut_slice ( start, end)
952
952
}
953
953
954
+ /// Returns a mutable slice of self from `start` to the end of the vec.
955
+ ///
956
+ /// # Failure
957
+ ///
958
+ /// Fails when `start` points outside the bounds of self.
959
+ ///
960
+ /// # Example
961
+ ///
962
+ /// ```rust
963
+ /// let mut vec = vec!(1, 2, 3, 4);
964
+ /// assert!(vec.mut_slice_from(2) == [3, 4]);
965
+ /// ```
966
+ #[ inline]
967
+ pub fn mut_slice_from < ' a > ( & ' a mut self , start : uint ) -> & ' a mut [ T ] {
968
+ self . as_mut_slice ( ) . mut_slice_from ( start)
969
+ }
970
+
971
+ /// Returns a mutable slice of self from the start of the vec to `end`.
972
+ ///
973
+ /// # Failure
974
+ ///
975
+ /// Fails when `end` points outside the bounds of self.
976
+ ///
977
+ /// # Example
978
+ ///
979
+ /// ```rust
980
+ /// let mut vec = vec!(1, 2, 3, 4);
981
+ /// assert!(vec.mut_slice_to(2) == [1, 2]);
982
+ /// ```
983
+ #[ inline]
984
+ pub fn mut_slice_to < ' a > ( & ' a mut self , end : uint ) -> & ' a mut [ T ] {
985
+ self . as_mut_slice ( ) . mut_slice_to ( end)
986
+ }
987
+
988
+ /// Returns a pair of mutable slices that divides the vec at an index.
989
+ ///
990
+ /// The first will contain all indices from `[0, mid)` (excluding
991
+ /// the index `mid` itself) and the second will contain all
992
+ /// indices from `[mid, len)` (excluding the index `len` itself).
993
+ ///
994
+ /// # Failure
995
+ ///
996
+ /// Fails if `mid > len`.
997
+ ///
998
+ /// # Example
999
+ ///
1000
+ /// ```rust
1001
+ /// let mut vec = vec!(1, 2, 3, 4, 5, 6);
1002
+ ///
1003
+ /// // scoped to restrict the lifetime of the borrows
1004
+ /// {
1005
+ /// let (left, right) = vec.mut_split_at(0);
1006
+ /// assert!(left == &mut []);
1007
+ /// assert!(right == &mut [1, 2, 3, 4, 5, 6]);
1008
+ /// }
1009
+ ///
1010
+ /// {
1011
+ /// let (left, right) = vec.mut_split_at(2);
1012
+ /// assert!(left == &mut [1, 2]);
1013
+ /// assert!(right == &mut [3, 4, 5, 6]);
1014
+ /// }
1015
+ ///
1016
+ /// {
1017
+ /// let (left, right) = vec.mut_split_at(6);
1018
+ /// assert!(left == &mut [1, 2, 3, 4, 5, 6]);
1019
+ /// assert!(right == &mut []);
1020
+ /// }
1021
+ /// ```
1022
+ #[ inline]
1023
+ pub fn mut_split_at < ' a > ( & ' a mut self , mid : uint ) -> ( & ' a mut [ T ] , & ' a mut [ T ] ) {
1024
+ self . as_mut_slice ( ) . mut_split_at ( mid)
1025
+ }
1026
+
954
1027
/// Reverse the order of elements in a vector, in place.
955
1028
///
956
1029
/// # Example
@@ -1293,6 +1366,8 @@ mod tests {
1293
1366
use ops:: Drop ;
1294
1367
use option:: { Some , None } ;
1295
1368
use ptr;
1369
+ use container:: Container ;
1370
+ use slice:: { Vector , MutableVector , ImmutableVector } ;
1296
1371
1297
1372
#[ test]
1298
1373
fn test_small_vec_struct ( ) {
@@ -1375,4 +1450,51 @@ mod tests {
1375
1450
1376
1451
assert_eq ! ( v, w) ;
1377
1452
}
1453
+
1454
+ #[ test]
1455
+ fn test_mut_slice_from ( ) {
1456
+ let mut values = Vec :: from_slice ( [ 1u8 , 2 , 3 , 4 , 5 ] ) ;
1457
+ {
1458
+ let slice = values. mut_slice_from ( 2 ) ;
1459
+ assert ! ( slice == [ 3 , 4 , 5 ] ) ;
1460
+ for p in slice. mut_iter ( ) {
1461
+ * p += 2 ;
1462
+ }
1463
+ }
1464
+
1465
+ assert ! ( values. as_slice( ) == [ 1 , 2 , 5 , 6 , 7 ] ) ;
1466
+ }
1467
+
1468
+ #[ test]
1469
+ fn test_mut_slice_to ( ) {
1470
+ let mut values = Vec :: from_slice ( [ 1u8 , 2 , 3 , 4 , 5 ] ) ;
1471
+ {
1472
+ let slice = values. mut_slice_to ( 2 ) ;
1473
+ assert ! ( slice == [ 1 , 2 ] ) ;
1474
+ for p in slice. mut_iter ( ) {
1475
+ * p += 1 ;
1476
+ }
1477
+ }
1478
+
1479
+ assert ! ( values. as_slice( ) == [ 2 , 3 , 3 , 4 , 5 ] ) ;
1480
+ }
1481
+
1482
+ #[ test]
1483
+ fn test_mut_split_at ( ) {
1484
+ let mut values = Vec :: from_slice ( [ 1u8 , 2 , 3 , 4 , 5 ] ) ;
1485
+ {
1486
+ let ( left, right) = values. mut_split_at ( 2 ) ;
1487
+ assert ! ( left. slice( 0 , left. len( ) ) == [ 1 , 2 ] ) ;
1488
+ for p in left. mut_iter ( ) {
1489
+ * p += 1 ;
1490
+ }
1491
+
1492
+ assert ! ( right. slice( 0 , right. len( ) ) == [ 3 , 4 , 5 ] ) ;
1493
+ for p in right. mut_iter ( ) {
1494
+ * p += 2 ;
1495
+ }
1496
+ }
1497
+
1498
+ assert ! ( values == Vec :: from_slice( [ 2u8 , 3 , 5 , 6 , 7 ] ) ) ;
1499
+ }
1378
1500
}
0 commit comments