Skip to content

Commit 01ac5a9

Browse files
committed
Stabilize slice_select_nth_unstable
This stabilizes the functionality in slice_partition_at_index, but under the names `select_nth_unstable*`. The functions `partition_at_index*` are left as deprecated, to be removed in a later release. Closes #55300
1 parent 576e227 commit 01ac5a9

File tree

2 files changed

+69
-31
lines changed

2 files changed

+69
-31
lines changed

library/core/src/slice/mod.rs

+53-15
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,50 @@ impl<T> [T] {
20342034
sort::quicksort(self, |a, b| f(a).lt(&f(b)));
20352035
}
20362036

2037+
/// Reorder the slice such that the element at `index` is at its final sorted position.
2038+
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
2039+
#[rustc_deprecated(since = "1.49.0", reason = "use the select_nth_unstable() instead")]
2040+
#[inline]
2041+
pub fn partition_at_index(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
2042+
where
2043+
T: Ord,
2044+
{
2045+
self.select_nth_unstable(index)
2046+
}
2047+
2048+
/// Reorder the slice with a comparator function such that the element at `index` is at its
2049+
/// final sorted position.
2050+
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
2051+
#[rustc_deprecated(since = "1.49.0", reason = "use select_nth_unstable_by() instead")]
2052+
#[inline]
2053+
pub fn partition_at_index_by<F>(
2054+
&mut self,
2055+
index: usize,
2056+
compare: F,
2057+
) -> (&mut [T], &mut T, &mut [T])
2058+
where
2059+
F: FnMut(&T, &T) -> Ordering,
2060+
{
2061+
self.select_nth_unstable_by(index, compare)
2062+
}
2063+
2064+
/// Reorder the slice with a key extraction function such that the element at `index` is at its
2065+
/// final sorted position.
2066+
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
2067+
#[rustc_deprecated(since = "1.49.0", reason = "use the select_nth_unstable_by_key() instead")]
2068+
#[inline]
2069+
pub fn partition_at_index_by_key<K, F>(
2070+
&mut self,
2071+
index: usize,
2072+
f: F,
2073+
) -> (&mut [T], &mut T, &mut [T])
2074+
where
2075+
F: FnMut(&T) -> K,
2076+
K: Ord,
2077+
{
2078+
self.select_nth_unstable_by_key(index, f)
2079+
}
2080+
20372081
/// Reorder the slice such that the element at `index` is at its final sorted position.
20382082
///
20392083
/// This reordering has the additional property that any value at position `i < index` will be
@@ -2058,12 +2102,10 @@ impl<T> [T] {
20582102
/// # Examples
20592103
///
20602104
/// ```
2061-
/// #![feature(slice_partition_at_index)]
2062-
///
20632105
/// let mut v = [-5i32, 4, 1, -3, 2];
20642106
///
20652107
/// // Find the median
2066-
/// v.partition_at_index(2);
2108+
/// v.select_nth_unstable(2);
20672109
///
20682110
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
20692111
/// // about the specified index.
@@ -2072,9 +2114,9 @@ impl<T> [T] {
20722114
/// v == [-3, -5, 1, 4, 2] ||
20732115
/// v == [-5, -3, 1, 4, 2]);
20742116
/// ```
2075-
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
2117+
#[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
20762118
#[inline]
2077-
pub fn partition_at_index(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
2119+
pub fn select_nth_unstable(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
20782120
where
20792121
T: Ord,
20802122
{
@@ -2108,12 +2150,10 @@ impl<T> [T] {
21082150
/// # Examples
21092151
///
21102152
/// ```
2111-
/// #![feature(slice_partition_at_index)]
2112-
///
21132153
/// let mut v = [-5i32, 4, 1, -3, 2];
21142154
///
21152155
/// // Find the median as if the slice were sorted in descending order.
2116-
/// v.partition_at_index_by(2, |a, b| b.cmp(a));
2156+
/// v.select_nth_unstable_by(2, |a, b| b.cmp(a));
21172157
///
21182158
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
21192159
/// // about the specified index.
@@ -2122,9 +2162,9 @@ impl<T> [T] {
21222162
/// v == [4, 2, 1, -5, -3] ||
21232163
/// v == [4, 2, 1, -3, -5]);
21242164
/// ```
2125-
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
2165+
#[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
21262166
#[inline]
2127-
pub fn partition_at_index_by<F>(
2167+
pub fn select_nth_unstable_by<F>(
21282168
&mut self,
21292169
index: usize,
21302170
mut compare: F,
@@ -2162,12 +2202,10 @@ impl<T> [T] {
21622202
/// # Examples
21632203
///
21642204
/// ```
2165-
/// #![feature(slice_partition_at_index)]
2166-
///
21672205
/// let mut v = [-5i32, 4, 1, -3, 2];
21682206
///
21692207
/// // Return the median as if the array were sorted according to absolute value.
2170-
/// v.partition_at_index_by_key(2, |a| a.abs());
2208+
/// v.select_nth_unstable_by_key(2, |a| a.abs());
21712209
///
21722210
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
21732211
/// // about the specified index.
@@ -2176,9 +2214,9 @@ impl<T> [T] {
21762214
/// v == [2, 1, -3, 4, -5] ||
21772215
/// v == [2, 1, -3, -5, 4]);
21782216
/// ```
2179-
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
2217+
#[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
21802218
#[inline]
2181-
pub fn partition_at_index_by_key<K, F>(
2219+
pub fn select_nth_unstable_by_key<K, F>(
21822220
&mut self,
21832221
index: usize,
21842222
mut f: F,

library/core/tests/slice.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ fn sort_unstable() {
15711571
#[test]
15721572
#[cfg(not(target_arch = "wasm32"))]
15731573
#[cfg_attr(miri, ignore)] // Miri is too slow
1574-
fn partition_at_index() {
1574+
fn select_nth_unstable() {
15751575
use core::cmp::Ordering::{Equal, Greater, Less};
15761576
use rand::rngs::StdRng;
15771577
use rand::seq::SliceRandom;
@@ -1597,7 +1597,7 @@ fn partition_at_index() {
15971597
// Sort in default order.
15981598
for pivot in 0..len {
15991599
let mut v = orig.clone();
1600-
v.partition_at_index(pivot);
1600+
v.select_nth_unstable(pivot);
16011601

16021602
assert_eq!(v_sorted[pivot], v[pivot]);
16031603
for i in 0..pivot {
@@ -1610,7 +1610,7 @@ fn partition_at_index() {
16101610
// Sort in ascending order.
16111611
for pivot in 0..len {
16121612
let mut v = orig.clone();
1613-
let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
1613+
let (left, pivot, right) = v.select_nth_unstable_by(pivot, |a, b| a.cmp(b));
16141614

16151615
assert_eq!(left.len() + right.len(), len - 1);
16161616

@@ -1633,7 +1633,7 @@ fn partition_at_index() {
16331633

16341634
for pivot in 0..len {
16351635
let mut v = orig.clone();
1636-
v.partition_at_index_by(pivot, sort_descending_comparator);
1636+
v.select_nth_unstable_by(pivot, sort_descending_comparator);
16371637

16381638
assert_eq!(v_sorted_descending[pivot], v[pivot]);
16391639
for i in 0..pivot {
@@ -1654,36 +1654,36 @@ fn partition_at_index() {
16541654
}
16551655

16561656
for pivot in 0..v.len() {
1657-
v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1657+
v.select_nth_unstable_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
16581658
v.sort();
16591659
for i in 0..v.len() {
16601660
assert_eq!(v[i], i as i32);
16611661
}
16621662
}
16631663

16641664
// Should not panic.
1665-
[(); 10].partition_at_index(0);
1666-
[(); 10].partition_at_index(5);
1667-
[(); 10].partition_at_index(9);
1668-
[(); 100].partition_at_index(0);
1669-
[(); 100].partition_at_index(50);
1670-
[(); 100].partition_at_index(99);
1665+
[(); 10].select_nth_unstable(0);
1666+
[(); 10].select_nth_unstable(5);
1667+
[(); 10].select_nth_unstable(9);
1668+
[(); 100].select_nth_unstable(0);
1669+
[(); 100].select_nth_unstable(50);
1670+
[(); 100].select_nth_unstable(99);
16711671

16721672
let mut v = [0xDEADBEEFu64];
1673-
v.partition_at_index(0);
1673+
v.select_nth_unstable(0);
16741674
assert!(v == [0xDEADBEEF]);
16751675
}
16761676

16771677
#[test]
16781678
#[should_panic(expected = "index 0 greater than length of slice")]
1679-
fn partition_at_index_zero_length() {
1680-
[0i32; 0].partition_at_index(0);
1679+
fn select_nth_unstable_zero_length() {
1680+
[0i32; 0].select_nth_unstable(0);
16811681
}
16821682

16831683
#[test]
16841684
#[should_panic(expected = "index 20 greater than length of slice")]
1685-
fn partition_at_index_past_length() {
1686-
[0i32; 10].partition_at_index(20);
1685+
fn select_nth_unstable_past_length() {
1686+
[0i32; 10].select_nth_unstable(20);
16871687
}
16881688

16891689
pub mod memchr {

0 commit comments

Comments
 (0)