Skip to content

Commit 5d653c1

Browse files
committed
auto merge of #17494 : aturon/rust/stabilize-mutable-slices, 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](#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]
2 parents aed9cc1 + c59ef66 commit 5d653c1

File tree

3 files changed

+348
-243
lines changed

3 files changed

+348
-243
lines changed

src/libcollections/dlist.rs

+1
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ mod tests {
844844
}
845845

846846
#[test]
847+
#[allow(deprecated)]
847848
fn test_append() {
848849
{
849850
let mut m = DList::new();

src/libcollections/slice.rs

+80-4
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,16 @@ mod tests {
850850
assert_eq!(a.as_slice().head().unwrap(), &11);
851851
}
852852

853+
#[test]
854+
fn test_head_mut() {
855+
let mut a = vec![];
856+
assert_eq!(a.as_mut_slice().head_mut(), None);
857+
a = vec![11i];
858+
assert_eq!(*a.as_mut_slice().head_mut().unwrap(), 11);
859+
a = vec![11i, 12];
860+
assert_eq!(*a.as_mut_slice().head_mut().unwrap(), 11);
861+
}
862+
853863
#[test]
854864
fn test_tail() {
855865
let mut a = vec![11i];
@@ -860,22 +870,39 @@ mod tests {
860870
assert_eq!(a.tail(), b);
861871
}
862872

873+
#[test]
874+
fn test_tail_mut() {
875+
let mut a = vec![11i];
876+
let b: &mut [int] = &mut [];
877+
assert!(a.as_mut_slice().tail_mut() == b);
878+
a = vec![11i, 12];
879+
let b: &mut [int] = &mut [12];
880+
assert!(a.as_mut_slice().tail_mut() == b);
881+
}
882+
863883
#[test]
864884
#[should_fail]
865885
fn test_tail_empty() {
866886
let a: Vec<int> = vec![];
867887
a.tail();
868888
}
869889

890+
#[test]
891+
#[should_fail]
892+
fn test_tail_mut_empty() {
893+
let mut a: Vec<int> = vec![];
894+
a.as_mut_slice().tail_mut();
895+
}
896+
870897
#[test]
871898
#[allow(deprecated)]
872899
fn test_tailn() {
873900
let mut a = vec![11i, 12, 13];
874-
let b: &[int] = &[11, 12, 13];
875-
assert_eq!(a.tailn(0), b);
901+
let b: &mut [int] = &mut [11, 12, 13];
902+
assert!(a.tailn(0) == b);
876903
a = vec![11i, 12, 13];
877-
let b: &[int] = &[13];
878-
assert_eq!(a.tailn(2), b);
904+
let b: &mut [int] = &mut [13];
905+
assert!(a.tailn(2) == b);
879906
}
880907

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

926+
#[test]
927+
fn test_init_mut() {
928+
let mut a = vec![11i];
929+
let b: &mut [int] = &mut [];
930+
assert!(a.as_mut_slice().init_mut() == b);
931+
a = vec![11i, 12];
932+
let b: &mut [int] = &mut [11];
933+
assert!(a.as_mut_slice().init_mut() == b);
934+
}
935+
899936
#[test]
900937
#[should_fail]
901938
fn test_init_empty() {
902939
let a: Vec<int> = vec![];
903940
a.init();
904941
}
905942

943+
#[test]
944+
#[should_fail]
945+
fn test_init_mut_empty() {
946+
let mut a: Vec<int> = vec![];
947+
a.as_mut_slice().init_mut();
948+
}
949+
906950
#[test]
907951
#[allow(deprecated)]
908952
fn test_initn() {
@@ -932,6 +976,16 @@ mod tests {
932976
assert_eq!(a.as_slice().last().unwrap(), &12);
933977
}
934978

979+
#[test]
980+
fn test_last_mut() {
981+
let mut a = vec![];
982+
assert_eq!(a.as_mut_slice().last_mut(), None);
983+
a = vec![11i];
984+
assert_eq!(*a.as_mut_slice().last_mut().unwrap(), 11);
985+
a = vec![11i, 12];
986+
assert_eq!(*a.as_mut_slice().last_mut().unwrap(), 12);
987+
}
988+
935989
#[test]
936990
fn test_slice() {
937991
// Test fixed length vector.
@@ -1077,6 +1131,7 @@ mod tests {
10771131
}
10781132

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

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

1852+
#[test]
1853+
fn test_splitnator_mut() {
1854+
let xs = &mut [1i,2,3,4,5];
1855+
1856+
let splits: &[&mut [int]] = &[&mut [1,2,3,4,5]];
1857+
assert_eq!(xs.splitn_mut(0, |x| *x % 2 == 0).collect::<Vec<&mut [int]>>().as_slice(),
1858+
splits);
1859+
let splits: &[&mut [int]] = &[&mut [1], &mut [3,4,5]];
1860+
assert_eq!(xs.splitn_mut(1, |x| *x % 2 == 0).collect::<Vec<&mut [int]>>().as_slice(),
1861+
splits);
1862+
let splits: &[&mut [int]] = &[&mut [], &mut [], &mut [], &mut [4,5]];
1863+
assert_eq!(xs.splitn_mut(3, |_| true).collect::<Vec<&mut [int]>>().as_slice(),
1864+
splits);
1865+
1866+
let xs: &mut [int] = &mut [];
1867+
let splits: &[&mut [int]] = &[&mut []];
1868+
assert_eq!(xs.splitn_mut(1, |x| *x == 5).collect::<Vec<&mut [int]>>().as_slice(),
1869+
splits);
1870+
}
1871+
17961872
#[test]
17971873
fn test_rsplitator() {
17981874
let xs = &[1i,2,3,4,5];

0 commit comments

Comments
 (0)