Description
Background
The embedded-hal
crate provides traits for embedded hardware. In #291 embedded-hal
removed their RNG trait in favor of rand_core
. embedded-hal
is now working on adding async
traits using GATs in #285.
This issue is really more of a question than a feature request: Is rand_core
the appropriate place to add async
RNG traits? If there are use-cases for async
RNG traits outside of embedded rust I think it would be a good idea to include this in rand_core
, otherwise it would probably be more appropriate for embedded-hal
.
What is your motivation?
Developing async
on embedded targets has been progressing nicely, and with GAT stabilization on the way it will soon be possible to write #![no_std]
friendly async
traits on rust stable.
HW RNG peripherals are fast, but can still benefit from async
methods that allow the CPU to yield to other tasks while waiting for the hardware to generate entropy. On the STM32WL (ARM Cortex-M4), generating a [u32; 4]
block of entropy takes ~2662 CPU cycles on a cold-boot, and ~412 cycles when a steady state is reached. An async
context switch takes a minimum of 62 cycles using the embassy executor.
What type of application is this?
Hardware entropy generation for embedded development on #![no_std]
targets.
Feature request
Add an async
RNG trait.
This would look something like this (prior art from embassy-rs):
use core::future::Future;
/// Random-number Generator
pub trait Rng {
type Error;
type RngFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
/// Completely fill the provided buffer with random bytes.
///
/// May result in delays if entropy is exhausted prior to completely
/// filling the buffer. Upon completion, the buffer will be completely
/// filled or an error will have been reported.
fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>;
}