Skip to content

Commit bdf0d71

Browse files
committed
Use p.cast::<T>() instead of as as *mut T.
1 parent 1b32fea commit bdf0d71

15 files changed

+24
-22
lines changed

src/apple-other.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C" {
1414
}
1515

1616
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
17-
let ret = unsafe { CCRandomGenerateBytes(dest.as_mut_ptr() as *mut c_void, dest.len()) };
17+
let ret = unsafe { CCRandomGenerateBytes(dest.as_mut_ptr().cast::<c_void>(), dest.len()) };
1818
// kCCSuccess (from CommonCryptoError.h) is always zero.
1919
if ret != 0 {
2020
Err(Error::IOS_SEC_RANDOM)

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl Error {
9999
cfg_if! {
100100
if #[cfg(unix)] {
101101
fn os_err(errno: i32, buf: &mut [u8]) -> Option<&str> {
102-
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
102+
let buf_ptr = buf.as_mut_ptr().cast::<libc::c_char>();
103103
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
104104
return None;
105105
}

src/fuchsia.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ extern "C" {
88
}
99

1010
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
11-
unsafe { zx_cprng_draw(dest.as_mut_ptr() as *mut u8, dest.len()) }
11+
unsafe { zx_cprng_draw(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
1212
Ok(())
1313
}

src/getentropy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
//!
99
//! For these targets, we use getentropy(2) because getrandom(2) doesn't exist.
1010
use crate::{util_libc::last_os_error, Error};
11-
use core::mem::MaybeUninit;
11+
use core::{ffi::c_void, mem::MaybeUninit};
1212

1313
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1414
for chunk in dest.chunks_mut(256) {
15-
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr() as *mut libc::c_void, chunk.len()) };
15+
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
1616
if ret != 0 {
1717
return Err(last_os_error());
1818
}

src/getrandom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
//! nothing. On illumos, the default pool is used to implement getentropy(2),
1717
//! so we assume it is acceptable here.
1818
use crate::{util_libc::sys_fill_exact, Error};
19-
use core::mem::MaybeUninit;
19+
use core::{ffi::c_void, mem::MaybeUninit};
2020

2121
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2222
sys_fill_exact(dest, |buf| unsafe {
23-
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
23+
libc::getrandom(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
2424
})
2525
}

src/hermit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" {
1313

1414
pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1515
while !dest.is_empty() {
16-
let res = unsafe { sys_read_entropy(dest.as_mut_ptr() as *mut u8, dest.len(), 0) };
16+
let res = unsafe { sys_read_entropy(dest.as_mut_ptr().cast::<u8>(), dest.len(), 0) };
1717
// Positive `isize`s can be safely casted to `usize`
1818
if res > 0 && (res as usize) <= dest.len() {
1919
dest = &mut dest[res as usize..];

src/js.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
4040
// have a notion of "uninitialized memory", this is purely
4141
// a Rust/C/C++ concept.
4242
let res = n.random_fill_sync(unsafe {
43-
Uint8Array::view_mut_raw(chunk.as_mut_ptr() as *mut u8, chunk.len())
43+
Uint8Array::view_mut_raw(chunk.as_mut_ptr().cast::<u8>(), chunk.len())
4444
});
4545
if res.is_err() {
4646
return Err(Error::NODE_RANDOM_FILL_SYNC);
@@ -60,7 +60,7 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
6060
}
6161

6262
// SAFETY: `sub_buf`'s length is the same length as `chunk`
63-
unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr() as *mut u8) };
63+
unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr().cast::<u8>()) };
6464
}
6565
}
6666
};

src/netbsd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
99
libc::sysctl(
1010
MIB.as_ptr(),
1111
MIB.len() as libc::c_uint,
12-
buf.as_mut_ptr() as *mut _,
12+
buf.as_mut_ptr().cast::<c_void>(),
1313
&mut len,
1414
ptr::null(),
1515
0,
@@ -38,7 +38,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3838
if !fptr.is_null() {
3939
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
4040
return sys_fill_exact(dest, |buf| unsafe {
41-
func(buf.as_mut_ptr() as *mut u8, buf.len(), 0)
41+
func(buf.as_mut_ptr().cast::<u8>(), buf.len(), 0)
4242
});
4343
}
4444

src/solaris.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
1414
//! which also explains why this crate should not use getentropy(2).
1515
use crate::{util_libc::last_os_error, Error};
16-
use core::mem::MaybeUninit;
16+
use core::{ffi::c_void, mem::MaybeUninit};
1717

1818
const MAX_BYTES: usize = 1024;
1919

2020
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2121
for chunk in dest.chunks_mut(MAX_BYTES) {
22-
let ptr = chunk.as_mut_ptr() as *mut libc::c_void;
22+
let ptr = chunk.as_mut_ptr().cast::<c_void>();
2323
let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
2424
// In case the man page has a typo, we also check for negative ret.
2525
if ret <= 0 {

src/solid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern "C" {
77
}
88

99
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
10-
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr() as *mut u8, dest.len()) };
10+
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr().cast::<u8>(), dest.len()) };
1111
if ret >= 0 {
1212
Ok(())
1313
} else {

0 commit comments

Comments
 (0)