diff options
author | 2018-09-08 21:57:12 +0000 | |
---|---|---|
committer | 2018-09-08 21:57:12 +0000 | |
commit | 513e66ff804c096d98b408fbf87f599aed0a9af2 (patch) | |
tree | 97ce43183744bafb6207a5ab3becd34ab4ae789b | |
parent | 9cb792ed7b6b45071080816d383296491c57202f (diff) | |
parent | ca46815047dd5f23f9a46f6cda7cf6e1e638815e (diff) | |
download | cortex-m-513e66ff804c096d98b408fbf87f599aed0a9af2.tar.gz cortex-m-513e66ff804c096d98b408fbf87f599aed0a9af2.tar.zst cortex-m-513e66ff804c096d98b408fbf87f599aed0a9af2.zip |
Merge #107
107: Disable default-features on rand dependency to avoid std version r=japaric a=therealprof
Due to the single dependency tree, the attempted use of a std version
flips all depending crates to the std version as well which will not
compile on no_std systems.
Fixes #105
Signed-off-by: Daniel Egger <daniel@eggers-club.de>
Co-authored-by: Daniel Egger <daniel@eggers-club.de>
-rw-r--r-- | cortex-m-rt/Cargo.toml | 4 | ||||
-rw-r--r-- | cortex-m-rt/ci/script.sh | 3 | ||||
-rw-r--r-- | cortex-m-rt/examples/rand.rs | 24 | ||||
-rw-r--r-- | cortex-m-rt/macros/Cargo.toml | 5 | ||||
-rw-r--r-- | cortex-m-rt/macros/src/lib.rs | 24 |
5 files changed, 57 insertions, 3 deletions
diff --git a/cortex-m-rt/Cargo.toml b/cortex-m-rt/Cargo.toml index 276f33c..9d82794 100644 --- a/cortex-m-rt/Cargo.toml +++ b/cortex-m-rt/Cargo.toml @@ -19,6 +19,10 @@ cortex-m = "0.5.4" panic-abort = "0.3.0" panic-semihosting = "0.4.0" +[dev-dependencies.rand] +default-features = false +version = "0.5.5" + [target.'cfg(not(target_os = "none"))'.dev-dependencies] compiletest_rs = "0.3.14" diff --git a/cortex-m-rt/ci/script.sh b/cortex-m-rt/ci/script.sh index cecb975..9933372 100644 --- a/cortex-m-rt/ci/script.sh +++ b/cortex-m-rt/ci/script.sh @@ -20,11 +20,12 @@ main() { minimal override-exception pre_init + rand state unsafe-default-handler - unsafe-hard-fault unsafe-entry unsafe-exception + unsafe-hard-fault ) local fail_examples=( data_overflow diff --git a/cortex-m-rt/examples/rand.rs b/cortex-m-rt/examples/rand.rs new file mode 100644 index 0000000..e0cfd31 --- /dev/null +++ b/cortex-m-rt/examples/rand.rs @@ -0,0 +1,24 @@ +//! Use rand crate to ensure it's configured for no_std compatbility + +#![deny(warnings)] +#![no_main] +#![no_std] + +extern crate cortex_m_rt as rt; +use rt::entry; + +extern crate panic_semihosting; + +extern crate rand; +use rand::Rng; +use rand::SeedableRng; + +// the program entry point +#[entry] +fn main() -> ! { + let seed: [u8; 32] = [0; 32]; + let mut rng = rand::ChaChaRng::from_seed(seed); + let _ = rng.gen::<u32>(); + + loop {} +} diff --git a/cortex-m-rt/macros/Cargo.toml b/cortex-m-rt/macros/Cargo.toml index 21a6795..48c96d0 100644 --- a/cortex-m-rt/macros/Cargo.toml +++ b/cortex-m-rt/macros/Cargo.toml @@ -14,12 +14,15 @@ proc-macro = true [dependencies] quote = "0.6.6" -rand = "0.5.5" proc-macro2 = "0.4.15" [dependencies.syn] features = ["extra-traits", "full"] version = "0.14.8" +[dependencies.rand] +version = "0.5.5" +default-features = false + [dev-dependencies] cortex-m-rt = { path = "..", version = "0.6.0" } diff --git a/cortex-m-rt/macros/src/lib.rs b/cortex-m-rt/macros/src/lib.rs index 3435756..579e566 100644 --- a/cortex-m-rt/macros/src/lib.rs +++ b/cortex-m-rt/macros/src/lib.rs @@ -4,13 +4,19 @@ extern crate proc_macro; extern crate rand; #[macro_use] extern crate quote; +extern crate core; extern crate proc_macro2; extern crate syn; use proc_macro2::Span; use rand::Rng; +use rand::SeedableRng; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::{SystemTime, UNIX_EPOCH}; use syn::{FnArg, Ident, Item, ItemFn, ItemStatic, ReturnType, Stmt, Type, Visibility}; +static CALL_COUNT: AtomicUsize = AtomicUsize::new(0); + use proc_macro::TokenStream; /// Attribute to declare the entry point of the program @@ -492,7 +498,23 @@ pub fn pre_init(args: TokenStream, input: TokenStream) -> TokenStream { // Creates a random identifier fn random_ident() -> Ident { - let mut rng = rand::thread_rng(); + let secs = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs(); + + let count: u64 = CALL_COUNT.fetch_add(1, Ordering::SeqCst) as u64; + let mut seed: [u8; 16] = [0; 16]; + + for (i, v) in seed.iter_mut().take(8).enumerate() { + *v = ((secs >> (i * 8)) & 0xFF) as u8 + } + + for (i, v) in seed.iter_mut().skip(8).enumerate() { + *v = ((count >> (i * 8)) & 0xFF) as u8 + } + + let mut rng = rand::rngs::SmallRng::from_seed(seed); Ident::new( &(0..16) .map(|i| { |