1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #2893 from g-k/2888-rand-0.8

update rand crate to 0.8
This commit is contained in:
Sylvestre Ledru 2022-01-25 23:23:43 +01:00 committed by GitHub
commit c7fc0a703b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 223 additions and 155 deletions

View file

@ -35,6 +35,7 @@ WASM
XFS
aarch
flac
impls
lzma
# * names

View file

@ -28,6 +28,7 @@ devs
discoverability
duplicative
dsync
endianness
enqueue
errored
executable

153
Cargo.lock generated
View file

@ -267,15 +267,6 @@ dependencies = [
"clap 3.0.10",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags",
]
[[package]]
name = "compare"
version = "0.1.0"
@ -312,7 +303,7 @@ dependencies = [
"libc",
"nix 0.23.1",
"pretty_assertions",
"rand 0.7.3",
"rand",
"regex",
"rlimit",
"selinux",
@ -672,7 +663,7 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68df3f2b690c1b86e65ef7830956aededf3cb0a16f898f79b9a6f421a7b6211b"
dependencies = [
"rand 0.8.4",
"rand",
]
[[package]]
@ -701,9 +692,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "env_logger"
version = "0.7.1"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
dependencies = [
"log",
"regex",
@ -792,12 +783,6 @@ dependencies = [
"libc",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "gcd"
version = "2.1.0"
@ -826,17 +811,6 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.4"
@ -845,7 +819,7 @@ checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
"wasi",
]
[[package]]
@ -1470,14 +1444,13 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
[[package]]
name = "quickcheck"
version = "0.9.2"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f"
checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6"
dependencies = [
"env_logger 0.7.1",
"env_logger 0.8.4",
"log",
"rand 0.7.3",
"rand_core 0.5.1",
"rand",
]
[[package]]
@ -1495,33 +1468,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"winapi 0.3.9",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
"rand_pcg",
]
[[package]]
name = "rand"
version = "0.8.4"
@ -1529,19 +1475,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
"rand_hc 0.3.1",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
@ -1551,31 +1487,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
"rand_core",
]
[[package]]
@ -1584,16 +1496,7 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom 0.2.4",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
"getrandom",
]
[[package]]
@ -1602,16 +1505,7 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core 0.6.3",
]
[[package]]
name = "rand_pcg"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
dependencies = [
"rand_core 0.5.1",
"rand_core",
]
[[package]]
@ -2429,7 +2323,7 @@ dependencies = [
"num-traits",
"paste 0.1.18",
"quickcheck",
"rand 0.7.3",
"rand",
"smallvec",
"uucore",
"uucore_procs",
@ -2652,7 +2546,7 @@ name = "uu_mktemp"
version = "0.0.12"
dependencies = [
"clap 3.0.10",
"rand 0.5.6",
"rand",
"tempfile",
"uucore",
"uucore_procs",
@ -2917,7 +2811,7 @@ version = "0.0.12"
dependencies = [
"clap 3.0.10",
"libc",
"rand 0.7.3",
"rand",
"uucore",
"uucore_procs",
]
@ -2927,7 +2821,8 @@ name = "uu_shuf"
version = "0.0.12"
dependencies = [
"clap 3.0.10",
"rand 0.5.6",
"rand",
"rand_core",
"uucore",
"uucore_procs",
]
@ -2953,7 +2848,7 @@ dependencies = [
"itertools 0.10.3",
"memchr 2.4.1",
"ouroboros",
"rand 0.7.3",
"rand",
"rayon",
"tempfile",
"unicode-width",
@ -3304,12 +3199,6 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"

View file

@ -369,7 +369,7 @@ filetime = "0.2"
glob = "0.3.0"
libc = "0.2"
pretty_assertions = "1"
rand = "0.7"
rand = "0.8"
regex = "1.0"
sha1 = { version="0.6", features=["std"] }
tempfile = "3.2.0"

View file

@ -18,14 +18,14 @@ num-traits = "0.2.13" # used in src/numerics.rs, which is included by build.rs
clap = { version = "3.0", features = ["wrap_help", "cargo"] }
coz = { version = "0.1.3", optional = true }
num-traits = "0.2.13" # Needs at least version 0.2.13 for "OverflowingAdd"
rand = { version = "0.7", features = ["small_rng"] }
rand = { version = "0.8", features = ["small_rng"] }
smallvec = "1.7" # TODO(nicoo): Use `union` feature, requires Rust 1.49 or later.
uucore = { version = ">=0.0.8", package = "uucore", path = "../../uucore" }
uucore_procs = { version=">=0.0.8", package = "uucore_procs", path = "../../uucore_procs" }
[dev-dependencies]
paste = "0.1.18"
quickcheck = "0.9.2"
quickcheck = "1.0.3"
[[bin]]

View file

@ -258,7 +258,7 @@ impl Distribution<Factors> for Standard {
// See Generating Random Factored Numbers, Easily, J. Cryptology (2003)
'attempt: loop {
while n > 1 {
n = rng.gen_range(1, n);
n = rng.gen_range(1..n);
if miller_rabin::is_prime(n) {
if let Some(h) = g.checked_mul(n) {
f.push(n);
@ -277,8 +277,8 @@ impl Distribution<Factors> for Standard {
#[cfg(test)]
impl quickcheck::Arbitrary for Factors {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
g.gen()
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
factor(u64::arbitrary(g))
}
}

View file

@ -200,7 +200,11 @@ mod tests {
quickcheck! {
fn composites(i: u64, j: u64) -> bool {
i < 2 || j < 2 || !is_prime(i*j)
// TODO: #1559 factor n > 2^64 - 1
match i.checked_mul(j) {
Some(n) => i < 2 || j < 2 || !is_prime(n),
_ => true,
}
}
}
}

View file

@ -6,6 +6,8 @@
// * For the full copyright and license information, please view the LICENSE file
// * that was distributed with this source code.
// spell-checker:ignore (vars) kgcdab gcdac gcdbc
use std::cmp::min;
use std::mem::swap;
@ -93,16 +95,35 @@ mod tests {
}
fn scalar_multiplication(a: u64, b: u64, k: u64) -> bool {
gcd(k * a, k * b) == k * gcd(a, b)
// TODO: #1559 factor n > 2^64 - 1
match (k.checked_mul(a), k.checked_mul(b), k.checked_mul(gcd(a, b))) {
(Some(ka), Some(kb), Some(kgcdab)) => gcd(ka, kb) == kgcdab,
_ => true
}
}
fn multiplicative(a: u64, b: u64, c: u64) -> bool {
// gcd(ab, c) = gcd(a, c) gcd(b, c) when a and b coprime
gcd(a, b) != 1 || gcd(a * b, c) == gcd(a, c) * gcd(b, c)
// TODO: #1559 factor n > 2^64 - 1
match (a.checked_mul(b), gcd(a, c).checked_mul(gcd(b, c))) {
(Some(ab), Some(gcdac_gcdbc)) => {
// gcd(ab, c) = gcd(a, c) gcd(b, c) when a and b coprime
gcd(a, b) != 1 || gcd(ab, c) == gcdac_gcdbc
},
_ => true,
}
}
fn linearity(a: u64, b: u64, k: u64) -> bool {
gcd(a + k * b, b) == gcd(a, b)
// TODO: #1559 factor n > 2^64 - 1
match k.checked_mul(b) {
Some(kb) => {
match a.checked_add(kb) {
Some(a_plus_kb) => gcd(a_plus_kb, b) == gcd(a, b),
_ => true,
}
}
_ => true,
}
}
}
}

View file

@ -63,13 +63,17 @@ mod tests {
quickcheck! {
fn random_values_u32(n: u32) -> bool {
let n = 2 * n + 1;
modular_inverse(n).wrapping_mul(n) == 1
match 2_u32.checked_mul(n) {
Some(n) => modular_inverse(n + 1).wrapping_mul(n + 1) == 1,
_ => true,
}
}
fn random_values_u64(n: u64) -> bool {
let n = 2 * n + 1;
modular_inverse(n).wrapping_mul(n) == 1
match 2_u64.checked_mul(n) {
Some(n) => modular_inverse(n + 1).wrapping_mul(n + 1) == 1,
_ => true,
}
}
}
}

View file

@ -16,7 +16,7 @@ path = "src/mktemp.rs"
[dependencies]
clap = { version = "3.0", features = ["wrap_help", "cargo"] }
rand = "0.5"
rand = "0.8"
tempfile = "3.1"
uucore = { version=">=0.0.11", package="uucore", path="../../uucore" }
uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" }

View file

@ -17,7 +17,7 @@ path = "src/shred.rs"
[dependencies]
clap = { version = "3.0", features = ["wrap_help", "cargo"] }
libc = "0.2.42"
rand = "0.7"
rand = "0.8"
uucore = { version=">=0.0.11", package="uucore", path="../../uucore" }
uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" }

View file

@ -16,7 +16,8 @@ path = "src/shuf.rs"
[dependencies]
clap = { version = "3.0", features = ["wrap_help", "cargo"] }
rand = "0.5"
rand = "0.8"
rand_core = "0.6"
uucore = { version=">=0.0.11", package="uucore", path="../../uucore" }
uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" }

View file

@ -0,0 +1,144 @@
// Copyright 2018 Developers of the Rand project.
// Copyright 2013 The Rust Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! A wrapper around any Read to treat it as an RNG.
use std::fmt;
use std::io::Read;
use rand_core::{impls, Error, RngCore};
/// An RNG that reads random bytes straight from any type supporting
/// [`std::io::Read`], for example files.
///
/// This will work best with an infinite reader, but that is not required.
///
/// This can be used with `/dev/urandom` on Unix but it is recommended to use
/// [`OsRng`] instead.
///
/// # Panics
///
/// `ReadRng` uses [`std::io::Read::read_exact`], which retries on interrupts.
/// All other errors from the underlying reader, including when it does not
/// have enough data, will only be reported through [`try_fill_bytes`].
/// The other [`RngCore`] methods will panic in case of an error.
///
/// [`OsRng`]: crate::rngs::OsRng
/// [`try_fill_bytes`]: RngCore::try_fill_bytes
#[derive(Debug)]
pub struct ReadRng<R> {
reader: R,
}
impl<R: Read> ReadRng<R> {
/// Create a new `ReadRng` from a `Read`.
pub fn new(r: R) -> ReadRng<R> {
ReadRng { reader: r }
}
}
impl<R: Read> RngCore for ReadRng<R> {
fn next_u32(&mut self) -> u32 {
impls::next_u32_via_fill(self)
}
fn next_u64(&mut self) -> u64 {
impls::next_u64_via_fill(self)
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.try_fill_bytes(dest).unwrap_or_else(|err| {
panic!(
"reading random bytes from Read implementation failed; error: {}",
err
)
});
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
if dest.is_empty() {
return Ok(());
}
// Use `std::io::read_exact`, which retries on `ErrorKind::Interrupted`.
self.reader
.read_exact(dest)
.map_err(|e| Error::new(ReadError(e)))
}
}
/// `ReadRng` error type
#[derive(Debug)]
pub struct ReadError(std::io::Error);
impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ReadError: {}", self.0)
}
}
impl std::error::Error for ReadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
#[cfg(test)]
mod test {
use std::println;
use super::ReadRng;
use rand::RngCore;
#[test]
fn test_reader_rng_u64() {
// transmute from the target to avoid endianness concerns.
#[rustfmt::skip]
let v = [0u8, 0, 0, 0, 0, 0, 0, 1,
0, 4, 0, 0, 3, 0, 0, 2,
5, 0, 0, 0, 0, 0, 0, 0];
let mut rng = ReadRng::new(&v[..]);
assert_eq!(rng.next_u64(), 1 << 56);
assert_eq!(rng.next_u64(), (2 << 56) + (3 << 32) + (4 << 8));
assert_eq!(rng.next_u64(), 5);
}
#[test]
fn test_reader_rng_u32() {
let v = [0u8, 0, 0, 1, 0, 0, 2, 0, 3, 0, 0, 0];
let mut rng = ReadRng::new(&v[..]);
assert_eq!(rng.next_u32(), 1 << 24);
assert_eq!(rng.next_u32(), 2 << 16);
assert_eq!(rng.next_u32(), 3);
}
#[test]
fn test_reader_rng_fill_bytes() {
let v = [1u8, 2, 3, 4, 5, 6, 7, 8];
let mut w = [0u8; 8];
let mut rng = ReadRng::new(&v[..]);
rng.fill_bytes(&mut w);
assert!(v == w);
}
#[test]
fn test_reader_rng_insufficient_bytes() {
let v = [1u8, 2, 3, 4, 5, 6, 7, 8];
let mut w = [0u8; 9];
let mut rng = ReadRng::new(&v[..]);
let result = rng.try_fill_bytes(&mut w);
assert!(result.is_err());
println!("Error: {}", result.unwrap_err());
}
}

View file

@ -15,6 +15,8 @@ use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::InvalidEncodingHandling;
mod rand_read_adapter;
enum Mode {
Default(String),
Echo(Vec<String>),
@ -244,7 +246,7 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) -> UResult<()> {
Some(r) => {
let file = File::open(&r[..])
.map_err_context(|| format!("failed to open random source {}", r.quote()))?;
WrappedRng::RngFile(rand::rngs::adapter::ReadRng::new(file))
WrappedRng::RngFile(rand_read_adapter::ReadRng::new(file))
}
None => WrappedRng::RngDefault(rand::thread_rng()),
};
@ -302,7 +304,7 @@ fn parse_range(input_range: &str) -> Result<(usize, usize), String> {
}
enum WrappedRng {
RngFile(rand::rngs::adapter::ReadRng<File>),
RngFile(rand_read_adapter::ReadRng<File>),
RngDefault(rand::rngs::ThreadRng),
}

View file

@ -23,7 +23,7 @@ fnv = "1.0.7"
itertools = "0.10.0"
memchr = "2.4.0"
ouroboros = "0.10.1"
rand = "0.7"
rand = "0.8"
rayon = "1.5"
tempfile = "3"
unicode-width = "0.1.8"

View file

@ -13,7 +13,7 @@ uu_factor = { path = "../../../src/uu/factor" }
[dev-dependencies]
array-init = "2.0.0"
criterion = "0.3"
rand = "0.7"
rand = "0.8"
rand_chacha = "0.2.2"

View file

@ -115,7 +115,7 @@ fn test_random() {
// log distribution---higher probability for lower numbers
let factor;
loop {
let next = rng.gen_range(0_f64, log_num_primes).exp2().floor() as usize;
let next = rng.gen_range(0_f64..log_num_primes).exp2().floor() as usize;
if next < NUM_PRIMES {
factor = primes[next];
break;

View file

@ -22,6 +22,7 @@ use std::{
fn random_chars(n: usize) -> String {
thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.map(char::from)
.take(n)
.collect::<String>()
}