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

no-std compile_time_sort

Sort arrays and slices of primitives in const contexts

21 releases (8 stable)

new 1.0.7 Mar 10, 2025
1.0.6 Mar 7, 2025
0.2.9 Mar 1, 2025
0.2.8 Jan 11, 2025
0.1.2 Dec 16, 2024

#451 in Algorithms

Download history 592/week @ 2024-12-16 44/week @ 2024-12-23 214/week @ 2024-12-30 149/week @ 2025-01-06 32/week @ 2025-01-13 2/week @ 2025-01-20 16/week @ 2025-02-03 4/week @ 2025-02-10 154/week @ 2025-02-24 955/week @ 2025-03-03

1,113 downloads per month

MIT/Apache

19KB
287 lines

compile_time_sort

Crates.io Version Docs.rs Documentation Github Repository Link GitHub Actions Workflow Status Code Coverage

This small crate provides functions for sorting arrays and slices of primitives in const contexts.

Arrays and slices of bools, u8s, and i8s are sorted with counting sort while arrays of other types are sorted with quicksort.

This implementation is usable on Rust version 1.59.0, before the const_trait_impl feature is stabilized. This means that it unfortunately can not be generic, and so there are separate functions for every primitive type.

Functions with the naming convention into_sorted_*_array take an array by value, and functions with the naming convention sort_*_slice take a mutable reference to a slice.

The functions that sort slices by reference are only available on Rust versions 1.83 and above.

Examples

Sort an array by value:

use const_sort::into_sorted_i32_array;

const ARRAY: [i32; 5] = [-3, 3, 2, i32::MAX, 0];
const SORTED_ARRAY: [i32; 5] = into_sorted_i32_array(ARRAY);

assert_eq!(SORTED_ARRAY, [-3, 0, 2, 3, i32::MAX]);

Sort by reference:

use const_sort::sort_i32_slice;

const SORTED_ARRAY: [i32; 5] = {
    let mut arr = [5, i32::MIN, 0, -2, 0];
    sort_i32_slice(&mut arr);
    arr
};

assert_eq!(SORTED_ARRAY, [i32::MIN, -2, 0, 0, 5]);

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies