Skip to content

Commit

Permalink
auto merge of rust-lang#17494 : aturon/rust/stabilize-mutable-slices,…
Browse files Browse the repository at this point in the history
… r=alexcrichton

This commit is another in the series of vector slice API stabilization. The focus here is the *mutable* slice API.

Largely, this API inherits the stability attributes [previously assigned](rust-lang#16332) to the analogous methods on immutable slides. It also adds comments to a few `unstable` attributes that were previously missing them.

In addition, the commit adds several `_mut` variants of APIs that were missing:

- `init_mut`
- `head_mut`
- `tail_mut`
- `splitn_mut`
- `rsplitn_mut`

Some of the unsafe APIs -- `unsafe_set`, `init_elem`, and `copy_memory` -- were deprecated in favor of working through `as_mut_ptr`, to simplify the API surface.

Due to deprecations, this is a:

[breaking-change]
  • Loading branch information
bors committed Sep 26, 2014
2 parents aed9cc1 + c59ef66 commit 5d653c1
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 243 deletions.
1 change: 1 addition & 0 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ mod tests {
}

#[test]
#[allow(deprecated)]
fn test_append() {
{
let mut m = DList::new();
Expand Down
84 changes: 80 additions & 4 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,16 @@ mod tests {
assert_eq!(a.as_slice().head().unwrap(), &11);
}

#[test]
fn test_head_mut() {
let mut a = vec![];
assert_eq!(a.as_mut_slice().head_mut(), None);
a = vec![11i];
assert_eq!(*a.as_mut_slice().head_mut().unwrap(), 11);
a = vec![11i, 12];
assert_eq!(*a.as_mut_slice().head_mut().unwrap(), 11);
}

#[test]
fn test_tail() {
let mut a = vec![11i];
Expand All @@ -860,22 +870,39 @@ mod tests {
assert_eq!(a.tail(), b);
}

#[test]
fn test_tail_mut() {
let mut a = vec![11i];
let b: &mut [int] = &mut [];
assert!(a.as_mut_slice().tail_mut() == b);
a = vec![11i, 12];
let b: &mut [int] = &mut [12];
assert!(a.as_mut_slice().tail_mut() == b);
}

#[test]
#[should_fail]
fn test_tail_empty() {
let a: Vec<int> = vec![];
a.tail();
}

#[test]
#[should_fail]
fn test_tail_mut_empty() {
let mut a: Vec<int> = vec![];
a.as_mut_slice().tail_mut();
}

#[test]
#[allow(deprecated)]
fn test_tailn() {
let mut a = vec![11i, 12, 13];
let b: &[int] = &[11, 12, 13];
assert_eq!(a.tailn(0), b);
let b: &mut [int] = &mut [11, 12, 13];
assert!(a.tailn(0) == b);
a = vec![11i, 12, 13];
let b: &[int] = &[13];
assert_eq!(a.tailn(2), b);
let b: &mut [int] = &mut [13];
assert!(a.tailn(2) == b);
}

#[test]
Expand All @@ -896,13 +923,30 @@ mod tests {
assert_eq!(a.init(), b);
}

#[test]
fn test_init_mut() {
let mut a = vec![11i];
let b: &mut [int] = &mut [];
assert!(a.as_mut_slice().init_mut() == b);
a = vec![11i, 12];
let b: &mut [int] = &mut [11];
assert!(a.as_mut_slice().init_mut() == b);
}

#[test]
#[should_fail]
fn test_init_empty() {
let a: Vec<int> = vec![];
a.init();
}

#[test]
#[should_fail]
fn test_init_mut_empty() {
let mut a: Vec<int> = vec![];
a.as_mut_slice().init_mut();
}

#[test]
#[allow(deprecated)]
fn test_initn() {
Expand Down Expand Up @@ -932,6 +976,16 @@ mod tests {
assert_eq!(a.as_slice().last().unwrap(), &12);
}

#[test]
fn test_last_mut() {
let mut a = vec![];
assert_eq!(a.as_mut_slice().last_mut(), None);
a = vec![11i];
assert_eq!(*a.as_mut_slice().last_mut().unwrap(), 11);
a = vec![11i, 12];
assert_eq!(*a.as_mut_slice().last_mut().unwrap(), 12);
}

#[test]
fn test_slice() {
// Test fixed length vector.
Expand Down Expand Up @@ -1077,6 +1131,7 @@ mod tests {
}

#[test]
#[allow(deprecated)]
fn test_grow_set() {
let mut v = vec![1i, 2, 3];
v.grow_set(4u, &4, 5);
Expand Down Expand Up @@ -1610,6 +1665,7 @@ mod tests {

#[test]
#[should_fail]
#[allow(deprecated)]
fn test_copy_memory_oob() {
unsafe {
let mut a = [1i, 2, 3, 4];
Expand Down Expand Up @@ -1793,6 +1849,26 @@ mod tests {
assert_eq!(xs.splitn(1, |x| *x == 5).collect::<Vec<&[int]>>().as_slice(), splits);
}

#[test]
fn test_splitnator_mut() {
let xs = &mut [1i,2,3,4,5];

let splits: &[&mut [int]] = &[&mut [1,2,3,4,5]];
assert_eq!(xs.splitn_mut(0, |x| *x % 2 == 0).collect::<Vec<&mut [int]>>().as_slice(),
splits);
let splits: &[&mut [int]] = &[&mut [1], &mut [3,4,5]];
assert_eq!(xs.splitn_mut(1, |x| *x % 2 == 0).collect::<Vec<&mut [int]>>().as_slice(),
splits);
let splits: &[&mut [int]] = &[&mut [], &mut [], &mut [], &mut [4,5]];
assert_eq!(xs.splitn_mut(3, |_| true).collect::<Vec<&mut [int]>>().as_slice(),
splits);

let xs: &mut [int] = &mut [];
let splits: &[&mut [int]] = &[&mut []];
assert_eq!(xs.splitn_mut(1, |x| *x == 5).collect::<Vec<&mut [int]>>().as_slice(),
splits);
}

#[test]
fn test_rsplitator() {
let xs = &[1i,2,3,4,5];
Expand Down
Loading

0 comments on commit 5d653c1

Please sign in to comment.