#panic #static-check #static-assert #static-assert #slice

no-std dont_panic_slice

Slice that causes link error instead of panicking

1 unstable release

Uses old Rust 2015

0.1.0 Jul 6, 2017

#5 in #static-assert

MITNFA license

188 lines

Don't panic!() slice

This crate uses dont_panic crate to create drop-in replacement for slices. (Not fully drop-in yet.) The goal is to ensure the code won't ever panic. The user of the crate must prove to the compiler that the panicking code is unreachable by checking bounds before indexing into slice.


Non-panicking drop-in replacement for slices. Instead of panic it causes link time error if bounds are not checked. (Not fully drop-in replacement yet. Some features are missing.)


extern crate dont_panic_slice;

use dont_panic_slice::DPSlice;

fn main() {
    let arr = [0, 1, 2, 3];
    let dps = <&DPSlice<_>>::from(&arr as &[_]);
    assert_eq!(dps[0], 0);
    assert_eq!(dps[3], 3);
    // This would not compile (instead of run time panicking)
    assert_eq!(dps[42], 42);

You must compile it with --release. If you don't want to slow down debug builds, you can use --features=panic to switch to normal panicking behaviour.