Skip to content

Commit

Permalink
Remove uses of cast
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 18, 2022
1 parent 563eac2 commit e90f51a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion benches/mod.rs
Expand Up @@ -15,7 +15,8 @@ fn bench<const N: usize>(b: &mut test::Bencher) {
fn bench_raw<const N: usize>(b: &mut test::Bencher) {
b.iter(|| {
let mut buf = core::mem::MaybeUninit::<[u8; N]>::uninit();
unsafe { getrandom::getrandom_raw(buf.as_mut_ptr().cast(), N).unwrap() };
// TODO: use `cast` on MSRV bump to 1.38
unsafe { getrandom::getrandom_raw(buf.as_mut_ptr() as *mut u8, N).unwrap() };
test::black_box(&buf);
});
b.bytes = N as u64;
Expand Down
5 changes: 4 additions & 1 deletion src/3ds.rs
Expand Up @@ -11,5 +11,8 @@ use crate::util_libc::sys_fill_exact;
use crate::Error;

pub unsafe fn getrandom_inner(dst: *mut u8, len: usize) -> Result<(), Error> {
sys_fill_exact(dst, len, |dst, len| libc::getrandom(dst.cast(), len, 0))
// TODO: use `cast` on MSRV bump to 1.38
sys_fill_exact(dst, len, |dst, len| {
libc::getrandom(dst as *mut c_void, len, 0)
})
}
3 changes: 2 additions & 1 deletion src/bsd_arandom.rs
Expand Up @@ -12,10 +12,11 @@ use core::ptr;

unsafe fn kern_arnd(dst: *mut u8, mut len: usize) -> libc::ssize_t {
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
// TODO: use `cast` on MSRV bump to 1.38
let ret = libc::sysctl(
MIB.as_ptr(),
MIB.len() as libc::c_uint,
dst.cast(),
dst as *mut c_void,
&mut len,
ptr::null(),
0,
Expand Down
3 changes: 2 additions & 1 deletion src/espidf.rs
Expand Up @@ -20,6 +20,7 @@ pub unsafe fn getrandom_inner(dst: *mut u8, len: usize) -> Result<(), Error> {
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
//
// However tracking if some of these entropy sources is enabled is way too difficult to implement here
esp_fill_random(dst.cast(), len);
// TODO: use `cast` on MSRV bump to 1.38
esp_fill_random(dst as *mut c_void, len);
Ok(())
}
2 changes: 1 addition & 1 deletion src/linux_android.rs
Expand Up @@ -17,7 +17,7 @@ pub unsafe fn getrandom_inner(dst: *mut u8, len: usize) -> Result<(), Error> {
// getrandom(2) was introduced in Linux 3.17
static HAS_GETRANDOM: LazyBool = LazyBool::new();
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
// TODO: use `cast_mut` on MSRV bump to 1.38
// TODO: use `cast` on MSRV bump to 1.38
sys_fill_exact(dst, len, |dst, len| {
getrandom(dst as *mut libc::c_void, len, 0)
})
Expand Down
3 changes: 2 additions & 1 deletion src/openbsd.rs
Expand Up @@ -13,7 +13,8 @@ pub unsafe fn getrandom_inner(mut dst: *mut u8, mut len: usize) -> Result<(), Er
// getentropy(2) was added in OpenBSD 5.6, so we can use it unconditionally.
while len != 0 {
let chunk_len = core::cmp::min(len, 256);
let ret = libc::getentropy(dst.cast(), chunk_len);
// TODO: use `cast` on MSRV bump to 1.38
let ret = libc::getentropy(dst as *mut c_void, chunk_len);
if ret == -1 {
return Err(last_os_error());
}
Expand Down
2 changes: 1 addition & 1 deletion src/rdrand.rs
Expand Up @@ -82,7 +82,7 @@ pub unsafe fn getrandom_inner(dst: *mut u8, len: usize) -> Result<(), Error> {
#[target_feature(enable = "rdrand")]
unsafe fn rdrand_exact(mut dst: *mut u8, mut len: usize) -> Result<(), Error> {
while len >= WORD_SIZE {
// TODO: use `cast_mut` on MSRV bump to 1.38
// TODO: use `cast` on MSRV bump to 1.38
ptr::write(dst as *mut [u8; WORD_SIZE], rdrand()?);
dst = dst.add(WORD_SIZE);
len -= WORD_SIZE;
Expand Down

0 comments on commit e90f51a

Please sign in to comment.