Skip to content

Commit

Permalink
NFC: Use pointer::cast instead of as for pointer casts.
Browse files Browse the repository at this point in the history
Enforce this pattern with Clippy.
  • Loading branch information
briansmith committed Oct 31, 2023
1 parent bb8276c commit 6b06550
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ fn ctr32_encrypt_blocks_(
let blocks_u32 = blocks as u32;
assert_eq!(blocks, polyfill::usize_from_u32(blocks_u32));

let input = in_out[src].as_ptr() as *const [u8; BLOCK_LEN];
let output = in_out.as_mut_ptr() as *mut [u8; BLOCK_LEN];
let input = in_out[src].as_ptr().cast::<[u8; BLOCK_LEN]>();
let output = in_out.as_mut_ptr().cast::<[u8; BLOCK_LEN]>();

unsafe {
f(input, output, blocks, key, ctr);
Expand Down
2 changes: 1 addition & 1 deletion src/aead/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Context {
debug_assert_eq!(input_bytes % BLOCK_LEN, 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:
// - `[[u8; BLOCK_LEN]]` has the same bit validity as `[u8]`.
// - `[[u8; BLOCK_LEN]]` has the same alignment requirement as `[u8]`.
Expand Down
2 changes: 1 addition & 1 deletion src/digest/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(super) extern "C" fn block_data_order(
) {
let state = unsafe { &mut state.as32 };
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) };
*state = block_data_order_(*state, blocks)
}
Expand Down
2 changes: 1 addition & 1 deletion src/digest/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn block_data_order<S: Sha2>(
M: *const u8,
num: c::size_t,
) -> [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) };

for M in M {
Expand Down
5 changes: 3 additions & 2 deletions src/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ macro_rules! impl_array_encoding {
{
#[inline]
fn as_byte_array(&self) -> &[u8; $elems * core::mem::size_of::<$base>()] {
let as_bytes_ptr =
self.as_ptr() as *const [u8; $elems * core::mem::size_of::<$base>()];
let as_bytes_ptr = self
.as_ptr()
.cast::<[u8; $elems * core::mem::size_of::<$base>()]>();
unsafe { &*as_bytes_ptr }
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
invalid_reference_casting,
clippy::char_lit_as_u8,
clippy::fn_to_numeric_cast,
clippy::fn_to_numeric_cast_with_truncation
clippy::fn_to_numeric_cast_with_truncation,
clippy::ptr_as_ptr
)]
#![warn(
clippy::unnecessary_cast,
Expand Down
4 changes: 2 additions & 2 deletions src/polyfill/chunks_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ macro_rules! define_chunks_fixed {
{
#[inline(always)]
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 = as_ptr as *const [[T; $chunk_len]; $chunked_len];
let as_ptr = self.as_ptr().cast::<[T; $chunk_len]>();
let as_ptr = as_ptr.cast::<[[T; $chunk_len]; $chunked_len]>();
unsafe { &*as_ptr }
}
}
Expand Down

0 comments on commit 6b06550

Please sign in to comment.