Skip to content

Commit 6536b9e

Browse files
authored
Remove #cfg from bsd_arandom.rs (#332)
Followup to #331, we don't need the condidtional compilation anymore, because this file is only used if `any(target_os = "freebsd", target_os = "netbsd")` anyway. Also cleans up `use` statements and type declarations to look like those in macos.rs Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 6e3bbde commit 6536b9e

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/bsd_arandom.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
// except according to those terms.
88

99
//! Implementation for FreeBSD and NetBSD
10-
use crate::{util_libc::sys_fill_exact, Error};
10+
use crate::{
11+
util_libc::{sys_fill_exact, Weak},
12+
Error,
13+
};
1114
use core::{mem::MaybeUninit, ptr};
1215

1316
fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
@@ -30,22 +33,18 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
3033
}
3134
}
3235

36+
type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
37+
3338
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3439
// getrandom(2) was introduced in FreeBSD 12.0 and NetBSD 10.0
35-
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
36-
{
37-
use crate::util_libc::Weak;
38-
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
39-
type GetRandomFn =
40-
unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
41-
42-
if let Some(fptr) = GETRANDOM.ptr() {
43-
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
44-
return sys_fill_exact(dest, |buf| unsafe {
45-
func(buf.as_mut_ptr() as *mut u8, buf.len(), 0)
46-
});
47-
}
40+
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
41+
if let Some(fptr) = GETRANDOM.ptr() {
42+
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
43+
return sys_fill_exact(dest, |buf| unsafe {
44+
func(buf.as_mut_ptr() as *mut u8, buf.len(), 0)
45+
});
4846
}
47+
4948
// Both FreeBSD and NetBSD will only return up to 256 bytes at a time, and
5049
// older NetBSD kernels will fail on longer buffers.
5150
for chunk in dest.chunks_mut(256) {

0 commit comments

Comments
 (0)