From 1544c728cd7725797e2e197aefd817603b0f227e Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Sat, 4 Sep 2021 18:33:39 -0400 Subject: [PATCH 1/3] fix no_std/alloc/std compatibility issues - using min_const_gen on no_std fails to compile because of a bad import - sample_efraimidis_spirakis only requires alloc but it was marked with a std requirement --- benches/distributions.rs | 8 ++++---- benches/generators.rs | 2 +- benches/misc.rs | 4 ++-- benches/seq.rs | 4 ++-- rand_distr/benches/src/distributions.rs | 2 +- rand_distr/tests/uniformity.rs | 2 +- src/distributions/distribution.rs | 4 ++-- src/distributions/other.rs | 2 +- src/distributions/uniform.rs | 9 ++++----- src/seq/index.rs | 10 +++++----- 10 files changed, 23 insertions(+), 24 deletions(-) diff --git a/benches/distributions.rs b/benches/distributions.rs index 7d8ac94c37b..76d5d258d9d 100644 --- a/benches/distributions.rs +++ b/benches/distributions.rs @@ -18,9 +18,9 @@ const RAND_BENCH_N: u64 = 1000; use rand::distributions::{Alphanumeric, Open01, OpenClosed01, Standard, Uniform}; use rand::distributions::uniform::{UniformInt, UniformSampler}; -use std::mem::size_of; -use std::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8}; -use std::time::Duration; +use core::mem::size_of; +use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8}; +use core::time::Duration; use test::{Bencher, black_box}; use rand::prelude::*; @@ -199,7 +199,7 @@ macro_rules! gen_range_int { for _ in 0..RAND_BENCH_N { accum = accum.wrapping_add(rng.gen_range($low..high)); // force recalculation of range each time - high = high.wrapping_add(1) & std::$ty::MAX; + high = high.wrapping_add(1) & core::$ty::MAX; } accum }); diff --git a/benches/generators.rs b/benches/generators.rs index f59c22224fb..65305a18e1f 100644 --- a/benches/generators.rs +++ b/benches/generators.rs @@ -14,7 +14,7 @@ extern crate test; const RAND_BENCH_N: u64 = 1000; const BYTES_LEN: usize = 1024; -use std::mem::size_of; +use core::mem::size_of; use test::{black_box, Bencher}; use rand::prelude::*; diff --git a/benches/misc.rs b/benches/misc.rs index 11d12eb24ad..f0b761f99ed 100644 --- a/benches/misc.rs +++ b/benches/misc.rs @@ -98,7 +98,7 @@ fn misc_bernoulli_var(b: &mut Bencher) { #[bench] fn gen_1kb_u16_iter_repeat(b: &mut Bencher) { - use std::iter; + use core::iter; let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap(); b.iter(|| { let v: Vec = iter::repeat(()).map(|()| rng.gen()).take(512).collect(); @@ -141,7 +141,7 @@ fn gen_1kb_u16_fill(b: &mut Bencher) { #[bench] fn gen_1kb_u64_iter_repeat(b: &mut Bencher) { - use std::iter; + use core::iter; let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap(); b.iter(|| { let v: Vec = iter::repeat(()).map(|()| rng.gen()).take(128).collect(); diff --git a/benches/seq.rs b/benches/seq.rs index a9bd88ff882..5b3a846f60b 100644 --- a/benches/seq.rs +++ b/benches/seq.rs @@ -15,7 +15,7 @@ use test::Bencher; use rand::prelude::*; use rand::seq::*; -use std::mem::size_of; +use core::mem::size_of; // We force use of 32-bit RNG since seq code is optimised for use with 32-bit // generators on all platforms. @@ -116,7 +116,7 @@ impl Iterator for WindowHintedIterator< } fn size_hint(&self) -> (usize, Option) { - (std::cmp::min(self.iter.len(), self.window_size), None) + (core::cmp::min(self.iter.len(), self.window_size), None) } } diff --git a/rand_distr/benches/src/distributions.rs b/rand_distr/benches/src/distributions.rs index 61c2aa1883c..d638debd526 100644 --- a/rand_distr/benches/src/distributions.rs +++ b/rand_distr/benches/src/distributions.rs @@ -17,7 +17,7 @@ use criterion::{criterion_group, criterion_main, Criterion, Throughput}; use criterion_cycles_per_byte::CyclesPerByte; -use std::mem::size_of; +use core::mem::size_of; use rand::prelude::*; use rand_distr::*; diff --git a/rand_distr/tests/uniformity.rs b/rand_distr/tests/uniformity.rs index 4a64babdc76..d37ef0a9d06 100644 --- a/rand_distr/tests/uniformity.rs +++ b/rand_distr/tests/uniformity.rs @@ -48,7 +48,7 @@ fn unit_sphere() { #[test] fn unit_circle() { - use std::f64::consts::PI; + use core::f64::consts::PI; let mut h = Histogram100::with_const_width(-PI, PI); let dist = rand_distr::UnitCircle; let mut rng = rand_pcg::Pcg32::from_entropy(); diff --git a/src/distributions/distribution.rs b/src/distributions/distribution.rs index a516a906bda..c5cf6a607b4 100644 --- a/src/distributions/distribution.rs +++ b/src/distributions/distribution.rs @@ -209,7 +209,7 @@ pub trait DistString { #[cfg(test)] mod tests { - use crate::distributions::{Alphanumeric, Distribution, Standard, Uniform}; + use crate::distributions::{Distribution, Uniform}; use crate::Rng; #[test] @@ -258,7 +258,7 @@ mod tests { #[cfg(feature = "alloc")] fn test_dist_string() { use core::str; - use crate::distributions::DistString; + use crate::distributions::{Alphanumeric, DistString, Standard}; let mut rng = crate::test::rng(213); let s1 = Alphanumeric.sample_string(&mut rng, 20); diff --git a/src/distributions/other.rs b/src/distributions/other.rs index 4c2471e6273..6fb1307c38f 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -21,7 +21,7 @@ use crate::Rng; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; #[cfg(feature = "min_const_gen")] -use std::mem::{self, MaybeUninit}; +use core::mem::{self, MaybeUninit}; // ----- Sampling distributions ----- diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index 11a791ef8d1..03d44023741 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -103,8 +103,7 @@ //! [`UniformDuration`]: crate::distributions::uniform::UniformDuration //! [`SampleBorrow::borrow`]: crate::distributions::uniform::SampleBorrow::borrow -#[cfg(not(feature = "std"))] use core::time::Duration; -#[cfg(feature = "std")] use std::time::Duration; +use core::time::Duration; use core::ops::{Range, RangeInclusive}; use crate::distributions::float::IntoFloat; @@ -1153,7 +1152,8 @@ mod tests { #[test] #[cfg(feature = "serde1")] fn test_serialization_uniform_duration() { - let distr = UniformDuration::new(std::time::Duration::from_secs(10), std::time::Duration::from_secs(60)); + use core::time::Duration; + let distr = UniformDuration::new(Duration::from_secs(10), Duration::from_secs(60)); let de_distr: UniformDuration = bincode::deserialize(&bincode::serialize(&distr).unwrap()).unwrap(); assert_eq!( distr.offset, de_distr.offset @@ -1503,8 +1503,7 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] // Miri is too slow fn test_durations() { - #[cfg(not(feature = "std"))] use core::time::Duration; - #[cfg(feature = "std")] use std::time::Duration; + use core::time::Duration; let mut rng = crate::test::rng(253); diff --git a/src/seq/index.rs b/src/seq/index.rs index ae36c323708..de61b900d36 100644 --- a/src/seq/index.rs +++ b/src/seq/index.rs @@ -272,8 +272,8 @@ where R: Rng + ?Sized { /// `O(length + amount * log length)` time otherwise. /// /// Panics if `amount > length`. -#[cfg(feature = "std")] -#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] +#[cfg(feature = "alloc")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pub fn sample_weighted( rng: &mut R, length: usize, weight: F, amount: usize, ) -> Result @@ -305,7 +305,7 @@ where /// + amount * log length)` time otherwise. /// /// Panics if `amount > length`. -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] fn sample_efraimidis_spirakis( rng: &mut R, length: N, weight: F, amount: N, ) -> Result @@ -380,7 +380,7 @@ where #[cfg(not(feature = "nightly"))] { - use std::collections::BinaryHeap; + use alloc::collections::BinaryHeap; // Partially sort the array such that the `amount` elements with the largest // keys are first using a binary max heap. @@ -621,7 +621,7 @@ mod test { assert_eq!(v1, v2); } - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] #[test] fn test_sample_weighted() { let seed_rng = crate::test::rng; From f5cfbc4b19400e548b1af2b5d81919402d3d7fd0 Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Mon, 6 Sep 2021 12:19:39 -0400 Subject: [PATCH 2/3] revert sample_efraimidis_spirakis and fix suggestions --- .github/workflows/test.yml | 6 +++--- src/distributions/other.rs | 2 +- src/distributions/uniform.rs | 3 --- src/seq/index.rs | 14 +++++++------- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eaee94097d7..caaea15d88b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,7 +23,7 @@ jobs: env: RUSTDOCFLAGS: --cfg doc_cfg # --all builds all crates, but with default features for other crates (okay in this case) - run: cargo deadlinks --ignore-fragments -- --all --features nightly,serde1,getrandom,small_rng + run: cargo deadlinks --ignore-fragments -- --all --features nightly,serde1,getrandom,small_rng,min_const_gen test: runs-on: ${{ matrix.os }} @@ -80,8 +80,8 @@ jobs: - name: Test rand run: | cargo test --target ${{ matrix.target }} --lib --tests --no-default-features - cargo build --target ${{ matrix.target }} --no-default-features --features alloc,getrandom,small_rng - cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features=alloc,getrandom,small_rng + cargo build --target ${{ matrix.target }} --no-default-features --features alloc,getrandom,small_rng,min_const_gen + cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features=alloc,getrandom,small_rng,min_const_gen # all stable features: cargo test --target ${{ matrix.target }} --features=serde1,log,small_rng cargo test --target ${{ matrix.target }} --examples diff --git a/src/distributions/other.rs b/src/distributions/other.rs index 6fb1307c38f..03802a76d5f 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -189,8 +189,8 @@ tuple_impl! {A, B, C, D, E, F, G, H, I, J} tuple_impl! {A, B, C, D, E, F, G, H, I, J, K} tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L} -#[cfg_attr(doc_cfg, doc(cfg(feature = "min_const_gen")))] #[cfg(feature = "min_const_gen")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "min_const_gen")))] impl Distribution<[T; N]> for Standard where Standard: Distribution { diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index 03d44023741..096009f9ecb 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -1152,7 +1152,6 @@ mod tests { #[test] #[cfg(feature = "serde1")] fn test_serialization_uniform_duration() { - use core::time::Duration; let distr = UniformDuration::new(Duration::from_secs(10), Duration::from_secs(60)); let de_distr: UniformDuration = bincode::deserialize(&bincode::serialize(&distr).unwrap()).unwrap(); assert_eq!( @@ -1503,8 +1502,6 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] // Miri is too slow fn test_durations() { - use core::time::Duration; - let mut rng = crate::test::rng(253); let v = &[ diff --git a/src/seq/index.rs b/src/seq/index.rs index de61b900d36..b38e4649d1f 100644 --- a/src/seq/index.rs +++ b/src/seq/index.rs @@ -16,11 +16,11 @@ use alloc::collections::BTreeSet; #[cfg(feature = "std")] use std::collections::HashSet; -#[cfg(feature = "alloc")] -use crate::distributions::{uniform::SampleUniform, Distribution, Uniform}; #[cfg(feature = "std")] use crate::distributions::WeightedError; -use crate::Rng; + +#[cfg(feature = "alloc")] +use crate::{Rng, distributions::{uniform::SampleUniform, Distribution, Uniform}}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; @@ -272,8 +272,8 @@ where R: Rng + ?Sized { /// `O(length + amount * log length)` time otherwise. /// /// Panics if `amount > length`. -#[cfg(feature = "alloc")] -#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] +#[cfg(feature = "std")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] pub fn sample_weighted( rng: &mut R, length: usize, weight: F, amount: usize, ) -> Result @@ -305,7 +305,7 @@ where /// + amount * log length)` time otherwise. /// /// Panics if `amount > length`. -#[cfg(feature = "alloc")] +#[cfg(feature = "std")] fn sample_efraimidis_spirakis( rng: &mut R, length: N, weight: F, amount: N, ) -> Result @@ -621,7 +621,7 @@ mod test { assert_eq!(v1, v2); } - #[cfg(feature = "alloc")] + #[cfg(feature = "std")] #[test] fn test_sample_weighted() { let seed_rng = crate::test::rng; From 6d4baefac7910f5d87685138c80f177485a2667c Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Mon, 6 Sep 2021 12:49:40 -0400 Subject: [PATCH 3/3] move min_const_gen test to nightly workflow --- .github/workflows/test.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index caaea15d88b..02b39b564ea 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -77,11 +77,12 @@ jobs: cargo test --target ${{ matrix.target }} --all-features cargo test --target ${{ matrix.target }} --benches --features=nightly cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --benches + cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features min_const_gen - name: Test rand run: | cargo test --target ${{ matrix.target }} --lib --tests --no-default-features - cargo build --target ${{ matrix.target }} --no-default-features --features alloc,getrandom,small_rng,min_const_gen - cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features=alloc,getrandom,small_rng,min_const_gen + cargo build --target ${{ matrix.target }} --no-default-features --features alloc,getrandom,small_rng + cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features=alloc,getrandom,small_rng # all stable features: cargo test --target ${{ matrix.target }} --features=serde1,log,small_rng cargo test --target ${{ matrix.target }} --examples