Skip to content

Commit 513e66f

Browse files
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 <[email protected]> Co-authored-by: Daniel Egger <[email protected]>
2 parents 9cb792e + ca46815 commit 513e66f

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

cortex-m-rt/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ cortex-m = "0.5.4"
1919
panic-abort = "0.3.0"
2020
panic-semihosting = "0.4.0"
2121

22+
[dev-dependencies.rand]
23+
default-features = false
24+
version = "0.5.5"
25+
2226
[target.'cfg(not(target_os = "none"))'.dev-dependencies]
2327
compiletest_rs = "0.3.14"
2428

cortex-m-rt/ci/script.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ main() {
2020
minimal
2121
override-exception
2222
pre_init
23+
rand
2324
state
2425
unsafe-default-handler
25-
unsafe-hard-fault
2626
unsafe-entry
2727
unsafe-exception
28+
unsafe-hard-fault
2829
)
2930
local fail_examples=(
3031
data_overflow

cortex-m-rt/examples/rand.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Use rand crate to ensure it's configured for no_std compatbility
2+
3+
#![deny(warnings)]
4+
#![no_main]
5+
#![no_std]
6+
7+
extern crate cortex_m_rt as rt;
8+
use rt::entry;
9+
10+
extern crate panic_semihosting;
11+
12+
extern crate rand;
13+
use rand::Rng;
14+
use rand::SeedableRng;
15+
16+
// the program entry point
17+
#[entry]
18+
fn main() -> ! {
19+
let seed: [u8; 32] = [0; 32];
20+
let mut rng = rand::ChaChaRng::from_seed(seed);
21+
let _ = rng.gen::<u32>();
22+
23+
loop {}
24+
}

cortex-m-rt/macros/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ proc-macro = true
1414

1515
[dependencies]
1616
quote = "0.6.6"
17-
rand = "0.5.5"
1817
proc-macro2 = "0.4.15"
1918

2019
[dependencies.syn]
2120
features = ["extra-traits", "full"]
2221
version = "0.14.8"
2322

23+
[dependencies.rand]
24+
version = "0.5.5"
25+
default-features = false
26+
2427
[dev-dependencies]
2528
cortex-m-rt = { path = "..", version = "0.6.0" }

cortex-m-rt/macros/src/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ extern crate proc_macro;
44
extern crate rand;
55
#[macro_use]
66
extern crate quote;
7+
extern crate core;
78
extern crate proc_macro2;
89
extern crate syn;
910

1011
use proc_macro2::Span;
1112
use rand::Rng;
13+
use rand::SeedableRng;
14+
use std::sync::atomic::{AtomicUsize, Ordering};
15+
use std::time::{SystemTime, UNIX_EPOCH};
1216
use syn::{FnArg, Ident, Item, ItemFn, ItemStatic, ReturnType, Stmt, Type, Visibility};
1317

18+
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
19+
1420
use proc_macro::TokenStream;
1521

1622
/// Attribute to declare the entry point of the program
@@ -492,7 +498,23 @@ pub fn pre_init(args: TokenStream, input: TokenStream) -> TokenStream {
492498

493499
// Creates a random identifier
494500
fn random_ident() -> Ident {
495-
let mut rng = rand::thread_rng();
501+
let secs = SystemTime::now()
502+
.duration_since(UNIX_EPOCH)
503+
.unwrap()
504+
.as_secs();
505+
506+
let count: u64 = CALL_COUNT.fetch_add(1, Ordering::SeqCst) as u64;
507+
let mut seed: [u8; 16] = [0; 16];
508+
509+
for (i, v) in seed.iter_mut().take(8).enumerate() {
510+
*v = ((secs >> (i * 8)) & 0xFF) as u8
511+
}
512+
513+
for (i, v) in seed.iter_mut().skip(8).enumerate() {
514+
*v = ((count >> (i * 8)) & 0xFF) as u8
515+
}
516+
517+
let mut rng = rand::rngs::SmallRng::from_seed(seed);
496518
Ident::new(
497519
&(0..16)
498520
.map(|i| {

0 commit comments

Comments
 (0)