-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathsys_rng.rs
More file actions
98 lines (87 loc) · 2.68 KB
/
sys_rng.rs
File metadata and controls
98 lines (87 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::Error;
use rand_core::{CryptoRng, RngCore, TryCryptoRng, TryRngCore};
/// A [`TryRngCore`] interface over the system's preferred random number source.
///
/// This is a zero-sized struct. It can be freely constructed with just `SysRng`.
///
/// This struct is also available as [`rand::rngs::SysRng`] when using [`rand`].
///
/// If you don't care about potential (but extremely unlikely in practice) errors,
/// you can use [`UnwrappingSysRng`] instead.
///
/// # Usage example
///
/// `SysRng` implements [`TryRngCore`]:
/// ```
/// use getrandom::{rand_core::TryRngCore, SysRng};
///
/// # fn main() -> Result<(), getrandom::Error> {
/// let mut key = [0u8; 32];
/// SysRng.try_fill_bytes(&mut key)?;
///
/// let x: u32 = SysRng.try_next_u32()?;
/// let y: u64 = SysRng.try_next_u64()?;
/// # Ok(()) }
/// ```
///
/// [`rand`]: https://crates.io/crates/rand
/// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html
#[derive(Clone, Copy, Debug, Default)]
pub struct SysRng;
impl TryRngCore for SysRng {
type Error = Error;
#[inline]
fn try_next_u32(&mut self) -> Result<u32, Error> {
crate::u32()
}
#[inline]
fn try_next_u64(&mut self) -> Result<u64, Error> {
crate::u64()
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
crate::fill(dest)
}
}
impl TryCryptoRng for SysRng {}
/// A potentially-panicking [`RngCore`] interface over the system's preferred random number source.
///
/// This is a zero-sized struct. It can be freely constructed with just `UnwrappingSysRng`.
///
/// If possible, we recommend to use [`SysRng`] instead and to properly handle potential errors.
///
/// This struct is also available as [`rand::rngs::UnwrappingSysRng`] when using [`rand`].
///
/// # Usage example
///
/// `UnwrappingSysRng` implements [`RngCore`]:
/// ```
/// use getrandom::{rand_core::RngCore, UnwrappingSysRng};
///
/// let mut key = [0u8; 32];
/// UnwrappingSysRng.fill_bytes(&mut key);
///
/// let x: u32 = UnwrappingSysRng.next_u32();
/// let y: u64 = UnwrappingSysRng.next_u64();
/// ```
///
/// [`rand`]: https://crates.io/crates/rand
/// [`rand::rngs::UnwrappingSysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.UnwrappingSysRng.html
/// [`RngCore`]: rand_core::RngCore
#[derive(Clone, Copy, Debug, Default)]
pub struct UnwrappingSysRng;
impl RngCore for UnwrappingSysRng {
#[inline]
fn next_u32(&mut self) -> u32 {
crate::u32().unwrap()
}
#[inline]
fn next_u64(&mut self) -> u64 {
crate::u64().unwrap()
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
crate::fill(dest).unwrap()
}
}
impl CryptoRng for UnwrappingSysRng {}