Skip to content

Commit 8c5840f

Browse files
committed
Move RngReader to rand
1 parent 1d76be5 commit 8c5840f

File tree

2 files changed

+53
-66
lines changed

2 files changed

+53
-66
lines changed

rand_core/src/lib.rs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -534,41 +534,6 @@ pub trait SeedableRng: Sized {
534534
}
535535
}
536536

537-
/// Adapter to support [`std::io::Read`] over a [`TryRngCore`]
538-
///
539-
/// # Examples
540-
///
541-
/// ```no_run
542-
/// use std::{io, io::Read};
543-
/// use std::fs::File;
544-
/// use rand_core::{OsRng, RngReader};
545-
///
546-
/// io::copy(
547-
/// &mut RngReader(OsRng).take(100),
548-
/// &mut File::create("/tmp/random.bytes").unwrap()
549-
/// ).unwrap();
550-
/// ```
551-
#[cfg(feature = "std")]
552-
pub struct RngReader<R: TryRngCore>(pub R);
553-
554-
#[cfg(feature = "std")]
555-
impl<R: TryRngCore> std::io::Read for RngReader<R> {
556-
#[inline]
557-
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
558-
self.0
559-
.try_fill_bytes(buf)
560-
.map_err(|err| std::io::Error::other(std::format!("RNG error: {err}")))?;
561-
Ok(buf.len())
562-
}
563-
}
564-
565-
#[cfg(feature = "std")]
566-
impl<R: TryRngCore> std::fmt::Debug for RngReader<R> {
567-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
568-
f.debug_tuple("RngReader").finish()
569-
}
570-
}
571-
572537
#[cfg(test)]
573538
mod test {
574539
use super::*;
@@ -729,35 +694,4 @@ mod test {
729694
}
730695
assert_eq!(rng.next_u32(), 4);
731696
}
732-
733-
struct StepRng(u32, u32);
734-
impl RngCore for StepRng {
735-
fn next_u32(&mut self) -> u32 {
736-
let x = self.0;
737-
self.0 += self.1;
738-
x
739-
}
740-
fn next_u64(&mut self) -> u64 {
741-
le::next_u64_via_u32(self)
742-
}
743-
fn fill_bytes(&mut self, dest: &mut [u8]) {
744-
le::fill_bytes_via_next(self, dest);
745-
}
746-
}
747-
748-
#[cfg(feature = "std")]
749-
#[test]
750-
fn rng_reader() {
751-
use std::io::Read;
752-
753-
let mut rng = StepRng(255, 1);
754-
let mut buf = [0u8; 16];
755-
let expected = [255, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 2, 1, 0, 0];
756-
757-
RngReader(&mut rng).read_exact(&mut buf).unwrap();
758-
assert_eq!(&buf, &expected);
759-
760-
RngReader(StepRng(255, 1)).read_exact(&mut buf).unwrap();
761-
assert_eq!(&buf, &expected);
762-
}
763697
}

src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,41 @@ pub use rng::{Fill, Rng};
129129
#[cfg(feature = "thread_rng")]
130130
use crate::distr::{Distribution, StandardUniform};
131131

132+
/// Adapter to support [`std::io::Read`] over a [`TryRngCore`]
133+
///
134+
/// # Examples
135+
///
136+
/// ```no_run
137+
/// use std::{io, io::Read};
138+
/// use std::fs::File;
139+
/// use rand::{rngs::OsRng, RngReader};
140+
///
141+
/// io::copy(
142+
/// &mut RngReader(OsRng).take(100),
143+
/// &mut File::create("/tmp/random.bytes").unwrap()
144+
/// ).unwrap();
145+
/// ```
146+
#[cfg(feature = "std")]
147+
pub struct RngReader<R: TryRngCore>(pub R);
148+
149+
#[cfg(feature = "std")]
150+
impl<R: TryRngCore> std::io::Read for RngReader<R> {
151+
#[inline]
152+
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
153+
self.0
154+
.try_fill_bytes(buf)
155+
.map_err(|err| std::io::Error::other(std::format!("RNG error: {err}")))?;
156+
Ok(buf.len())
157+
}
158+
}
159+
160+
#[cfg(feature = "std")]
161+
impl<R: TryRngCore> std::fmt::Debug for RngReader<R> {
162+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163+
f.debug_tuple("RngReader").finish()
164+
}
165+
}
166+
132167
/// Generate a random value using the thread-local random number generator.
133168
///
134169
/// This function is shorthand for <code>[rng()].[random()](Rng::random)</code>:
@@ -337,6 +372,24 @@ mod test {
337372
}
338373
}
339374

375+
#[cfg(feature = "std")]
376+
#[test]
377+
fn rng_reader() {
378+
use std::io::Read;
379+
380+
let mut rng = StepRng(255, 1);
381+
let mut buf = [0u8; 24];
382+
let expected = [
383+
255, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
384+
];
385+
386+
RngReader(&mut rng).read_exact(&mut buf).unwrap();
387+
assert_eq!(&buf, &expected);
388+
389+
RngReader(StepRng(255, 1)).read_exact(&mut buf).unwrap();
390+
assert_eq!(&buf, &expected);
391+
}
392+
340393
#[test]
341394
#[cfg(feature = "thread_rng")]
342395
fn test_random() {

0 commit comments

Comments
 (0)