-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unchecked versions of GenericArray::from_[mut_]slice #124
Comments
Not exactly ergonomic, but you could do something like this in your own code where necessary: use generic_array::{GenericArray, typenum::{U24, Unsigned}};
pub fn from_mut_slice(x: &mut [u8]) -> &mut GenericArray<u8, U24> {
if x.len() != U24::USIZE {
unsafe { std::hint::unreachable_unchecked() }
}
GenericArray::from_mut_slice(x)
} |
Additional notes based on deleted comment: If the goal is optimization, then the Click here to open examplepub fn version_a(x: &mut [u8]) -> &mut GenericArray<u8, U24> {
if x.len() != U24::USIZE {
unsafe { std::hint::unreachable_unchecked() }
}
GenericArray::from_mut_slice(x)
}
pub fn version_b(x: &mut [u8]) -> &mut GenericArray<u8, U24> {
GenericArray::from_mut_slice(x)
} compiles down to playground::version_a:
movq %rdi, %rax
retq
playground::version_b:
subq $56, %rsp
movq %rsi, (%rsp)
cmpq $24, %rsi
jne .LBB4_1
movq %rdi, %rax
addq $56, %rsp
retq
.LBB4_1:
movq $0, 8(%rsp)
movq %rsp, %rdi
leaq 8(%rsp), %rsi
callq core::panicking::assert_failed
ud2 If the goal is to just be unsafe for the sake of being unsafe, that's just asking for trouble. |
Thank you, this does (unergonomically, as you say) compile down to what I need. However, it also relies on:
I'm quite happy with this workaround if exposing |
As of 1.0, |
Minor point, but would it be possible to expose an
unsafe
version of these functions that doesn't perform any length-check?The text was updated successfully, but these errors were encountered: