Skip to content

Commit 9239d69

Browse files
committed
auto merge of #7691 : nikomatsakis/rust/vec-split-method, r=thestinger
I don't think we have this yet. This makes `&mut [T]` much more flexible. r? @strcat (or whomever)
2 parents 4957414 + 9ee5ce2 commit 9239d69

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/libstd/cast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use sys;
1414
use unstable::intrinsics;
1515

16-
/// Casts the value at `src` to U. The two types must have the same length.
1716
/// Casts the value at `src` to U. The two types must have the same length.
1817
#[cfg(target_word_size = "32")]
1918
#[inline]

src/libstd/vec.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,15 @@ pub trait MutableVector<'self, T> {
16711671

16721672
fn swap(self, a: uint, b: uint);
16731673

1674+
/**
1675+
* Divides one `&mut` into two. The first will
1676+
* contain all indices from `0..mid` (excluding the index `mid`
1677+
* itself) and the second will contain all indices from
1678+
* `mid..len` (excluding the index `len` itself).
1679+
*/
1680+
fn mut_split(self, mid: uint) -> (&'self mut [T],
1681+
&'self mut [T]);
1682+
16741683
fn reverse(self);
16751684

16761685
/**
@@ -1708,6 +1717,15 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
17081717
}
17091718
}
17101719

1720+
#[inline]
1721+
fn mut_split(self, mid: uint) -> (&'self mut [T], &'self mut [T]) {
1722+
unsafe {
1723+
let len = self.len();
1724+
let self2: &'self mut [T] = cast::transmute_copy(&self);
1725+
(self.mut_slice(0, mid), self2.mut_slice(mid, len))
1726+
}
1727+
}
1728+
17111729
#[inline]
17121730
fn mut_iter(self) -> VecMutIterator<'self, T> {
17131731
unsafe {
@@ -3355,4 +3373,23 @@ mod tests {
33553373
v.push(1);
33563374
v.push(2);
33573375
}
3376+
3377+
#[test]
3378+
fn test_mut_split() {
3379+
let mut values = [1u8,2,3,4,5];
3380+
{
3381+
let (left, right) = values.mut_split(2);
3382+
assert_eq!(left.slice(0, left.len()), [1, 2]);
3383+
for left.mut_iter().advance |p| {
3384+
*p += 1;
3385+
}
3386+
3387+
assert_eq!(right.slice(0, right.len()), [3, 4, 5]);
3388+
for right.mut_iter().advance |p| {
3389+
*p += 2;
3390+
}
3391+
}
3392+
3393+
assert_eq!(values, [2, 3, 5, 6, 7]);
3394+
}
33583395
}

0 commit comments

Comments
 (0)