Macro const_quicksort

Source
macro_rules! const_quicksort {
    ($data:expr) => { ... };
    ($data:expr, |$a:ident, $b:ident| $cmp:expr) => { ... };
    ($data:expr, $cmp:expr) => { ... };
}
Expand description

Sorts a const array or mutable slice as a const expression.

Can be called with just the data to be sorted, in which case elements are compared by a < b resulting in the smallest element at the head of the data and the largest at the tail.

use sort_const::const_quicksort;
 
const U8S: &[u8] = &const_quicksort!([3, 1, 2]);
 
assert_eq!(U8S, [1, 2, 3]);

Can also be called with a lambda-like expression (e.g. |a, b| a < b) where true identifies that a should come before b.

use sort_const::const_quicksort;
 
#[derive(Debug, PartialEq)]
struct Foo(u8);
 
const FOOS_MUT_REF: &[Foo] = &{
    let mut foo = [Foo(1), Foo(2), Foo(4), Foo(3)];
    const_quicksort!(foo.split_last_mut().expect("failed").1, |a, b| a.0 > b.0);
    foo
};
 
assert_eq!(FOOS_MUT_REF, [4, 2, 1, 3].map(Foo));

Can also be called with the name of a const function which must return a boolean and which will be evaluated over &data[a], &data[b].