Skip to content

Commit

Permalink
Add Vec::mut_slice_from(), mut_slice_to(), and mut_split_at()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kroisse committed Mar 22, 2014
1 parent e233a43 commit 5b03050
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,79 @@ impl<T> Vec<T> {
self.as_mut_slice().mut_slice(start, end)
}

/// Returns a mutable slice of self from `start` to the end of the vec.
///
/// # Failure
///
/// Fails when `start` points outside the bounds of self.
///
/// # Example
///
/// ```rust
/// let mut vec = vec!(1, 2, 3, 4);
/// assert!(vec.mut_slice_from(2) == [3, 4]);
/// ```
#[inline]
pub fn mut_slice_from<'a>(&'a mut self, start: uint) -> &'a mut [T] {
self.as_mut_slice().mut_slice_from(start)
}

/// Returns a mutable slice of self from the start of the vec to `end`.
///
/// # Failure
///
/// Fails when `end` points outside the bounds of self.
///
/// # Example
///
/// ```rust
/// let mut vec = vec!(1, 2, 3, 4);
/// assert!(vec.mut_slice_to(2) == [1, 2]);
/// ```
#[inline]
pub fn mut_slice_to<'a>(&'a mut self, end: uint) -> &'a mut [T] {
self.as_mut_slice().mut_slice_to(end)
}

/// Returns a pair of mutable slices that divides the vec at an index.
///
/// The first will contain all indices from `[0, mid)` (excluding
/// the index `mid` itself) and the second will contain all
/// indices from `[mid, len)` (excluding the index `len` itself).
///
/// # Failure
///
/// Fails if `mid > len`.
///
/// # Example
///
/// ```rust
/// let mut vec = vec!(1, 2, 3, 4, 5, 6);
///
/// // scoped to restrict the lifetime of the borrows
/// {
/// let (left, right) = vec.mut_split_at(0);
/// assert!(left == &mut []);
/// assert!(right == &mut [1, 2, 3, 4, 5, 6]);
/// }
///
/// {
/// let (left, right) = vec.mut_split_at(2);
/// assert!(left == &mut [1, 2]);
/// assert!(right == &mut [3, 4, 5, 6]);
/// }
///
/// {
/// let (left, right) = vec.mut_split_at(6);
/// assert!(left == &mut [1, 2, 3, 4, 5, 6]);
/// assert!(right == &mut []);
/// }
/// ```
#[inline]
pub fn mut_split_at<'a>(&'a mut self, mid: uint) -> (&'a mut [T], &'a mut [T]) {
self.as_mut_slice().mut_split_at(mid)
}

/// Reverse the order of elements in a vector, in place.
///
/// # Example
Expand Down Expand Up @@ -1293,6 +1366,8 @@ mod tests {
use ops::Drop;
use option::{Some, None};
use ptr;
use container::Container;
use slice::{Vector, MutableVector, ImmutableVector};

#[test]
fn test_small_vec_struct() {
Expand Down Expand Up @@ -1375,4 +1450,51 @@ mod tests {

assert_eq!(v, w);
}

#[test]
fn test_mut_slice_from() {
let mut values = Vec::from_slice([1u8,2,3,4,5]);
{
let slice = values.mut_slice_from(2);
assert!(slice == [3, 4, 5]);
for p in slice.mut_iter() {
*p += 2;
}
}

assert!(values.as_slice() == [1, 2, 5, 6, 7]);
}

#[test]
fn test_mut_slice_to() {
let mut values = Vec::from_slice([1u8,2,3,4,5]);
{
let slice = values.mut_slice_to(2);
assert!(slice == [1, 2]);
for p in slice.mut_iter() {
*p += 1;
}
}

assert!(values.as_slice() == [2, 3, 3, 4, 5]);
}

#[test]
fn test_mut_split_at() {
let mut values = Vec::from_slice([1u8,2,3,4,5]);
{
let (left, right) = values.mut_split_at(2);
assert!(left.slice(0, left.len()) == [1, 2]);
for p in left.mut_iter() {
*p += 1;
}

assert!(right.slice(0, right.len()) == [3, 4, 5]);
for p in right.mut_iter() {
*p += 2;
}
}

assert!(values == Vec::from_slice([2u8, 3, 5, 6, 7]));
}
}

5 comments on commit 5b03050

@bors
Copy link
Contributor

@bors bors commented on 5b03050 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Kroisse@5b03050

@bors
Copy link
Contributor

@bors bors commented on 5b03050 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Kroisse/rust/vec_ng_mut_slices = 5b03050 into auto

@bors
Copy link
Contributor

@bors bors commented on 5b03050 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kroisse/rust/vec_ng_mut_slices = 5b03050 merged ok, testing candidate = 0e6f90e

@bors
Copy link
Contributor

@bors bors commented on 5b03050 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 5b03050 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 0e6f90e

Please sign in to comment.