Add std impl for rand

This commit is contained in:
Dario Nieuwenhuis 2020-12-14 00:36:29 +01:00
parent 1aae27270e
commit 80c504cd95
4 changed files with 23 additions and 2 deletions

View File

@ -5,7 +5,7 @@ authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
edition = "2018"
[features]
std = ["futures/std"]
std = ["futures/std", "rand_core"]
defmt-trace = []
defmt-debug = []
defmt-info = []
@ -16,6 +16,9 @@ defmt-error = []
defmt = { version = "0.1.3", optional = true }
log = { version = "0.4.11", optional = true }
# std-only
rand_core = { version = "0.5.1", optional = true, features = ["std"] }
cortex-m = "0.6.4"
futures = { version = "0.3.5", default-features = false }
pin-project = { version = "1.0.2", default-features = false }

View File

@ -4,6 +4,9 @@ pub trait Rand {
fn rand(&self, buf: &mut [u8]);
}
#[cfg(feature = "std")]
static mut RAND: Option<&'static dyn Rand> = Some(&if_std::Rand);
#[cfg(not(feature = "std"))]
static mut RAND: Option<&'static dyn Rand> = None;
pub unsafe fn set_rand(rand: &'static dyn Rand) {
@ -13,3 +16,15 @@ pub unsafe fn set_rand(rand: &'static dyn Rand) {
pub fn rand(buf: &mut [u8]) {
unsafe { unwrap!(RAND, "No rand set").rand(buf) }
}
#[cfg(feature = "std")]
mod if_std {
use rand_core::{OsRng, RngCore};
pub(crate) struct Rand;
impl super::Rand for Rand {
fn rand(&self, buf: &mut [u8]) {
OsRng.fill_bytes(buf)
}
}
}

View File

@ -5,7 +5,10 @@ set -euxo pipefail
# examples
(cd examples; cargo build --target thumbv7em-none-eabi --bins)
# embassy
# embassy std
(cd embassy; cargo build --features log,std)
# embassy embedded
(cd embassy; cargo build --target thumbv7em-none-eabi)
(cd embassy; cargo build --target thumbv7em-none-eabi --features log)
(cd embassy; cargo build --target thumbv7em-none-eabi --features defmt)