random: add tracking issue, address other comments
This commit is contained in:
parent
a21ff017f4
commit
e94dd9b712
@ -394,7 +394,7 @@ pub mod assert_matches {
|
|||||||
#[unstable(feature = "core_pattern_types", issue = "123646")]
|
#[unstable(feature = "core_pattern_types", issue = "123646")]
|
||||||
pub mod pat;
|
pub mod pat;
|
||||||
pub mod pin;
|
pub mod pin;
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub mod random;
|
pub mod random;
|
||||||
#[unstable(feature = "new_range_api", issue = "125687")]
|
#[unstable(feature = "new_range_api", issue = "125687")]
|
||||||
pub mod range;
|
pub mod range;
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
//! given [`RandomSource`].
|
//! given [`RandomSource`].
|
||||||
|
|
||||||
/// A source of randomness.
|
/// A source of randomness.
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub trait RandomSource {
|
pub trait RandomSource {
|
||||||
/// Fills `bytes` with random bytes.
|
/// Fills `bytes` with random bytes.
|
||||||
fn fill_bytes(&mut self, bytes: &mut [u8]);
|
fn fill_bytes(&mut self, bytes: &mut [u8]);
|
||||||
@ -17,7 +17,7 @@ pub trait RandomSource {
|
|||||||
/// distribution, so a value of 1 is just as likely as [`i32::MAX`]. By using
|
/// distribution, so a value of 1 is just as likely as [`i32::MAX`]. By using
|
||||||
/// modulo operations, some of the resulting values can become more likely than
|
/// modulo operations, some of the resulting values can become more likely than
|
||||||
/// others. Use audited crates when in doubt.
|
/// others. Use audited crates when in doubt.
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub trait Random: Sized {
|
pub trait Random: Sized {
|
||||||
/// Generates a random value.
|
/// Generates a random value.
|
||||||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self;
|
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self;
|
||||||
|
@ -597,7 +597,7 @@
|
|||||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||||
pub mod pipe;
|
pub mod pipe;
|
||||||
pub mod process;
|
pub mod process;
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub mod random;
|
pub mod random;
|
||||||
pub mod sync;
|
pub mod sync;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
//! The [`Random`] trait allows generating a random value for a type using a
|
//! The [`Random`] trait allows generating a random value for a type using a
|
||||||
//! given [`RandomSource`].
|
//! given [`RandomSource`].
|
||||||
|
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub use core::random::*;
|
pub use core::random::*;
|
||||||
|
|
||||||
use crate::sys::random as sys;
|
use crate::sys::random as sys;
|
||||||
@ -15,9 +15,9 @@
|
|||||||
/// documentation below for the specific guarantees your target provides.
|
/// documentation below for the specific guarantees your target provides.
|
||||||
///
|
///
|
||||||
/// The high quality of randomness provided by this source means it can be quite
|
/// The high quality of randomness provided by this source means it can be quite
|
||||||
/// slow. If you need a large quantity of random numbers and security is not a
|
/// slow on some targets. If you need a large quantity of random numbers and
|
||||||
/// concern, consider using an alternative random number generator (potentially
|
/// security is not a concern, consider using an alternative random number
|
||||||
/// seeded from this one).
|
/// generator (potentially seeded from this one).
|
||||||
///
|
///
|
||||||
/// # Underlying sources
|
/// # Underlying sources
|
||||||
///
|
///
|
||||||
@ -26,9 +26,9 @@
|
|||||||
/// Linux | [`getrandom`] or [`/dev/urandom`] after polling `/dev/random`
|
/// Linux | [`getrandom`] or [`/dev/urandom`] after polling `/dev/random`
|
||||||
/// Windows | [`ProcessPrng`](https://learn.microsoft.com/en-us/windows/win32/seccng/processprng)
|
/// Windows | [`ProcessPrng`](https://learn.microsoft.com/en-us/windows/win32/seccng/processprng)
|
||||||
/// Apple | `CCRandomGenerateBytes`
|
/// Apple | `CCRandomGenerateBytes`
|
||||||
/// DragonFly | [`arc4random_buf`](https://man.dragonflybsd.org/?command=arc4random§ion=ANY)
|
/// DragonFly | [`arc4random_buf`](https://man.dragonflybsd.org/?command=arc4random)
|
||||||
/// ESP-IDF | [`esp_fill_random`](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html#_CPPv415esp_fill_randomPv6size_t)
|
/// ESP-IDF | [`esp_fill_random`](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html#_CPPv415esp_fill_randomPv6size_t)
|
||||||
/// FreeBSD | [`arc4random_buf`](https://man.freebsd.org/cgi/man.cgi?query=arc4random&apropos=0&sektion=0&manpath=FreeBSD+15.0-CURRENT&arch=default&format=html)
|
/// FreeBSD | [`arc4random_buf`](https://man.freebsd.org/cgi/man.cgi?query=arc4random)
|
||||||
/// Fuchsia | [`cprng_draw`](https://fuchsia.dev/reference/syscalls/cprng_draw)
|
/// Fuchsia | [`cprng_draw`](https://fuchsia.dev/reference/syscalls/cprng_draw)
|
||||||
/// Haiku | `arc4random_buf`
|
/// Haiku | `arc4random_buf`
|
||||||
/// Illumos | [`arc4random_buf`](https://www.illumos.org/man/3C/arc4random)
|
/// Illumos | [`arc4random_buf`](https://www.illumos.org/man/3C/arc4random)
|
||||||
@ -48,15 +48,19 @@
|
|||||||
/// WASI | [`random_get`](https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-random_getbuf-pointeru8-buf_len-size---result-errno)
|
/// WASI | [`random_get`](https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-random_getbuf-pointeru8-buf_len-size---result-errno)
|
||||||
/// ZKVM | `sys_rand`
|
/// ZKVM | `sys_rand`
|
||||||
///
|
///
|
||||||
/// **Disclaimer:** The sources used might change over time.
|
/// Note that the sources used might change over time.
|
||||||
|
///
|
||||||
|
/// Consult the documentation for the underlying operations on your supported
|
||||||
|
/// targets to determine whether they provide any particular desired properties,
|
||||||
|
/// such as support for reseeding on VM fork operations.
|
||||||
///
|
///
|
||||||
/// [`getrandom`]: https://www.man7.org/linux/man-pages/man2/getrandom.2.html
|
/// [`getrandom`]: https://www.man7.org/linux/man-pages/man2/getrandom.2.html
|
||||||
/// [`/dev/urandom`]: https://www.man7.org/linux/man-pages/man4/random.4.html
|
/// [`/dev/urandom`]: https://www.man7.org/linux/man-pages/man4/random.4.html
|
||||||
#[derive(Default, Debug, Clone, Copy)]
|
#[derive(Default, Debug, Clone, Copy)]
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub struct DefaultRandomSource;
|
pub struct DefaultRandomSource;
|
||||||
|
|
||||||
#[unstable(feature = "random", issue = "none")]
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
impl RandomSource for DefaultRandomSource {
|
impl RandomSource for DefaultRandomSource {
|
||||||
fn fill_bytes(&mut self, bytes: &mut [u8]) {
|
fn fill_bytes(&mut self, bytes: &mut [u8]) {
|
||||||
sys::fill_bytes(bytes)
|
sys::fill_bytes(bytes)
|
||||||
@ -83,7 +87,7 @@ fn fill_bytes(&mut self, bytes: &mut [u8]) {
|
|||||||
///
|
///
|
||||||
/// use std::random::random;
|
/// use std::random::random;
|
||||||
///
|
///
|
||||||
/// let bits = random::<u128>();
|
/// let bits: u128 = random();
|
||||||
/// let g1 = (bits >> 96) as u32;
|
/// let g1 = (bits >> 96) as u32;
|
||||||
/// let g2 = (bits >> 80) as u16;
|
/// let g2 = (bits >> 80) as u16;
|
||||||
/// let g3 = (0x4000 | (bits >> 64) & 0x0fff) as u16;
|
/// let g3 = (0x4000 | (bits >> 64) & 0x0fff) as u16;
|
||||||
@ -94,6 +98,7 @@ fn fill_bytes(&mut self, bytes: &mut [u8]) {
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [version 4/variant 1 UUID]: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
|
/// [version 4/variant 1 UUID]: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
|
||||||
|
#[unstable(feature = "random", issue = "130703")]
|
||||||
pub fn random<T: Random>() -> T {
|
pub fn random<T: Random>() -> T {
|
||||||
T::random(&mut DefaultRandomSource)
|
T::random(&mut DefaultRandomSource)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user