Skip to content

Commit d122cec

Browse files
committed
Relaxed trait bounds.
Added missing `const_is_sorted_by_key` function.
1 parent 8d704cf commit d122cec

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

const_sort_rs/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "const_sort_rs"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
authors = [

const_sort_rs/src/const_slice_sort_ext.rs

+51-4
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,49 @@ pub trait ConstSliceSortExt<T> {
379379
fn const_is_sorted_by<F>(&self, compare: F) -> bool
380380
where
381381
F: FnMut(&T, &T) -> Option<Ordering>;
382+
/// Checks if the elements of this slice are sorted using the given key extraction function.
383+
///
384+
/// Instead of comparing the slice's elements directly, this function compares the keys of the
385+
/// elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see its
386+
/// documentation for more information.
387+
///
388+
/// [`is_sorted`]: slice::is_sorted
389+
///
390+
/// # Examples
391+
///
392+
/// ```
393+
/// #![feature(const_mut_refs)]
394+
/// #![feature(const_trait_impl)]
395+
/// use const_sort_rs::ConstSliceSortExt;
396+
///
397+
/// const fn map_len(s: &&str) -> usize {
398+
/// s.len()
399+
/// }
400+
/// const A: bool = ["c", "bb", "aaa"].const_is_sorted_by_key(map_len);
401+
/// assert!(A);
402+
///
403+
/// const fn map_abs(i: &i32) -> i32 {
404+
/// i.abs()
405+
/// }
406+
/// const B: bool = [-2i32, -1, 0, 3].const_is_sorted_by_key(map_abs);
407+
/// assert!(!B);
408+
/// ```
409+
#[must_use]
410+
fn const_is_sorted_by_key<F, K>(&self, f: F) -> bool
411+
where
412+
F: FnMut(&T) -> K,
413+
K: PartialOrd;
382414
}
383415

384416
pub(crate) const fn const_pred_lt<T: Ord + ~const PartialOrd>(a: &T, b: &T) -> bool {
385417
a.lt(b)
386418
}
387419

388-
impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
420+
impl<T> const ConstSliceSortExt<T> for [T] {
389421
#[inline]
390422
fn const_sort_unstable(&mut self)
391423
where
392-
T: Ord,
424+
T: ~const PartialOrd + Ord,
393425
{
394426
const_sort::const_quicksort(self, const_pred_lt);
395427
}
@@ -425,7 +457,7 @@ impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
425457
#[inline]
426458
fn const_select_nth_unstable(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
427459
where
428-
T: Ord,
460+
T: ~const PartialOrd + Ord,
429461
{
430462
// let mut f = |a: &T, b: &T| a.lt(b);
431463
// sort::partition_at_index(self, index, &mut f)
@@ -473,7 +505,10 @@ impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
473505
}
474506

475507
#[inline]
476-
fn const_is_sorted(&self) -> bool {
508+
fn const_is_sorted(&self) -> bool
509+
where
510+
T: ~const PartialOrd,
511+
{
477512
const fn const_pred_p_cmp<T: ~const PartialOrd>(a: &T, b: &T) -> Option<Ordering> {
478513
a.partial_cmp(b)
479514
}
@@ -499,4 +534,16 @@ impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
499534
}
500535
true
501536
}
537+
#[inline]
538+
fn const_is_sorted_by_key<F, K>(&self, mut f: F) -> bool
539+
where
540+
F: ~const FnMut(&T) -> K + ~const Destruct,
541+
K: ~const PartialOrd + ~const Destruct,
542+
{
543+
self.const_is_sorted_by(
544+
const_closure!(FnMut for<T, K: PartialOrd, F: FnMut(&T) -> K> [f: F] (a:&T, b:&T) -> Option<Ordering> {
545+
f(a).partial_cmp(&f(b))
546+
}),
547+
)
548+
}
502549
}

0 commit comments

Comments
 (0)