#sorting #const #no-alloc #const-sort

no-std sort-const

Sort arrays and slices in const contexts

2 stable releases

new 1.0.1 Mar 5, 2025
1.0.0 Mar 3, 2025

#762 in Algorithms

Download history 266/week @ 2025-03-02

266 downloads per month
Used in cphf

MIT/Apache

17KB
255 lines

sort-const

Crates.io Workflow Status

A macro for sorting arrays and slices at compile time.

Usage

Just use the const_quicksort or const_shellsort macros.

use sort_const::const_quicksort;

const fn lt(a: &u8, b: &u8) -> bool {
    *a < *b
}

const A: &[u8] = &const_quicksort!([3, 1, 2]);
const B: &[u8] = &const_quicksort!([3, 1, 2], |a, b| *a < *b);
const C: &[u8] = &const_quicksort!([3, 1, 2], lt);

assert_eq!(A, [1, 2, 3]);
assert_eq!(B, [1, 2, 3]);
assert_eq!(C, [1, 2, 3]);

For now, this crate uses a custom consty fork of arrayvec. bluss/arrayvec#294 should fix this.

License

Licensed under

Dependencies

~300KB