From 057579bce52b42588638cfc3f12a61223ca71591 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sat, 21 Sep 2024 11:41:25 +0200 Subject: [PATCH] Same for nrf --- embassy-nrf/Cargo.toml | 2 +- embassy-nrf/src/rng.rs | 20 +++++++++++++++++++- examples/nrf-rtos-trace/Cargo.toml | 2 +- examples/nrf52840/Cargo.toml | 2 +- examples/nrf5340/Cargo.toml | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 3e66d6886..ce5cbe32d 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml @@ -155,7 +155,7 @@ log = { version = "0.4.14", optional = true } cortex-m-rt = ">=0.6.15,<0.8" cortex-m = "0.7.6" critical-section = "1.1" -rand_core = "0.6.3" +rand_core = "0.9.0-alpha.2" fixed = "1.10.0" embedded-storage = "0.3.1" embedded-storage-async = "0.4.1" diff --git a/embassy-nrf/src/rng.rs b/embassy-nrf/src/rng.rs index ff61e08f3..d61a027ec 100644 --- a/embassy-nrf/src/rng.rs +++ b/embassy-nrf/src/rng.rs @@ -3,6 +3,7 @@ #![macro_use] use core::cell::{RefCell, RefMut}; +use core::convert::Infallible; use core::future::poll_fn; use core::marker::PhantomData; use core::ptr; @@ -199,8 +200,25 @@ impl<'d, T: Instance> rand_core::RngCore for Rng<'d, T> { self.blocking_fill_bytes(&mut bytes); u64::from_ne_bytes(bytes) } +} - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { +impl<'d, T: crate::rng::Instance> rand_core::TryRngCore for Rng<'d, T> { + type Error = Infallible; + + fn try_next_u32(&mut self) -> Result { + let mut bytes = [0; 4]; + self.blocking_fill_bytes(&mut bytes); + // We don't care about the endianness, so just use the native one. + Ok(u32::from_ne_bytes(bytes)) + } + + fn try_next_u64(&mut self) -> Result { + let mut bytes = [0; 8]; + self.blocking_fill_bytes(&mut bytes); + Ok(u64::from_ne_bytes(bytes)) + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> { self.blocking_fill_bytes(dest); Ok(()) } diff --git a/examples/nrf-rtos-trace/Cargo.toml b/examples/nrf-rtos-trace/Cargo.toml index 98a678815..8f8002909 100644 --- a/examples/nrf-rtos-trace/Cargo.toml +++ b/examples/nrf-rtos-trace/Cargo.toml @@ -23,7 +23,7 @@ embassy-nrf = { version = "0.2.0", path = "../../embassy-nrf", features = ["nrf5 cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } cortex-m-rt = "0.7.0" panic-probe = { version = "0.3" } -rand = { version = "0.8.4", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } serde = { version = "1.0.136", default-features = false } rtos-trace = "0.1.3" systemview-target = { version = "0.1.2", features = ["callbacks-app", "callbacks-os", "log", "cortex-m"] } diff --git a/examples/nrf52840/Cargo.toml b/examples/nrf52840/Cargo.toml index 17fa6234d..9aba827f2 100644 --- a/examples/nrf52840/Cargo.toml +++ b/examples/nrf52840/Cargo.toml @@ -25,7 +25,7 @@ static_cell = { version = "2" } cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } cortex-m-rt = "0.7.0" panic-probe = { version = "0.3", features = ["print-defmt"] } -rand = { version = "0.8.4", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } embedded-storage = "0.3.1" usbd-hid = "0.8.1" serde = { version = "1.0.136", default-features = false } diff --git a/examples/nrf5340/Cargo.toml b/examples/nrf5340/Cargo.toml index 0da85be07..bba8c229b 100644 --- a/examples/nrf5340/Cargo.toml +++ b/examples/nrf5340/Cargo.toml @@ -21,7 +21,7 @@ static_cell = "2" cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } cortex-m-rt = "0.7.0" panic-probe = { version = "0.3", features = ["print-defmt"] } -rand = { version = "0.8.4", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } embedded-storage = "0.3.1" usbd-hid = "0.8.1" serde = { version = "1.0.136", default-features = false }