aboutsummaryrefslogtreecommitdiff
path: root/src/safer_unchecked.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/safer_unchecked.rs')
-rw-r--r--src/safer_unchecked.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/safer_unchecked.rs b/src/safer_unchecked.rs
new file mode 100644
index 0000000..34216bc
--- /dev/null
+++ b/src/safer_unchecked.rs
@@ -0,0 +1,30 @@
+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] {
+ #[inline(always)]
+ 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)
+ }
+ }
+}
+
+#[inline(always)]
+pub unsafe fn unreachable_kinda_unchecked() -> ! {
+ if cfg!(debug_assertions) {
+ panic!("UB: Unreachable unchecked was executed")
+ } else {
+ core::hint::unreachable_unchecked()
+ }
+}