feat: conditionally compile without a source of randomness
This commit is contained in:
parent
98a2733173
commit
d14f99c6af
2 changed files with 15 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
|
|||
[features]
|
||||
default = ["getrandom"]
|
||||
getrandom = ["dep:getrandom"]
|
||||
no_random = [] # compile without access to a source of randomness
|
||||
|
||||
[dependencies]
|
||||
getrandom = { version = "0.2", optional = true }
|
||||
|
|
|
|||
14
src/lib.rs
14
src/lib.rs
|
|
@ -12,6 +12,20 @@ pub(crate) fn get_random(buf: &mut [u8]) {
|
|||
getrandom::getrandom(buf).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(feature = "no_random")]
|
||||
pub(crate) fn get_random(buf: &mut [u8]) {
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
static COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
let value = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
let bytes = value.to_ne_bytes();
|
||||
|
||||
for (i, byte) in buf.iter_mut().enumerate() {
|
||||
*byte = bytes[i % core::mem::size_of::<usize>()];
|
||||
}
|
||||
}
|
||||
|
||||
/// FIXME: remove hard coded defaults
|
||||
/// The strategy for message processing is such:
|
||||
///
|
||||
|
|
|
|||
Loading…
Reference in a new issue