Created
November 20, 2021 21:54
-
-
Save expenses/28eca46ef2f0535ece9cd9eba0a8029c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait GetUnchecked<T> { | |
unsafe fn get_unchecked(&self, index: usize) -> &T; | |
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T; | |
} | |
impl<T> GetUnchecked<T> for [T] { | |
unsafe fn get_unchecked(&self, index: usize) -> &T { | |
asm!( | |
"%slice_ptr = OpLoad _ {slice_ptr_ptr}", | |
"%data_ptr = OpCompositeExtract _ %slice_ptr 0", | |
"%val_ptr = OpAccessChain _ %data_ptr {index}", | |
"OpReturnValue %val_ptr", | |
slice_ptr_ptr = in(reg) &self, | |
index = in(reg) index, | |
options(noreturn) | |
) | |
} | |
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T { | |
asm!( | |
"%slice_ptr = OpLoad _ {slice_ptr_ptr}", | |
"%data_ptr = OpCompositeExtract _ %slice_ptr 0", | |
"%val_ptr = OpAccessChain _ %data_ptr {index}", | |
"OpReturnValue %val_ptr", | |
slice_ptr_ptr = in(reg) &self, | |
index = in(reg) index, | |
options(noreturn) | |
) | |
} | |
} | |
impl<T, const N: usize> GetUnchecked<T> for [T; N] { | |
unsafe fn get_unchecked(&self, index: usize) -> &T { | |
asm!( | |
"%val_ptr = OpAccessChain _ {array_ptr} {index}", | |
"OpReturnValue %val_ptr", | |
array_ptr = in(reg) self, | |
index = in(reg) index, | |
options(noreturn) | |
) | |
} | |
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T { | |
asm!( | |
"%val_ptr = OpAccessChain _ {array_ptr} {index}", | |
"OpReturnValue %val_ptr", | |
array_ptr = in(reg) self, | |
index = in(reg) index, | |
options(noreturn) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment