Skip to content

Commit

Permalink
Merge pull request #1173 from bhgomes/no-std-fix
Browse files Browse the repository at this point in the history
Fix no_std compatibility issues
  • Loading branch information
vks committed Sep 10, 2021
2 parents 0932a58 + 2bddede commit fcc5baf
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -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 }}
Expand Down Expand Up @@ -77,6 +77,7 @@ 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
Expand Down
8 changes: 4 additions & 4 deletions benches/distributions.rs
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion benches/generators.rs
Expand Up @@ -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::*;
Expand Down
4 changes: 2 additions & 2 deletions benches/misc.rs
Expand Up @@ -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<u16> = iter::repeat(()).map(|()| rng.gen()).take(512).collect();
Expand Down Expand Up @@ -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<u64> = iter::repeat(()).map(|()| rng.gen()).take(128).collect();
Expand Down
4 changes: 2 additions & 2 deletions benches/seq.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<I: ExactSizeIterator + Iterator + Clone> Iterator for WindowHintedIterator<
}

fn size_hint(&self) -> (usize, Option<usize>) {
(std::cmp::min(self.iter.len(), self.window_size), None)
(core::cmp::min(self.iter.len(), self.window_size), None)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rand_distr/benches/src/distributions.rs
Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/tests/uniformity.rs
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/distributions/distribution.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/distributions/other.rs
Expand Up @@ -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 -----
Expand Down Expand Up @@ -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<T, const N: usize> Distribution<[T; N]> for Standard
where Standard: Distribution<T>
{
Expand Down
8 changes: 2 additions & 6 deletions src/distributions/uniform.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -1153,7 +1152,7 @@ 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));
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
Expand Down Expand Up @@ -1503,9 +1502,6 @@ 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;

let mut rng = crate::test::rng(253);

let v = &[
Expand Down
8 changes: 4 additions & 4 deletions src/seq/index.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fcc5baf

Please sign in to comment.