1 // Copyright 2018 Developers of the Rand project.
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
9 //! Deprecated re-exports (we can't add deprecation warnings otherwise)
14 use {RngCore, CryptoRng, SeedableRng, Error}
;
15 use rand_core
::block
::BlockRngCore
;
23 #[derive(Clone, Debug)]
24 #[deprecated(since="0.6.0",
25 note
="import from rand_isaac crate instead, or use the newer Hc128Rng")]
26 pub struct IsaacRng(rand_isaac
::IsaacRng
);
28 impl RngCore
for IsaacRng
{
30 fn next_u32(&mut self) -> u32 {
35 fn next_u64(&mut self) -> u64 {
40 fn fill_bytes(&mut self, dest
: &mut [u8]) {
41 self.0.fill_bytes
(dest
);
45 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
46 self.0.try_fill_bytes(dest
)
50 impl SeedableRng
for IsaacRng
{
51 type Seed
= <rand_isaac
::IsaacRng
as SeedableRng
>::Seed
;
53 fn from_seed(seed
: Self::Seed
) -> Self {
54 IsaacRng(rand_isaac
::IsaacRng
::from_seed(seed
))
57 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
58 rand_isaac
::IsaacRng
::from_rng(rng
).map(IsaacRng
)
63 pub fn new_from_u64(seed
: u64) -> Self {
64 IsaacRng(rand_isaac
::IsaacRng
::new_from_u64(seed
))
69 #[derive(Clone, Debug)]
70 #[deprecated(since="0.6.0",
71 note
="import from rand_isaac crate instead, or use newer Hc128Rng")]
72 pub struct Isaac64Rng(rand_isaac
::Isaac64Rng
);
74 impl RngCore
for Isaac64Rng
{
76 fn next_u32(&mut self) -> u32 {
81 fn next_u64(&mut self) -> u64 {
86 fn fill_bytes(&mut self, dest
: &mut [u8]) {
87 self.0.fill_bytes
(dest
);
91 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
92 self.0.try_fill_bytes(dest
)
96 impl SeedableRng
for Isaac64Rng
{
97 type Seed
= <rand_isaac
::Isaac64Rng
as SeedableRng
>::Seed
;
99 fn from_seed(seed
: Self::Seed
) -> Self {
100 Isaac64Rng(rand_isaac
::Isaac64Rng
::from_seed(seed
))
103 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
104 rand_isaac
::Isaac64Rng
::from_rng(rng
).map(Isaac64Rng
)
109 pub fn new_from_u64(seed
: u64) -> Self {
110 Isaac64Rng(rand_isaac
::Isaac64Rng
::new_from_u64(seed
))
115 #[derive(Clone, Debug)]
116 #[deprecated(since="0.6.0", note="import from rand_chacha crate instead")]
117 pub struct ChaChaRng(rand_chacha
::ChaChaRng
);
119 impl RngCore
for ChaChaRng
{
121 fn next_u32(&mut self) -> u32 {
126 fn next_u64(&mut self) -> u64 {
131 fn fill_bytes(&mut self, dest
: &mut [u8]) {
132 self.0.fill_bytes
(dest
);
136 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
137 self.0.try_fill_bytes(dest
)
141 impl SeedableRng
for ChaChaRng
{
142 type Seed
= <rand_chacha
::ChaChaRng
as SeedableRng
>::Seed
;
144 fn from_seed(seed
: Self::Seed
) -> Self {
145 ChaChaRng(rand_chacha
::ChaChaRng
::from_seed(seed
))
148 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
149 rand_chacha
::ChaChaRng
::from_rng(rng
).map(ChaChaRng
)
154 #[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
155 pub fn get_word_pos(&self) -> u128
{
156 self.0.get_word_pos()
159 #[cfg(all(rustc_1_26, not(target_os = "emscripten")))]
160 pub fn set_word_pos(&mut self, word_offset
: u128
) {
161 self.0.set_word_pos(word_offset
)
164 pub fn set_stream(&mut self, stream
: u64) {
165 self.0.set_stream(stream
)
169 impl CryptoRng
for ChaChaRng {}
172 #[derive(Clone, Debug)]
173 #[deprecated(since="0.6.0", note="import from rand_hc crate instead")]
174 pub struct Hc128Rng(rand_hc
::Hc128Rng
);
176 impl RngCore
for Hc128Rng
{
178 fn next_u32(&mut self) -> u32 {
183 fn next_u64(&mut self) -> u64 {
188 fn fill_bytes(&mut self, dest
: &mut [u8]) {
189 self.0.fill_bytes
(dest
);
193 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
194 self.0.try_fill_bytes(dest
)
198 impl SeedableRng
for Hc128Rng
{
199 type Seed
= <rand_hc
::Hc128Rng
as SeedableRng
>::Seed
;
201 fn from_seed(seed
: Self::Seed
) -> Self {
202 Hc128Rng(rand_hc
::Hc128Rng
::from_seed(seed
))
205 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
206 rand_hc
::Hc128Rng
::from_rng(rng
).map(Hc128Rng
)
210 impl CryptoRng
for Hc128Rng {}
213 #[derive(Clone, Debug)]
214 #[deprecated(since="0.6.0", note="import from rand_xorshift crate instead")]
215 pub struct XorShiftRng(::rand_xorshift
::XorShiftRng
);
217 impl RngCore
for XorShiftRng
{
219 fn next_u32(&mut self) -> u32 {
224 fn next_u64(&mut self) -> u64 {
229 fn fill_bytes(&mut self, dest
: &mut [u8]) {
230 self.0.fill_bytes
(dest
);
234 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
235 self.0.try_fill_bytes(dest
)
239 impl SeedableRng
for XorShiftRng
{
240 type Seed
= <::rand_xorshift
::XorShiftRng
as SeedableRng
>::Seed
;
242 fn from_seed(seed
: Self::Seed
) -> Self {
243 XorShiftRng(::rand_xorshift
::XorShiftRng
::from_seed(seed
))
246 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
247 ::rand_xorshift
::XorShiftRng
::from_rng(rng
).map(XorShiftRng
)
252 #[derive(Clone, Debug)]
253 #[deprecated(since="0.6.0",
254 note
="import with rand::prelude::* or rand::rngs::StdRng instead")]
255 pub struct StdRng(rngs
::StdRng
);
257 impl RngCore
for StdRng
{
259 fn next_u32(&mut self) -> u32 {
264 fn next_u64(&mut self) -> u64 {
269 fn fill_bytes(&mut self, dest
: &mut [u8]) {
270 self.0.fill_bytes
(dest
);
274 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
275 self.0.try_fill_bytes(dest
)
279 impl SeedableRng
for StdRng
{
280 type Seed
= <rngs
::StdRng
as SeedableRng
>::Seed
;
282 fn from_seed(seed
: Self::Seed
) -> Self {
283 StdRng(rngs
::StdRng
::from_seed(seed
))
286 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
287 rngs
::StdRng
::from_rng(rng
).map(StdRng
)
291 impl CryptoRng
for StdRng {}
294 #[cfg(feature="rand_os")]
295 #[derive(Clone, Debug)]
296 #[deprecated(since="0.6.0", note="import with rand::rngs::OsRng instead")]
297 pub struct OsRng(rngs
::OsRng
);
299 #[cfg(feature="rand_os")]
300 impl RngCore
for OsRng
{
302 fn next_u32(&mut self) -> u32 {
307 fn next_u64(&mut self) -> u64 {
312 fn fill_bytes(&mut self, dest
: &mut [u8]) {
313 self.0.fill_bytes
(dest
);
317 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
318 self.0.try_fill_bytes(dest
)
322 #[cfg(feature="rand_os")]
324 pub fn new() -> Result
<Self, Error
> {
325 rngs
::OsRng
::new().map(OsRng
)
329 #[cfg(feature="rand_os")]
330 impl CryptoRng
for OsRng {}
333 #[cfg(feature="std")]
335 #[deprecated(since="0.6.0", note="import with rand::rngs::EntropyRng instead")]
336 pub struct EntropyRng(rngs
::EntropyRng
);
338 #[cfg(feature="std")]
339 impl RngCore
for EntropyRng
{
341 fn next_u32(&mut self) -> u32 {
346 fn next_u64(&mut self) -> u64 {
351 fn fill_bytes(&mut self, dest
: &mut [u8]) {
352 self.0.fill_bytes
(dest
);
356 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
357 self.0.try_fill_bytes(dest
)
361 #[cfg(feature="std")]
363 pub fn new() -> Self {
364 EntropyRng(rngs
::EntropyRng
::new())
368 #[cfg(feature="std")]
369 impl Default
for EntropyRng
{
370 fn default() -> Self {
375 #[cfg(feature="std")]
376 impl CryptoRng
for EntropyRng {}
379 #[derive(Clone, Debug)]
380 #[deprecated(since="0.6.0", note="import with rand::rngs::JitterRng instead")]
381 pub struct JitterRng(rngs
::JitterRng
);
383 impl RngCore
for JitterRng
{
385 fn next_u32(&mut self) -> u32 {
390 fn next_u64(&mut self) -> u64 {
395 fn fill_bytes(&mut self, dest
: &mut [u8]) {
396 self.0.fill_bytes
(dest
);
400 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
401 self.0.try_fill_bytes(dest
)
406 #[cfg(all(feature="std", not(target_arch = "wasm32")))]
407 pub fn new() -> Result
<JitterRng
, rngs
::TimerError
> {
408 rngs
::JitterRng
::new().map(JitterRng
)
411 pub fn new_with_timer(timer
: fn() -> u64) -> JitterRng
{
412 JitterRng(rngs
::JitterRng
::new_with_timer(timer
))
415 pub fn set_rounds(&mut self, rounds
: u8) {
416 self.0.set_rounds(rounds
)
419 pub fn test_timer(&mut self) -> Result
<u8, rngs
::TimerError
> {
423 #[cfg(feature="std")]
424 pub fn timer_stats(&mut self, var_rounds
: bool
) -> i64 {
425 self.0.timer_stats(var_rounds
)
429 impl CryptoRng
for JitterRng {}
432 #[cfg(feature="std")]
433 #[derive(Clone, Debug)]
434 #[deprecated(since="0.6.0",
435 note
="import with rand::prelude::* or rand::rngs::ThreadRng instead")]
436 pub struct ThreadRng(rngs
::ThreadRng
);
438 #[cfg(feature="std")]
439 impl RngCore
for ThreadRng
{
441 fn next_u32(&mut self) -> u32 {
446 fn next_u64(&mut self) -> u64 {
451 fn fill_bytes(&mut self, dest
: &mut [u8]) {
452 self.0.fill_bytes
(dest
);
456 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
457 self.0.try_fill_bytes(dest
)
461 #[cfg(feature="std")]
462 impl CryptoRng
for ThreadRng {}
465 #[cfg(feature="std")]
467 #[deprecated(since="0.6.0", note="import with rand::rngs::adapter::ReadRng instead")]
468 pub struct ReadRng
<R
>(rngs
::adapter
::ReadRng
<R
>);
470 #[cfg(feature="std")]
471 impl<R
: Read
> RngCore
for ReadRng
<R
> {
473 fn next_u32(&mut self) -> u32 {
478 fn next_u64(&mut self) -> u64 {
483 fn fill_bytes(&mut self, dest
: &mut [u8]) {
484 self.0.fill_bytes
(dest
);
488 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
489 self.0.try_fill_bytes(dest
)
493 #[cfg(feature="std")]
494 impl<R
: Read
> ReadRng
<R
> {
495 pub fn new(r
: R
) -> ReadRng
<R
> {
496 ReadRng(rngs
::adapter
::ReadRng
::new(r
))
501 #[derive(Clone, Debug)]
502 pub struct ReseedingRng
<R
, Rsdr
>(rngs
::adapter
::ReseedingRng
<R
, Rsdr
>)
503 where R
: BlockRngCore
+ SeedableRng
,
506 impl<R
, Rsdr
: RngCore
> RngCore
for ReseedingRng
<R
, Rsdr
>
507 where R
: BlockRngCore
<Item
= u32> + SeedableRng
,
508 <R
as BlockRngCore
>::Results
: AsRef
<[u32]> + AsMut
<[u32]>
511 fn next_u32(&mut self) -> u32 {
516 fn next_u64(&mut self) -> u64 {
520 fn fill_bytes(&mut self, dest
: &mut [u8]) {
521 self.0.fill_bytes
(dest
)
524 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
525 self.0.try_fill_bytes(dest
)
529 impl<R
, Rsdr
> ReseedingRng
<R
, Rsdr
>
530 where R
: BlockRngCore
+ SeedableRng
,
533 pub fn new(rng
: R
, threshold
: u64, reseeder
: Rsdr
) -> Self {
534 ReseedingRng(rngs
::adapter
::ReseedingRng
::new(rng
, threshold
, reseeder
))
537 pub fn reseed(&mut self) -> Result
<(), Error
> {
542 impl<R
, Rsdr
> CryptoRng
for ReseedingRng
<R
, Rsdr
>
543 where R
: BlockRngCore
+ SeedableRng
+ CryptoRng
,
544 Rsdr
: RngCore
+ CryptoRng {}