Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Rename fns `IndexedRandom::choose_multiple` -> `sample`, `choose_multiple_array` -> `sample_array`, `choose_multiple_weighted` -> `sample_weighted`, struct `SliceChooseIter` -> `IndexedSamples` and fns `IteratorRandom::choose_multiple` -> `sample`, `choose_multiple_fill` -> `sample_fill` (#1632)
- Use Edition 2024 and MSRV 1.85 (#1653)
- Let `Fill` be implemented for element types, not sliceable types (#1652)
- Replace fn `TryRngCore::read_adapter(..) -> RngReadAdapter` with simpler struct `RngReader` (#1669)

### Additions
- Add fns `IndexedRandom::choose_iter`, `choose_weighted_iter` (#1632)
Expand Down
1 change: 1 addition & 0 deletions rand_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Relax `Sized` bound on impls of `SeedableRng` (#1641)
- Move `rand_core::impls::*` to `rand_core::le` module (#1667)
- Use Edition 2024 and MSRV 1.85 (#1668)
- Remove fn `TryRngCore::read_adapter(..) -> RngReadAdapter` (replaced with `rand::RngReader`) (#1669)

## [0.9.3] — 2025-02-29
### Other
Expand Down
43 changes: 0 additions & 43 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,6 @@ pub trait TryRngCore {
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self> {
UnwrapMut(self)
}

/// Convert an [`RngCore`] to a [`RngReadAdapter`].
#[cfg(feature = "std")]
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
where
Self: Sized,
{
RngReadAdapter { inner: self }
}
}

// Note that, unfortunately, this blanket impl prevents us from implementing
Expand Down Expand Up @@ -543,40 +534,6 @@ pub trait SeedableRng: Sized {
}
}

/// Adapter that enables reading through a [`io::Read`](std::io::Read) from a [`RngCore`].
///
/// # Examples
///
/// ```no_run
/// # use std::{io, io::Read};
/// # use std::fs::File;
/// # use rand_core::{OsRng, TryRngCore};
///
/// io::copy(&mut OsRng.read_adapter().take(100), &mut File::create("/tmp/random.bytes").unwrap()).unwrap();
/// ```
#[cfg(feature = "std")]
pub struct RngReadAdapter<'a, R: TryRngCore + ?Sized> {
inner: &'a mut R,
}

#[cfg(feature = "std")]
impl<R: TryRngCore + ?Sized> std::io::Read for RngReadAdapter<'_, R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.inner
.try_fill_bytes(buf)
.map_err(|err| std::io::Error::other(std::format!("RNG error: {err}")))?;
Ok(buf.len())
}
}

#[cfg(feature = "std")]
impl<R: TryRngCore + ?Sized> std::fmt::Debug for RngReadAdapter<'_, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ReadAdapter").finish()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
53 changes: 53 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ pub use rng::{Fill, Rng};
#[cfg(feature = "thread_rng")]
use crate::distr::{Distribution, StandardUniform};

/// Adapter to support [`std::io::Read`] over a [`TryRngCore`]
///
/// # Examples
///
/// ```no_run
/// use std::{io, io::Read};
/// use std::fs::File;
/// use rand::{rngs::OsRng, RngReader};
///
/// io::copy(
/// &mut RngReader(OsRng).take(100),
/// &mut File::create("/tmp/random.bytes").unwrap()
/// ).unwrap();
/// ```
#[cfg(feature = "std")]
pub struct RngReader<R: TryRngCore>(pub R);

#[cfg(feature = "std")]
impl<R: TryRngCore> std::io::Read for RngReader<R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.0
.try_fill_bytes(buf)
.map_err(|err| std::io::Error::other(std::format!("RNG error: {err}")))?;
Ok(buf.len())
}
}

#[cfg(feature = "std")]
impl<R: TryRngCore> std::fmt::Debug for RngReader<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("RngReader").finish()
}
}

/// Generate a random value using the thread-local random number generator.
///
/// This function is shorthand for <code>[rng()].[random()](Rng::random)</code>:
Expand Down Expand Up @@ -337,6 +372,24 @@ mod test {
}
}

#[cfg(feature = "std")]
#[test]
fn rng_reader() {
use std::io::Read;

let mut rng = StepRng(255, 1);
let mut buf = [0u8; 24];
let expected = [
255, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
];

RngReader(&mut rng).read_exact(&mut buf).unwrap();
assert_eq!(&buf, &expected);

RngReader(StepRng(255, 1)).read_exact(&mut buf).unwrap();
assert_eq!(&buf, &expected);
}

#[test]
#[cfg(feature = "thread_rng")]
fn test_random() {
Expand Down