aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-12-14 00:36:29 +0100
committerDario Nieuwenhuis <[email protected]>2020-12-14 00:36:29 +0100
commit80c504cd950349caa96ae37898f6f3325030c615 (patch)
treec32386334a8fd16de8d83285f5ab5c9b2f37ac18
parent1aae27270e9cce4571bb39d8082b548a39f5c389 (diff)
Add std impl for rand
-rw-r--r--embassy/Cargo.toml5
-rw-r--r--embassy/src/rand.rs15
-rw-r--r--examples/.cargo/config (renamed from .cargo/config)0
-rwxr-xr-xtest-build.sh5
4 files changed, 23 insertions, 2 deletions
diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml
index 0e4c801bb..08d273dbb 100644
--- a/embassy/Cargo.toml
+++ b/embassy/Cargo.toml
@@ -5,7 +5,7 @@ authors = ["Dario Nieuwenhuis <[email protected]>"]
5edition = "2018" 5edition = "2018"
6 6
7[features] 7[features]
8std = ["futures/std"] 8std = ["futures/std", "rand_core"]
9defmt-trace = [] 9defmt-trace = []
10defmt-debug = [] 10defmt-debug = []
11defmt-info = [] 11defmt-info = []
@@ -16,6 +16,9 @@ defmt-error = []
16defmt = { version = "0.1.3", optional = true } 16defmt = { version = "0.1.3", optional = true }
17log = { version = "0.4.11", optional = true } 17log = { version = "0.4.11", optional = true }
18 18
19# std-only
20rand_core = { version = "0.5.1", optional = true, features = ["std"] }
21
19cortex-m = "0.6.4" 22cortex-m = "0.6.4"
20futures = { version = "0.3.5", default-features = false } 23futures = { version = "0.3.5", default-features = false }
21pin-project = { version = "1.0.2", default-features = false } 24pin-project = { version = "1.0.2", default-features = false }
diff --git a/embassy/src/rand.rs b/embassy/src/rand.rs
index 7e3788380..6e78c24ae 100644
--- a/embassy/src/rand.rs
+++ b/embassy/src/rand.rs
@@ -4,6 +4,9 @@ pub trait Rand {
4 fn rand(&self, buf: &mut [u8]); 4 fn rand(&self, buf: &mut [u8]);
5} 5}
6 6
7#[cfg(feature = "std")]
8static mut RAND: Option<&'static dyn Rand> = Some(&if_std::Rand);
9#[cfg(not(feature = "std"))]
7static mut RAND: Option<&'static dyn Rand> = None; 10static mut RAND: Option<&'static dyn Rand> = None;
8 11
9pub unsafe fn set_rand(rand: &'static dyn Rand) { 12pub unsafe fn set_rand(rand: &'static dyn Rand) {
@@ -13,3 +16,15 @@ pub unsafe fn set_rand(rand: &'static dyn Rand) {
13pub fn rand(buf: &mut [u8]) { 16pub fn rand(buf: &mut [u8]) {
14 unsafe { unwrap!(RAND, "No rand set").rand(buf) } 17 unsafe { unwrap!(RAND, "No rand set").rand(buf) }
15} 18}
19
20#[cfg(feature = "std")]
21mod if_std {
22 use rand_core::{OsRng, RngCore};
23
24 pub(crate) struct Rand;
25 impl super::Rand for Rand {
26 fn rand(&self, buf: &mut [u8]) {
27 OsRng.fill_bytes(buf)
28 }
29 }
30}
diff --git a/.cargo/config b/examples/.cargo/config
index 3f319ae55..3f319ae55 100644
--- a/.cargo/config
+++ b/examples/.cargo/config
diff --git a/test-build.sh b/test-build.sh
index ae603b289..52bf7bb7b 100755
--- a/test-build.sh
+++ b/test-build.sh
@@ -5,7 +5,10 @@ set -euxo pipefail
5# examples 5# examples
6(cd examples; cargo build --target thumbv7em-none-eabi --bins) 6(cd examples; cargo build --target thumbv7em-none-eabi --bins)
7 7
8# embassy 8# embassy std
9(cd embassy; cargo build --features log,std)
10
11# embassy embedded
9(cd embassy; cargo build --target thumbv7em-none-eabi) 12(cd embassy; cargo build --target thumbv7em-none-eabi)
10(cd embassy; cargo build --target thumbv7em-none-eabi --features log) 13(cd embassy; cargo build --target thumbv7em-none-eabi --features log)
11(cd embassy; cargo build --target thumbv7em-none-eabi --features defmt) 14(cd embassy; cargo build --target thumbv7em-none-eabi --features defmt)