aboutsummaryrefslogtreecommitdiff
path: root/cortex-m-rt/macros/src/lib.rs
diff options
context:
space:
mode:
authorGravatar bors[bot] <bors[bot]@users.noreply.github.com> 2018-09-08 21:57:12 +0000
committerGravatar bors[bot] <bors[bot]@users.noreply.github.com> 2018-09-08 21:57:12 +0000
commit513e66ff804c096d98b408fbf87f599aed0a9af2 (patch)
tree97ce43183744bafb6207a5ab3becd34ab4ae789b /cortex-m-rt/macros/src/lib.rs
parent9cb792ed7b6b45071080816d383296491c57202f (diff)
parentca46815047dd5f23f9a46f6cda7cf6e1e638815e (diff)
downloadcortex-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>
Diffstat (limited to 'cortex-m-rt/macros/src/lib.rs')
-rw-r--r--cortex-m-rt/macros/src/lib.rs24
1 files changed, 23 insertions, 1 deletions
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| {