Crate unchecked_index [] [src]

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

Rust Version

This version of the crate requires Rust 1.15 or later.

Structs

UncheckedIndex

Wrapper type for unchecked indexing through the regular index syntax

Traits

CheckIndex
GetUnchecked
GetUncheckedMut

Functions

get_unchecked

Access the element(s) at index, without bounds checks!

get_unchecked_mut

Access the element(s) at index, without bounds checks!

unchecked_index

Create a new unchecked indexing wrapper.