Skip to content

Commit

Permalink
Remove rand_trng::rng function
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 30, 2024
1 parent d47d72a commit b1ac355
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
52 changes: 24 additions & 28 deletions rand_trng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use std::fmt;
use std::rc::Rc;
use std::thread_local;

pub use rand_core;
pub use rand_chacha::rand_core::{self, CryptoRng, RngCore};

use rand_chacha::ChaCha12Rng;
use rand_core::{CryptoRng, RngCore, SeedableRng};
use rand_chacha::{rand_core::SeedableRng, ChaCha12Rng};

// Number of generated bytes after which to reseed `ThreadRng`.
// According to benchmarks, reseeding has a noticeable impact with thresholds
Expand Down Expand Up @@ -80,6 +79,20 @@ thread_local!(
/// [`ThreadRng::default()`].
/// The handle cannot be passed between threads (is not `Send` or `Sync`).
///
/// # Example
///
/// ```
/// use rand_trng::{ThreadRng, RngCore};
///
/// let mut rng = ThreadRng::new();
///
/// let random_u32 = rng.next_u32();
/// let random_u64 = rng.next_u64();
///
/// let mut buf = [0u8; 32];
/// rng.fill_bytes(&mut buf);
/// ```
///
/// # Security
///
/// Security must be considered relative to a threat model and validation
Expand Down Expand Up @@ -129,6 +142,14 @@ pub struct ThreadRng {
}

impl ThreadRng {
/// Get [`ThreadRng`] instance.
///
/// Note that this method usually returns instance which references already initialized
/// thread-local state.
pub fn new() -> Self {
Default::default()
}

/// Immediately reseed the generator
///
/// This discards any remaining random data in the cache.
Expand All @@ -146,31 +167,6 @@ impl fmt::Debug for ThreadRng {
}
}

/// Access a fast, pre-initialized generator
///
/// This is a handle to the local [`ThreadRng`].
///
/// # Example
///
/// ```
/// use rand_trng::rand_core::RngCore;
///
/// let mut rng = rand_trng::rng();
///
/// let random_u32 = rng.next_u32();
/// let random_u64 = rng.next_u64();
///
/// let mut buf = [0u8; 32];
/// rng.fill_bytes(&mut buf);
/// ```
///
/// # Security
///
/// Refer to [`ThreadRng#Security`].
pub fn rng() -> ThreadRng {
Default::default()
}

impl Default for ThreadRng {
fn default() -> Self {
let rng = THREAD_RNG_KEY.with(|t| t.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/rngs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub use self::small::SmallRng;
#[cfg(feature = "std_rng")]
pub use self::std::StdRng;
#[cfg(feature = "thread_rng")]
pub use rand_trng::{rng, ThreadRng};
pub use rand_trng::ThreadRng;

#[cfg(feature = "getrandom")]
pub use rand_core::OsRng;

0 comments on commit b1ac355

Please sign in to comment.