From eb9d7c8711aff50ce0ab84edcc75d964bc8b78b0 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 30 May 2026 10:50:41 -0700 Subject: [PATCH] Stabilize `RandomSource` and `DefaultRandomSource` This provides enough of an interface for people to obtain random bytes. The `Distribution` trait and the `random` function remain unstable; those don't need to block stabilization of `RandomSource` and `DefaultRandomSource`. Similarly, this leaves a `fill_buf` function using `BorrowedCursor` as future work. --- library/core/src/random.rs | 3 ++- library/std/src/random.rs | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/library/core/src/random.rs b/library/core/src/random.rs index 06f4f30efe2b5..0bb7b0b01aead 100644 --- a/library/core/src/random.rs +++ b/library/core/src/random.rs @@ -3,7 +3,7 @@ use crate::ops::RangeFull; /// A source of randomness. -#[unstable(feature = "random", issue = "130703")] +#[stable(feature = "random_source", since = "CURRENT_RUSTC_VERSION")] pub trait RandomSource { /// Fills `bytes` with random bytes. /// @@ -11,6 +11,7 @@ pub trait RandomSource { /// with a larger buffer. A `RandomSource` is allowed to return different bytes for those two /// cases. For instance, this allows a `RandomSource` to generate a word at a time and throw /// part of it away if not needed. + #[stable(feature = "random_source", since = "CURRENT_RUSTC_VERSION")] fn fill_bytes(&mut self, bytes: &mut [u8]); } diff --git a/library/std/src/random.rs b/library/std/src/random.rs index a18dcf98ec7fc..1e11451e8d493 100644 --- a/library/std/src/random.rs +++ b/library/std/src/random.rs @@ -1,7 +1,9 @@ //! Random value generation. #[unstable(feature = "random", issue = "130703")] -pub use core::random::*; +pub use core::random::Distribution; +#[stable(feature = "random_source", since = "CURRENT_RUSTC_VERSION")] +pub use core::random::RandomSource; use crate::sys::random as sys; @@ -55,10 +57,10 @@ use crate::sys::random as sys; /// [`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 #[derive(Default, Debug, Clone, Copy)] -#[unstable(feature = "random", issue = "130703")] +#[stable(feature = "random_source", since = "CURRENT_RUSTC_VERSION")] pub struct DefaultRandomSource; -#[unstable(feature = "random", issue = "130703")] +#[stable(feature = "random_source", since = "CURRENT_RUSTC_VERSION")] impl RandomSource for DefaultRandomSource { fn fill_bytes(&mut self, bytes: &mut [u8]) { sys::fill_bytes(bytes)