NFC: Use pointer::cast instead of as for pointer casts.

Enforce this pattern with Clippy.
This commit is contained in:
Brian Smith 2023-10-30 18:11:56 -07:00
parent 1fa6d09eef
commit 2b1194c845
7 changed files with 12 additions and 10 deletions

View File

@ -114,8 +114,8 @@ fn ctr32_encrypt_blocks_(
let blocks_u32 = blocks as u32; let blocks_u32 = blocks as u32;
assert_eq!(blocks, polyfill::usize_from_u32(blocks_u32)); assert_eq!(blocks, polyfill::usize_from_u32(blocks_u32));
let input = in_out[src].as_ptr() as *const [u8; BLOCK_LEN]; let input = in_out[src].as_ptr().cast::<[u8; BLOCK_LEN]>();
let output = in_out.as_mut_ptr() as *mut [u8; BLOCK_LEN]; let output = in_out.as_mut_ptr().cast::<[u8; BLOCK_LEN]>();
unsafe { unsafe {
f(input, output, blocks, key, ctr); f(input, output, blocks, key, ctr);

View File

@ -124,7 +124,7 @@ impl Context {
debug_assert_eq!(input_bytes % BLOCK_LEN, 0); debug_assert_eq!(input_bytes % BLOCK_LEN, 0);
debug_assert!(input_bytes > 0); debug_assert!(input_bytes > 0);
let input = input.as_ptr() as *const [u8; BLOCK_LEN]; let input = input.as_ptr().cast::<[u8; BLOCK_LEN]>();
// SAFETY: // SAFETY:
// - `[[u8; BLOCK_LEN]]` has the same bit validity as `[u8]`. // - `[[u8; BLOCK_LEN]]` has the same bit validity as `[u8]`.
// - `[[u8; BLOCK_LEN]]` has the same alignment requirement as `[u8]`. // - `[[u8; BLOCK_LEN]]` has the same alignment requirement as `[u8]`.

View File

@ -40,7 +40,7 @@ pub(super) extern "C" fn block_data_order(
) { ) {
let state = unsafe { &mut state.as32 }; let state = unsafe { &mut state.as32 };
let state: &mut State = (&mut state[..CHAINING_WORDS]).try_into().unwrap(); let state: &mut State = (&mut state[..CHAINING_WORDS]).try_into().unwrap();
let data = data as *const [<W32 as Word>::InputBytes; 16]; let data = data.cast::<[<W32 as Word>::InputBytes; 16]>();
let blocks = unsafe { core::slice::from_raw_parts(data, num) }; let blocks = unsafe { core::slice::from_raw_parts(data, num) };
*state = block_data_order_(*state, blocks) *state = block_data_order_(*state, blocks)
} }

View File

@ -48,7 +48,7 @@ fn block_data_order<S: Sha2>(
M: *const u8, M: *const u8,
num: c::size_t, num: c::size_t,
) -> [S; CHAINING_WORDS] { ) -> [S; CHAINING_WORDS] {
let M = M as *const [S::InputBytes; 16]; let M = M.cast::<[S::InputBytes; 16]>();
let M: &[[S::InputBytes; 16]] = unsafe { core::slice::from_raw_parts(M, num) }; let M: &[[S::InputBytes; 16]] = unsafe { core::slice::from_raw_parts(M, num) };
for M in M { for M in M {

View File

@ -32,8 +32,9 @@ macro_rules! impl_array_encoding {
{ {
#[inline] #[inline]
fn as_byte_array(&self) -> &[u8; $elems * core::mem::size_of::<$base>()] { fn as_byte_array(&self) -> &[u8; $elems * core::mem::size_of::<$base>()] {
let as_bytes_ptr = let as_bytes_ptr = self
self.as_ptr() as *const [u8; $elems * core::mem::size_of::<$base>()]; .as_ptr()
.cast::<[u8; $elems * core::mem::size_of::<$base>()]>();
unsafe { &*as_bytes_ptr } unsafe { &*as_bytes_ptr }
} }
} }

View File

@ -54,7 +54,8 @@
invalid_reference_casting, invalid_reference_casting,
clippy::char_lit_as_u8, clippy::char_lit_as_u8,
clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast,
clippy::fn_to_numeric_cast_with_truncation clippy::fn_to_numeric_cast_with_truncation,
clippy::ptr_as_ptr
)] )]
#![warn( #![warn(
clippy::unnecessary_cast, clippy::unnecessary_cast,

View File

@ -18,8 +18,8 @@ macro_rules! define_chunks_fixed {
{ {
#[inline(always)] #[inline(always)]
fn chunks_fixed(self) -> &'a [[T; $chunk_len]; $chunked_len] { fn chunks_fixed(self) -> &'a [[T; $chunk_len]; $chunked_len] {
let as_ptr: *const [T; $chunk_len] = self.as_ptr() as *const [T; $chunk_len]; let as_ptr = self.as_ptr().cast::<[T; $chunk_len]>();
let as_ptr = as_ptr as *const [[T; $chunk_len]; $chunked_len]; let as_ptr = as_ptr.cast::<[[T; $chunk_len]; $chunked_len]>();
unsafe { &*as_ptr } unsafe { &*as_ptr }
} }
} }