Skip to content

Commit 1c7212b

Browse files
authored
Merge pull request #203 from datdenkikniet/implement_rngcore
Implement RngCore for Rng
2 parents 683c781 + a7b1c14 commit 1c7212b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ stm32l4 = "0.13.0"
2727
as-slice = "0.1"
2828
generic-array = "0.13"
2929

30+
[dependencies.rand_core]
31+
version = "0.6.2"
32+
default-features = false
33+
3034
[dependencies.cast]
3135
version = "0.2.2"
3236
default-features = false

src/rng.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::cmp;
44

55
use crate::rcc::{Clocks, AHB2};
66
use crate::stm32::RNG;
7+
pub use rand_core::RngCore;
78

89
/// Extension trait to activate the RNG
910
pub trait RngExt {
@@ -91,6 +92,24 @@ impl Rng {
9192
}
9293
}
9394

95+
impl RngCore for Rng {
96+
fn next_u32(&mut self) -> u32 {
97+
self.get_random_data()
98+
}
99+
100+
fn next_u64(&mut self) -> u64 {
101+
rand_core::impls::next_u64_via_u32(self)
102+
}
103+
104+
fn fill_bytes(&mut self, dest: &mut [u8]) {
105+
rand_core::impls::fill_bytes_via_next(self, dest)
106+
}
107+
108+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
109+
Ok(self.fill_bytes(dest))
110+
}
111+
}
112+
94113
#[derive(Debug)]
95114
pub enum Error {}
96115

0 commit comments

Comments
 (0)