Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump rand core to 0.9.0 #3360

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion embassy-nrf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 19 additions & 1 deletion embassy-nrf/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u32, Self::Error> {
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<u64, Self::Error> {
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(())
}
Expand Down
2 changes: 1 addition & 1 deletion embassy-rp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
22 changes: 18 additions & 4 deletions embassy-rp/src/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#[cfg(feature = "rp2040")]
use core::arch::asm;
use core::convert::Infallible;
use core::marker::PhantomData;
#[cfg(feature = "rp2040")]
use core::sync::atomic::AtomicU16;
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")]
Expand Down Expand Up @@ -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)
}
Expand All @@ -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<u32, Self::Error> {
Ok(self.next_u32())
}

fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
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
Expand Down
16 changes: 14 additions & 2 deletions embassy-rp/src/trng.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! True Random Number Generator (TRNG) driver.

use core::convert::Infallible;
use core::future::poll_fn;
use core::marker::PhantomData;
use core::ops::Not;
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;
Expand Down Expand Up @@ -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<u32, Self::Error> {
Ok(self.blocking_next_u32())
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
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<T: Instance> {
_trng: PhantomData<T>,
Expand Down
2 changes: 1 addition & 1 deletion embassy-stm32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
19 changes: 16 additions & 3 deletions embassy-stm32/src/rng.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<u32, Self::Error> {
Ok(self.next_u32())
}

fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
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(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nrf-rtos-trace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/nrf52840/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion examples/nrf5340/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion examples/rp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/rp23/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32f7/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32h5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32h7/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32h755cm4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32l4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32l5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion tests/rp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/stm32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [] }

Expand Down