From a66be66c22bc31526ac35c1cffdb28992a392ccf Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 22 Jun 2024 11:03:43 -0700 Subject: move DisplaySink code out from yaxpeax-x86 it was built in-place around yaxpeax-x86, hoisted out once it seemed suitable and could be generalized. yay! also include a Makefile in yaxpeax-arch now to test that various crate feature flag combinations.. work. --- src/safer_unchecked.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/safer_unchecked.rs (limited to 'src/safer_unchecked.rs') 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 { + unsafe fn get_kinda_unchecked(&self, index: I) -> &>::Output + where + I: SliceIndex<[T]>; +} + +impl GetSaferUnchecked for [T] { + #[inline(always)] + unsafe fn get_kinda_unchecked(&self, index: I) -> &>::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() + } +} -- cgit v1.1