1 // Copyright 2018 Developers of the Rand project. 2 // 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. 8 9 //! Deprecated re-exports (we can't add deprecation warnings otherwise) 10 11 #![allow(deprecated)] 12 13 use rngs; 14 use {RngCore, CryptoRng, SeedableRng, Error}; 15 use rand_core::block::BlockRngCore; 16 use rand_isaac; 17 use rand_chacha; 18 use rand_hc; 19 20 #[cfg(feature="std")] 21 use std::io::Read; 22 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); 27 28 impl RngCore for IsaacRng { 29 #[inline(always)] next_u32(&mut self) -> u3230 fn next_u32(&mut self) -> u32 { 31 self.0.next_u32() 32 } 33 34 #[inline(always)] next_u64(&mut self) -> u6435 fn next_u64(&mut self) -> u64 { 36 self.0.next_u64() 37 } 38 39 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])40 fn fill_bytes(&mut self, dest: &mut [u8]) { 41 self.0.fill_bytes(dest); 42 } 43 44 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>45 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 46 self.0.try_fill_bytes(dest) 47 } 48 } 49 50 impl SeedableRng for IsaacRng { 51 type Seed = <rand_isaac::IsaacRng as SeedableRng>::Seed; 52 from_seed(seed: Self::Seed) -> Self53 fn from_seed(seed: Self::Seed) -> Self { 54 IsaacRng(rand_isaac::IsaacRng::from_seed(seed)) 55 } 56 from_rng<R: RngCore>(rng: R) -> Result<Self, Error>57 fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { 58 rand_isaac::IsaacRng::from_rng(rng).map(IsaacRng) 59 } 60 } 61 62 impl IsaacRng { new_from_u64(seed: u64) -> Self63 pub fn new_from_u64(seed: u64) -> Self { 64 IsaacRng(rand_isaac::IsaacRng::new_from_u64(seed)) 65 } 66 } 67 68 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); 73 74 impl RngCore for Isaac64Rng { 75 #[inline(always)] next_u32(&mut self) -> u3276 fn next_u32(&mut self) -> u32 { 77 self.0.next_u32() 78 } 79 80 #[inline(always)] next_u64(&mut self) -> u6481 fn next_u64(&mut self) -> u64 { 82 self.0.next_u64() 83 } 84 85 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])86 fn fill_bytes(&mut self, dest: &mut [u8]) { 87 self.0.fill_bytes(dest); 88 } 89 90 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>91 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 92 self.0.try_fill_bytes(dest) 93 } 94 } 95 96 impl SeedableRng for Isaac64Rng { 97 type Seed = <rand_isaac::Isaac64Rng as SeedableRng>::Seed; 98 from_seed(seed: Self::Seed) -> Self99 fn from_seed(seed: Self::Seed) -> Self { 100 Isaac64Rng(rand_isaac::Isaac64Rng::from_seed(seed)) 101 } 102 from_rng<R: RngCore>(rng: R) -> Result<Self, Error>103 fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { 104 rand_isaac::Isaac64Rng::from_rng(rng).map(Isaac64Rng) 105 } 106 } 107 108 impl Isaac64Rng { new_from_u64(seed: u64) -> Self109 pub fn new_from_u64(seed: u64) -> Self { 110 Isaac64Rng(rand_isaac::Isaac64Rng::new_from_u64(seed)) 111 } 112 } 113 114 115 #[derive(Clone, Debug)] 116 #[deprecated(since="0.6.0", note="import from rand_chacha crate instead")] 117 pub struct ChaChaRng(rand_chacha::ChaChaRng); 118 119 impl RngCore for ChaChaRng { 120 #[inline(always)] next_u32(&mut self) -> u32121 fn next_u32(&mut self) -> u32 { 122 self.0.next_u32() 123 } 124 125 #[inline(always)] next_u64(&mut self) -> u64126 fn next_u64(&mut self) -> u64 { 127 self.0.next_u64() 128 } 129 130 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])131 fn fill_bytes(&mut self, dest: &mut [u8]) { 132 self.0.fill_bytes(dest); 133 } 134 135 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>136 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 137 self.0.try_fill_bytes(dest) 138 } 139 } 140 141 impl SeedableRng for ChaChaRng { 142 type Seed = <rand_chacha::ChaChaRng as SeedableRng>::Seed; 143 from_seed(seed: Self::Seed) -> Self144 fn from_seed(seed: Self::Seed) -> Self { 145 ChaChaRng(rand_chacha::ChaChaRng::from_seed(seed)) 146 } 147 from_rng<R: RngCore>(rng: R) -> Result<Self, Error>148 fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { 149 rand_chacha::ChaChaRng::from_rng(rng).map(ChaChaRng) 150 } 151 } 152 153 impl ChaChaRng { 154 #[cfg(all(rustc_1_26, not(target_os = "emscripten")))] get_word_pos(&self) -> u128155 pub fn get_word_pos(&self) -> u128 { 156 self.0.get_word_pos() 157 } 158 159 #[cfg(all(rustc_1_26, not(target_os = "emscripten")))] set_word_pos(&mut self, word_offset: u128)160 pub fn set_word_pos(&mut self, word_offset: u128) { 161 self.0.set_word_pos(word_offset) 162 } 163 set_stream(&mut self, stream: u64)164 pub fn set_stream(&mut self, stream: u64) { 165 self.0.set_stream(stream) 166 } 167 } 168 169 impl CryptoRng for ChaChaRng {} 170 171 172 #[derive(Clone, Debug)] 173 #[deprecated(since="0.6.0", note="import from rand_hc crate instead")] 174 pub struct Hc128Rng(rand_hc::Hc128Rng); 175 176 impl RngCore for Hc128Rng { 177 #[inline(always)] next_u32(&mut self) -> u32178 fn next_u32(&mut self) -> u32 { 179 self.0.next_u32() 180 } 181 182 #[inline(always)] next_u64(&mut self) -> u64183 fn next_u64(&mut self) -> u64 { 184 self.0.next_u64() 185 } 186 187 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])188 fn fill_bytes(&mut self, dest: &mut [u8]) { 189 self.0.fill_bytes(dest); 190 } 191 192 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>193 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 194 self.0.try_fill_bytes(dest) 195 } 196 } 197 198 impl SeedableRng for Hc128Rng { 199 type Seed = <rand_hc::Hc128Rng as SeedableRng>::Seed; 200 from_seed(seed: Self::Seed) -> Self201 fn from_seed(seed: Self::Seed) -> Self { 202 Hc128Rng(rand_hc::Hc128Rng::from_seed(seed)) 203 } 204 from_rng<R: RngCore>(rng: R) -> Result<Self, Error>205 fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { 206 rand_hc::Hc128Rng::from_rng(rng).map(Hc128Rng) 207 } 208 } 209 210 impl CryptoRng for Hc128Rng {} 211 212 213 #[derive(Clone, Debug)] 214 #[deprecated(since="0.6.0", note="import from rand_xorshift crate instead")] 215 pub struct XorShiftRng(::rand_xorshift::XorShiftRng); 216 217 impl RngCore for XorShiftRng { 218 #[inline(always)] next_u32(&mut self) -> u32219 fn next_u32(&mut self) -> u32 { 220 self.0.next_u32() 221 } 222 223 #[inline(always)] next_u64(&mut self) -> u64224 fn next_u64(&mut self) -> u64 { 225 self.0.next_u64() 226 } 227 228 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])229 fn fill_bytes(&mut self, dest: &mut [u8]) { 230 self.0.fill_bytes(dest); 231 } 232 233 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>234 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 235 self.0.try_fill_bytes(dest) 236 } 237 } 238 239 impl SeedableRng for XorShiftRng { 240 type Seed = <::rand_xorshift::XorShiftRng as SeedableRng>::Seed; 241 from_seed(seed: Self::Seed) -> Self242 fn from_seed(seed: Self::Seed) -> Self { 243 XorShiftRng(::rand_xorshift::XorShiftRng::from_seed(seed)) 244 } 245 from_rng<R: RngCore>(rng: R) -> Result<Self, Error>246 fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { 247 ::rand_xorshift::XorShiftRng::from_rng(rng).map(XorShiftRng) 248 } 249 } 250 251 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); 256 257 impl RngCore for StdRng { 258 #[inline(always)] next_u32(&mut self) -> u32259 fn next_u32(&mut self) -> u32 { 260 self.0.next_u32() 261 } 262 263 #[inline(always)] next_u64(&mut self) -> u64264 fn next_u64(&mut self) -> u64 { 265 self.0.next_u64() 266 } 267 268 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])269 fn fill_bytes(&mut self, dest: &mut [u8]) { 270 self.0.fill_bytes(dest); 271 } 272 273 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>274 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 275 self.0.try_fill_bytes(dest) 276 } 277 } 278 279 impl SeedableRng for StdRng { 280 type Seed = <rngs::StdRng as SeedableRng>::Seed; 281 from_seed(seed: Self::Seed) -> Self282 fn from_seed(seed: Self::Seed) -> Self { 283 StdRng(rngs::StdRng::from_seed(seed)) 284 } 285 from_rng<R: RngCore>(rng: R) -> Result<Self, Error>286 fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { 287 rngs::StdRng::from_rng(rng).map(StdRng) 288 } 289 } 290 291 impl CryptoRng for StdRng {} 292 293 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); 298 299 #[cfg(feature="rand_os")] 300 impl RngCore for OsRng { 301 #[inline(always)] next_u32(&mut self) -> u32302 fn next_u32(&mut self) -> u32 { 303 self.0.next_u32() 304 } 305 306 #[inline(always)] next_u64(&mut self) -> u64307 fn next_u64(&mut self) -> u64 { 308 self.0.next_u64() 309 } 310 311 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])312 fn fill_bytes(&mut self, dest: &mut [u8]) { 313 self.0.fill_bytes(dest); 314 } 315 316 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>317 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 318 self.0.try_fill_bytes(dest) 319 } 320 } 321 322 #[cfg(feature="rand_os")] 323 impl OsRng { new() -> Result<Self, Error>324 pub fn new() -> Result<Self, Error> { 325 rngs::OsRng::new().map(OsRng) 326 } 327 } 328 329 #[cfg(feature="rand_os")] 330 impl CryptoRng for OsRng {} 331 332 333 #[cfg(feature="std")] 334 #[derive(Debug)] 335 #[deprecated(since="0.6.0", note="import with rand::rngs::EntropyRng instead")] 336 pub struct EntropyRng(rngs::EntropyRng); 337 338 #[cfg(feature="std")] 339 impl RngCore for EntropyRng { 340 #[inline(always)] next_u32(&mut self) -> u32341 fn next_u32(&mut self) -> u32 { 342 self.0.next_u32() 343 } 344 345 #[inline(always)] next_u64(&mut self) -> u64346 fn next_u64(&mut self) -> u64 { 347 self.0.next_u64() 348 } 349 350 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])351 fn fill_bytes(&mut self, dest: &mut [u8]) { 352 self.0.fill_bytes(dest); 353 } 354 355 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>356 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 357 self.0.try_fill_bytes(dest) 358 } 359 } 360 361 #[cfg(feature="std")] 362 impl EntropyRng { new() -> Self363 pub fn new() -> Self { 364 EntropyRng(rngs::EntropyRng::new()) 365 } 366 } 367 368 #[cfg(feature="std")] 369 impl Default for EntropyRng { default() -> Self370 fn default() -> Self { 371 EntropyRng::new() 372 } 373 } 374 375 #[cfg(feature="std")] 376 impl CryptoRng for EntropyRng {} 377 378 379 #[derive(Clone, Debug)] 380 #[deprecated(since="0.6.0", note="import with rand::rngs::JitterRng instead")] 381 pub struct JitterRng(rngs::JitterRng); 382 383 impl RngCore for JitterRng { 384 #[inline(always)] next_u32(&mut self) -> u32385 fn next_u32(&mut self) -> u32 { 386 self.0.next_u32() 387 } 388 389 #[inline(always)] next_u64(&mut self) -> u64390 fn next_u64(&mut self) -> u64 { 391 self.0.next_u64() 392 } 393 394 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])395 fn fill_bytes(&mut self, dest: &mut [u8]) { 396 self.0.fill_bytes(dest); 397 } 398 399 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>400 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 401 self.0.try_fill_bytes(dest) 402 } 403 } 404 405 impl JitterRng { 406 #[cfg(all(feature="std", not(target_arch = "wasm32")))] new() -> Result<JitterRng, rngs::TimerError>407 pub fn new() -> Result<JitterRng, rngs::TimerError> { 408 rngs::JitterRng::new().map(JitterRng) 409 } 410 new_with_timer(timer: fn() -> u64) -> JitterRng411 pub fn new_with_timer(timer: fn() -> u64) -> JitterRng { 412 JitterRng(rngs::JitterRng::new_with_timer(timer)) 413 } 414 set_rounds(&mut self, rounds: u8)415 pub fn set_rounds(&mut self, rounds: u8) { 416 self.0.set_rounds(rounds) 417 } 418 test_timer(&mut self) -> Result<u8, rngs::TimerError>419 pub fn test_timer(&mut self) -> Result<u8, rngs::TimerError> { 420 self.0.test_timer() 421 } 422 423 #[cfg(feature="std")] timer_stats(&mut self, var_rounds: bool) -> i64424 pub fn timer_stats(&mut self, var_rounds: bool) -> i64 { 425 self.0.timer_stats(var_rounds) 426 } 427 } 428 429 impl CryptoRng for JitterRng {} 430 431 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); 437 438 #[cfg(feature="std")] 439 impl RngCore for ThreadRng { 440 #[inline(always)] next_u32(&mut self) -> u32441 fn next_u32(&mut self) -> u32 { 442 self.0.next_u32() 443 } 444 445 #[inline(always)] next_u64(&mut self) -> u64446 fn next_u64(&mut self) -> u64 { 447 self.0.next_u64() 448 } 449 450 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])451 fn fill_bytes(&mut self, dest: &mut [u8]) { 452 self.0.fill_bytes(dest); 453 } 454 455 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>456 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 457 self.0.try_fill_bytes(dest) 458 } 459 } 460 461 #[cfg(feature="std")] 462 impl CryptoRng for ThreadRng {} 463 464 465 #[cfg(feature="std")] 466 #[derive(Debug)] 467 #[deprecated(since="0.6.0", note="import with rand::rngs::adapter::ReadRng instead")] 468 pub struct ReadRng<R>(rngs::adapter::ReadRng<R>); 469 470 #[cfg(feature="std")] 471 impl<R: Read> RngCore for ReadRng<R> { 472 #[inline(always)] next_u32(&mut self) -> u32473 fn next_u32(&mut self) -> u32 { 474 self.0.next_u32() 475 } 476 477 #[inline(always)] next_u64(&mut self) -> u64478 fn next_u64(&mut self) -> u64 { 479 self.0.next_u64() 480 } 481 482 #[inline(always)] fill_bytes(&mut self, dest: &mut [u8])483 fn fill_bytes(&mut self, dest: &mut [u8]) { 484 self.0.fill_bytes(dest); 485 } 486 487 #[inline(always)] try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>488 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 489 self.0.try_fill_bytes(dest) 490 } 491 } 492 493 #[cfg(feature="std")] 494 impl<R: Read> ReadRng<R> { new(r: R) -> ReadRng<R>495 pub fn new(r: R) -> ReadRng<R> { 496 ReadRng(rngs::adapter::ReadRng::new(r)) 497 } 498 } 499 500 501 #[derive(Clone, Debug)] 502 pub struct ReseedingRng<R, Rsdr>(rngs::adapter::ReseedingRng<R, Rsdr>) 503 where R: BlockRngCore + SeedableRng, 504 Rsdr: RngCore; 505 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]> 509 { 510 #[inline(always)] next_u32(&mut self) -> u32511 fn next_u32(&mut self) -> u32 { 512 self.0.next_u32() 513 } 514 515 #[inline(always)] next_u64(&mut self) -> u64516 fn next_u64(&mut self) -> u64 { 517 self.0.next_u64() 518 } 519 fill_bytes(&mut self, dest: &mut [u8])520 fn fill_bytes(&mut self, dest: &mut [u8]) { 521 self.0.fill_bytes(dest) 522 } 523 try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>524 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 525 self.0.try_fill_bytes(dest) 526 } 527 } 528 529 impl<R, Rsdr> ReseedingRng<R, Rsdr> 530 where R: BlockRngCore + SeedableRng, 531 Rsdr: RngCore 532 { new(rng: R, threshold: u64, reseeder: Rsdr) -> Self533 pub fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self { 534 ReseedingRng(rngs::adapter::ReseedingRng::new(rng, threshold, reseeder)) 535 } 536 reseed(&mut self) -> Result<(), Error>537 pub fn reseed(&mut self) -> Result<(), Error> { 538 self.0.reseed() 539 } 540 } 541 542 impl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr> 543 where R: BlockRngCore + SeedableRng + CryptoRng, 544 Rsdr: RngCore + CryptoRng {} 545