aboutsummaryrefslogtreecommitdiff
path: root/src/safer_unchecked.rs
blob: afd0355e6d4671bb0cbb61df00731bc2f11e7eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use core::slice::SliceIndex;

pub trait GetSaferUnchecked<T> {
    unsafe fn get_kinda_unchecked<I>(&self, index: I) -> &<I as SliceIndex<[T]>>::Output
    where
        I: SliceIndex<[T]>;
}

impl<T> GetSaferUnchecked<T> for [T] {
    unsafe fn get_kinda_unchecked<I>(&self, index: I) -> &<I as SliceIndex<[T]>>::Output
    where
        I: SliceIndex<[T]>,
    {
        if cfg!(debug_assertions) {
            &self[index]
        } else {
            self.get_unchecked(index)
        }
    }
}

pub unsafe fn unreachable_kinda_unchecked() -> ! {
    if cfg!(debug_assertions) {
        panic!("UB: Unreachable unchecked was executed")
    } else {
        core::hint::unreachable_unchecked()
    }
}