#index #unchecked #slice #debugging #no-std

no-std unchecked-index

Unchecked indexing wrapper using regular index syntax

5 releases

Uses old Rust 2015

0.2.2 Nov 18, 2017
0.2.1 Nov 12, 2017
0.2.0 Oct 29, 2017
0.1.1 Oct 22, 2017
0.1.0 Oct 8, 2017

#2264 in Rust patterns

Download history 10910/week @ 2024-10-31 9782/week @ 2024-11-07 10609/week @ 2024-11-14 12001/week @ 2024-11-21 9204/week @ 2024-11-28 8757/week @ 2024-12-05 10090/week @ 2024-12-12 6561/week @ 2024-12-19 4537/week @ 2024-12-26 8710/week @ 2025-01-02 9958/week @ 2025-01-09 10900/week @ 2025-01-16 10922/week @ 2025-01-23 13401/week @ 2025-01-30 13798/week @ 2025-02-06 7629/week @ 2025-02-13

48,015 downloads per month
Used in 147 crates (8 directly)

MIT/Apache

15KB
259 lines

unchecked-index

Unchecked indexing through the regular index syntax.

Using a wrapper type that requires an unsafe block to create.

Note: All unchecked indexing here is actually “checked” with debug assertions when they are enabled (they are off by default in release builds). This is a feature! Debug checking does not make your code safe, but it helps finding bugs in unsafe code. Test your code responsibly.

Example


use unchecked_index::unchecked_index;

/// unsafe because: trusts the permutation to be correct
unsafe fn apply_permutation<T>(perm: &mut [usize], v: &mut [T]) {
    debug_assert_eq!(perm.len(), v.len());
    
    // use unchecked (in reality, debug-checked) indexing throughout
    let mut perm = unchecked_index(perm);
    
    for i in 0..perm.len() {
        let mut current = i;
        while i != perm[current] {
            let next = perm[current];
            // move element from next to current
            v.swap(next, current);
            perm[current] = current;
            current = next;
        }
        perm[current] = current;
    }
}

How to contribute:

  • Fix a bug or implement a new thing
  • Include tests for your new feature
  • Make a pull request

Recent Changes

  • 0.2.2

    • The crate is now always no_std.
  • 0.2.1

    • Improve the (debug) assertion messages; fix a typo and always include the relevant quantities (start, end, length)
  • 0.2.0

    • Add support for unchecked indexing with ranges (“slicing”)
    • Add two free functions, get_unchecked and get_unchecked_mut
  • 0.1.1

    • Add Copy impl (for shared slices)
  • 0.1.0

    • Initial release

No runtime deps