diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 3e66d6886c..ce5cbe32d8 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 ff61e08f32..d61a027ecf 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) } +} + +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<(), rand_core::Error> { + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> { self.blocking_fill_bytes(dest); Ok(()) } diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 29a8a3c53a..7935bb2b57 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml @@ -128,7 +128,7 @@ embedded-io = { version = "0.6.1" } embedded-io-async = { version = "0.6.1" } embedded-storage = { version = "0.3" } embedded-storage-async = { version = "0.4.1" } -rand_core = "0.6.4" +rand_core = "0.9.0-alpha.2" fixed = "1.23.1" rp-pac = { git = "https://github.com/embassy-rs/rp-pac.git", rev = "a7f42d25517f7124ad3b4ed492dec8b0f50a0e6c", feature = ["rt"] } diff --git a/embassy-rp/src/clocks.rs b/embassy-rp/src/clocks.rs index ed146844c2..0f8358cf4d 100644 --- a/embassy-rp/src/clocks.rs +++ b/embassy-rp/src/clocks.rs @@ -2,6 +2,7 @@ #[cfg(feature = "rp2040")] use core::arch::asm; +use core::convert::Infallible; use core::marker::PhantomData; #[cfg(feature = "rp2040")] use core::sync::atomic::AtomicU16; @@ -9,6 +10,7 @@ use core::sync::atomic::{AtomicU32, Ordering}; use embassy_hal_internal::{into_ref, PeripheralRef}; use pac::clocks::vals::*; +use rand_core::RngCore; use crate::gpio::{AnyPin, SealedPin}; #[cfg(feature = "rp2040")] @@ -1032,10 +1034,6 @@ impl RoscRng { } impl rand_core::RngCore for RoscRng { - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { - Ok(self.fill_bytes(dest)) - } - fn next_u32(&mut self) -> u32 { rand_core::impls::next_u32_via_fill(self) } @@ -1048,6 +1046,22 @@ impl rand_core::RngCore for RoscRng { dest.fill_with(Self::next_u8) } } + +impl rand_core::TryRngCore for RoscRng { + type Error = Infallible; + + fn try_next_u32(&mut self) -> Result { + Ok(self.next_u32()) + } + + fn try_next_u64(&mut self) -> Result { + Ok(self.next_u64()) + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> { + Ok(self.fill_bytes(dest)) + } +} /// Enter the `DORMANT` sleep state. This will stop *all* internal clocks /// and can only be exited through resets, dormant-wake GPIO interrupts, /// and RTC interrupts. If RTC is clocked from an internal clock source diff --git a/embassy-rp/src/trng.rs b/embassy-rp/src/trng.rs index 9f2f33c4b0..d1a36240b6 100644 --- a/embassy-rp/src/trng.rs +++ b/embassy-rp/src/trng.rs @@ -1,5 +1,6 @@ //! True Random Number Generator (TRNG) driver. +use core::convert::Infallible; use core::future::poll_fn; use core::marker::PhantomData; use core::ops::Not; @@ -7,7 +8,6 @@ use core::task::Poll; use embassy_hal_internal::Peripheral; use embassy_sync::waitqueue::AtomicWaker; -use rand_core::Error; use crate::interrupt::typelevel::{Binding, Interrupt}; use crate::peripherals::TRNG; @@ -366,12 +366,24 @@ impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> { fn fill_bytes(&mut self, dest: &mut [u8]) { self.blocking_fill_bytes(dest) } +} +impl<'d, T: Instance> rand_core::TryRngCore for Trng<'d, T> { + type Error = Infallible; + + fn try_next_u32(&mut self) -> Result { + Ok(self.blocking_next_u32()) + } - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + fn try_next_u64(&mut self) -> Result { + Ok(self.blocking_next_u64()) + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> { self.blocking_fill_bytes(dest); Ok(()) } } + /// TRNG interrupt handler. pub struct InterruptHandler { _trng: PhantomData, diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 3c6484c966..94bbcab8ac 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -68,7 +68,7 @@ log = { version = "0.4.14", optional = true } cortex-m-rt = ">=0.6.15,<0.8" cortex-m = "0.7.6" futures-util = { version = "0.3.30", default-features = false } -rand_core = "0.6.3" +rand_core = "0.9.0-alpha.2" sdio-host = "0.5.0" critical-section = "1.1" #stm32-metapac = { version = "15" } diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 6f4c81c8ac..a7aabc16a7 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -1,13 +1,14 @@ //! Random Number Generator (RNG) #![macro_use] +use core::convert::Infallible; use core::future::poll_fn; use core::marker::PhantomData; use core::task::Poll; use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_sync::waitqueue::AtomicWaker; -use rand_core::{CryptoRng, RngCore}; +use rand_core::{CryptoRng, RngCore, TryRngCore}; use crate::interrupt::typelevel::Interrupt; use crate::{interrupt, pac, peripherals, rcc, Peripheral}; @@ -213,9 +214,21 @@ impl<'d, T: Instance> RngCore for Rng<'d, T> { } } } +} + +impl<'d, T: crate::rng::Instance> TryRngCore for Rng<'d, T> { + type Error = Infallible; + + fn try_next_u32(&mut self) -> Result { + Ok(self.next_u32()) + } + + fn try_next_u64(&mut self) -> Result { + Ok(self.next_u64()) + } - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { - self.fill_bytes(dest); + fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> { + self.fill_bytes(dst); Ok(()) } } diff --git a/examples/nrf-rtos-trace/Cargo.toml b/examples/nrf-rtos-trace/Cargo.toml index 98a6788151..8f80029091 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 17fa6234d3..9aba827f2a 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 0da85be079..bba8c229ba 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 } diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index 04b4c6317e..7134c5fa2e 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml @@ -56,7 +56,7 @@ portable-atomic = { version = "1.5", features = ["critical-section"] } log = "0.4" pio-proc = "0.2" pio = "0.2.1" -rand = { version = "0.8.5", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } embedded-sdmmc = "0.7.0" bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] } diff --git a/examples/rp23/Cargo.toml b/examples/rp23/Cargo.toml index 087f6fd698..45b51315e6 100644 --- a/examples/rp23/Cargo.toml +++ b/examples/rp23/Cargo.toml @@ -56,7 +56,7 @@ portable-atomic = { version = "1.5", features = ["critical-section"] } log = "0.4" pio-proc = "0.2" pio = "0.2.1" -rand = { version = "0.8.5", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } embedded-sdmmc = "0.7.0" bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] } diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index 8c591ebd26..70890189b9 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml @@ -24,7 +24,7 @@ embedded-hal = "0.2.6" panic-probe = { version = "0.3", features = ["print-defmt"] } heapless = { version = "0.8", default-features = false } nb = "1.0.0" -rand_core = "0.6.3" +rand_core = "0.9.0-alpha.2" critical-section = "1.1" embedded-storage = "0.3.1" static_cell = "2" diff --git a/examples/stm32h5/Cargo.toml b/examples/stm32h5/Cargo.toml index 30b1d2be91..d039f956d7 100644 --- a/examples/stm32h5/Cargo.toml +++ b/examples/stm32h5/Cargo.toml @@ -26,7 +26,7 @@ embedded-io-async = { version = "0.6.1" } embedded-nal-async = { version = "0.7.1" } panic-probe = { version = "0.3", features = ["print-defmt"] } heapless = { version = "0.8", default-features = false } -rand_core = "0.6.3" +rand_core = "0.9.0-alpha.2" critical-section = "1.1" micromath = "2.0.0" stm32-fmc = "0.3.0" diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 13fce7dc79..d7b6ff8d32 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml @@ -27,7 +27,7 @@ embedded-nal-async = { version = "0.7.1" } embedded-io-async = { version = "0.6.1" } panic-probe = { version = "0.3", features = ["print-defmt"] } heapless = { version = "0.8", default-features = false } -rand_core = "0.6.3" +rand_core = "0.9.0-alpha.2" critical-section = "1.1" micromath = "2.0.0" stm32-fmc = "0.3.0" diff --git a/examples/stm32h755cm4/Cargo.toml b/examples/stm32h755cm4/Cargo.toml index 7a42fbdaae..9876fa7d8f 100644 --- a/examples/stm32h755cm4/Cargo.toml +++ b/examples/stm32h755cm4/Cargo.toml @@ -27,7 +27,7 @@ embedded-nal-async = { version = "0.7.1" } embedded-io-async = { version = "0.6.1" } panic-probe = { version = "0.3", features = ["print-defmt"] } heapless = { version = "0.8", default-features = false } -rand_core = "0.6.3" +rand_core = "0.9.0-alpha.2" critical-section = "1.1" micromath = "2.0.0" stm32-fmc = "0.3.0" diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index c5478b17ba..f73abab571 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml @@ -30,7 +30,7 @@ embedded-hal-bus = { version = "0.1", features = ["async"] } panic-probe = { version = "0.3", features = ["print-defmt"] } heapless = { version = "0.8", default-features = false } chrono = { version = "^0.4", default-features = false } -rand = { version = "0.8.5", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } static_cell = "2" micromath = "2.0.0" diff --git a/examples/stm32l5/Cargo.toml b/examples/stm32l5/Cargo.toml index 16c184de23..444ce8265a 100644 --- a/examples/stm32l5/Cargo.toml +++ b/examples/stm32l5/Cargo.toml @@ -23,7 +23,7 @@ cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-sing cortex-m-rt = "0.7.0" embedded-hal = "0.2.6" heapless = { version = "0.8", default-features = false } -rand_core = { version = "0.6.3", default-features = false } +rand_core = { version = "0.9.0-alpha.2", default-features = false } embedded-io-async = { version = "0.6.1" } static_cell = "2" diff --git a/tests/rp/Cargo.toml b/tests/rp/Cargo.toml index 12f1ec3ce5..08c440fb04 100644 --- a/tests/rp/Cargo.toml +++ b/tests/rp/Cargo.toml @@ -35,7 +35,7 @@ static_cell = "2" portable-atomic = { version = "1.5", features = ["critical-section"] } pio = "0.2" pio-proc = "0.2" -rand = { version = "0.8.5", default-features = false } +rand = { version = "0.9.0-alpha.2", default-features = false } [profile.dev] debug = 2 diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 2eac636e5b..6edf3db11f 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml @@ -79,8 +79,8 @@ embedded-hal-async = { version = "1.0" } embedded-can = { version = "0.4" } micromath = "2.0.0" panic-probe = { version = "0.3.0", features = ["print-defmt"] } -rand_core = { version = "0.6", default-features = false } -rand_chacha = { version = "0.3", default-features = false } +rand_core = { version = "0.9.0-alpha.2", default-features = false } +rand_chacha = { version = "0.9.0-alpha.2", default-features = false } static_cell = "2" portable-atomic = { version = "1.5", features = [] }