const_shellsort

Macro const_shellsort 

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

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

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_shellsort;

const U8S: &[u8] = &const_shellsort!([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_shellsort;

#[derive(Debug, PartialEq)]
struct Foo(u8);

const FOOS_MUT_REF: &[Foo] = &{
    let mut foo = [Foo(1), Foo(2), Foo(4), Foo(3)];
    const_shellsort!(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].