Skip to content

Commit 86327dd

Browse files
committed
Avoid conditional module inclusion
This requires slightly more logic in util_libc, but produces a more typical setup for the crate.
1 parent 658bb1a commit 86327dd

File tree

14 files changed

+98
-107
lines changed

14 files changed

+98
-107
lines changed

src/backends/getentropy.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
//! - vita newlib since Dec 2021
88
//!
99
//! For these targets, we use getentropy(2) because getrandom(2) doesn't exist.
10-
use crate::Error;
10+
use crate::{Error, util_libc};
1111
use core::{ffi::c_void, mem::MaybeUninit};
1212

1313
pub use crate::util::{inner_u32, inner_u64};
1414

15-
#[path = "../util_libc.rs"]
16-
mod util_libc;
17-
1815
#[inline]
1916
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2017
for chunk in dest.chunks_mut(256) {

src/backends/getrandom.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
//! GRND_RANDOM is not recommended. On NetBSD/FreeBSD/Dragonfly/3ds, it does
1616
//! nothing. On illumos, the default pool is used to implement getentropy(2),
1717
//! so we assume it is acceptable here.
18-
use crate::Error;
18+
use crate::{Error, util_libc};
1919
use core::mem::MaybeUninit;
2020

2121
pub use crate::util::{inner_u32, inner_u64};
2222

23-
#[path = "../util_libc.rs"]
24-
mod util_libc;
25-
2623
#[inline]
2724
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2825
util_libc::sys_fill_exact(dest, |buf| {

src/backends/linux_android_with_fallback.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Implementation for Linux / Android with `/dev/urandom` fallback
22
use super::{sanitizer, use_file};
3-
use crate::Error;
3+
use crate::{Error, util_libc};
44
use core::{
55
ffi::c_void,
66
mem::{MaybeUninit, transmute},
77
ptr::NonNull,
88
sync::atomic::{AtomicPtr, Ordering},
99
};
10-
use use_file::util_libc;
1110

1211
pub use crate::util::{inner_u32, inner_u64};
1312

src/backends/netbsd.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! `getrandom(2)` was introduced in NetBSD 10. To support older versions we
44
//! implement our own weak linkage to it, and provide a fallback based on the
55
//! KERN_ARND sysctl.
6-
use crate::Error;
6+
use crate::{Error, util_libc};
77
use core::{
88
cmp,
99
ffi::c_void,
@@ -14,9 +14,6 @@ use core::{
1414

1515
pub use crate::util::{inner_u32, inner_u64};
1616

17-
#[path = "../util_libc.rs"]
18-
mod util_libc;
19-
2017
unsafe extern "C" fn polyfill_using_kern_arand(
2118
buf: *mut c_void,
2219
buflen: libc::size_t,

src/backends/rdrand.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! RDRAND backend for x86(-64) targets
2-
use crate::{Error, util::slice_as_uninit};
2+
use crate::{Error, lazy, util::slice_as_uninit};
33
use core::mem::{MaybeUninit, size_of};
44

5-
#[path = "../lazy.rs"]
6-
mod lazy;
7-
85
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
96
compile_error!("`rdrand` backend can be enabled only for x86 and x86-64 targets!");
107

src/backends/rndr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ fn is_rndr_available() -> bool {
6969

7070
#[cfg(not(target_feature = "rand"))]
7171
fn is_rndr_available() -> bool {
72-
#[path = "../lazy.rs"]
73-
mod lazy;
72+
use crate::lazy;
7473
static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new();
7574

7675
cfg_if::cfg_if! {

src/backends/solaris.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212
//! For more information, see the man page linked in lib.rs and this blog post:
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).
15-
use crate::Error;
15+
use crate::{Error, util_libc};
1616
use core::{ffi::c_void, mem::MaybeUninit};
1717

1818
pub use crate::util::{inner_u32, inner_u64};
1919

20-
#[path = "../util_libc.rs"]
21-
mod util_libc;
22-
2320
const MAX_BYTES: usize = 1024;
2421

2522
#[inline]

src/backends/use_file.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Implementations that just need to read from a file
2-
use crate::Error;
2+
use crate::{Error, util_libc};
33
use core::{
44
ffi::{CStr, c_void},
55
mem::MaybeUninit,
@@ -9,9 +9,6 @@ use core::{
99
#[cfg(not(any(target_os = "android", target_os = "linux")))]
1010
pub use crate::util::{inner_u32, inner_u64};
1111

12-
#[path = "../util_libc.rs"]
13-
pub(super) mod util_libc;
14-
1512
/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
1613
/// For more information see the linked man pages in lib.rs.
1714
/// - On Linux, "/dev/urandom is preferred and sufficient in all use cases".

src/backends/vxworks.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//! Implementation for VxWorks
2-
use crate::Error;
2+
use crate::{Error, util_libc};
33
use core::{
44
cmp::Ordering::{Equal, Greater, Less},
55
mem::MaybeUninit,
66
sync::atomic::{AtomicBool, Ordering::Relaxed},
77
};
88

9-
#[path = "../util_libc.rs"]
10-
mod util_libc;
11-
129
pub use crate::util::{inner_u32, inner_u64};
1310

1411
static RNG_INIT: AtomicBool = AtomicBool::new(false);

src/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ impl Error {
5959

6060
/// Creates a new instance of an `Error` from a negative error code.
6161
#[cfg(not(target_os = "uefi"))]
62-
#[allow(dead_code)]
6362
pub(super) fn from_neg_error_code(code: RawOsError) -> Self {
6463
if code < 0 {
6564
let code = NonZeroRawOsError::new(code).expect("`code` is negative");
@@ -71,7 +70,6 @@ impl Error {
7170

7271
/// Creates a new instance of an `Error` from an UEFI error code.
7372
#[cfg(target_os = "uefi")]
74-
#[allow(dead_code)]
7573
pub(super) fn from_uefi_code(code: RawOsError) -> Self {
7674
if code & UEFI_ERROR_FLAG != 0 {
7775
let code = NonZeroRawOsError::new(code).expect("The highest bit of `code` is set to 1");

0 commit comments

Comments
 (0)