Skip to content

Commit fcc5baf

Browse files
authored
Merge pull request #1173 from bhgomes/no-std-fix
Fix no_std compatibility issues
2 parents 0932a58 + 2bddede commit fcc5baf

File tree

11 files changed

+23
-26
lines changed

11 files changed

+23
-26
lines changed

.github/workflows/test.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
env:
2424
RUSTDOCFLAGS: --cfg doc_cfg
2525
# --all builds all crates, but with default features for other crates (okay in this case)
26-
run: cargo deadlinks --ignore-fragments -- --all --features nightly,serde1,getrandom,small_rng
26+
run: cargo deadlinks --ignore-fragments -- --all --features nightly,serde1,getrandom,small_rng,min_const_gen
2727

2828
test:
2929
runs-on: ${{ matrix.os }}
@@ -77,6 +77,7 @@ jobs:
7777
cargo test --target ${{ matrix.target }} --all-features
7878
cargo test --target ${{ matrix.target }} --benches --features=nightly
7979
cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --benches
80+
cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features min_const_gen
8081
- name: Test rand
8182
run: |
8283
cargo test --target ${{ matrix.target }} --lib --tests --no-default-features

benches/distributions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const RAND_BENCH_N: u64 = 1000;
1818

1919
use rand::distributions::{Alphanumeric, Open01, OpenClosed01, Standard, Uniform};
2020
use rand::distributions::uniform::{UniformInt, UniformSampler};
21-
use std::mem::size_of;
22-
use std::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};
23-
use std::time::Duration;
21+
use core::mem::size_of;
22+
use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};
23+
use core::time::Duration;
2424
use test::{Bencher, black_box};
2525

2626
use rand::prelude::*;
@@ -199,7 +199,7 @@ macro_rules! gen_range_int {
199199
for _ in 0..RAND_BENCH_N {
200200
accum = accum.wrapping_add(rng.gen_range($low..high));
201201
// force recalculation of range each time
202-
high = high.wrapping_add(1) & std::$ty::MAX;
202+
high = high.wrapping_add(1) & core::$ty::MAX;
203203
}
204204
accum
205205
});

benches/generators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate test;
1414
const RAND_BENCH_N: u64 = 1000;
1515
const BYTES_LEN: usize = 1024;
1616

17-
use std::mem::size_of;
17+
use core::mem::size_of;
1818
use test::{black_box, Bencher};
1919

2020
use rand::prelude::*;

benches/misc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn misc_bernoulli_var(b: &mut Bencher) {
9898

9999
#[bench]
100100
fn gen_1kb_u16_iter_repeat(b: &mut Bencher) {
101-
use std::iter;
101+
use core::iter;
102102
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
103103
b.iter(|| {
104104
let v: Vec<u16> = iter::repeat(()).map(|()| rng.gen()).take(512).collect();
@@ -141,7 +141,7 @@ fn gen_1kb_u16_fill(b: &mut Bencher) {
141141

142142
#[bench]
143143
fn gen_1kb_u64_iter_repeat(b: &mut Bencher) {
144-
use std::iter;
144+
use core::iter;
145145
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
146146
b.iter(|| {
147147
let v: Vec<u64> = iter::repeat(()).map(|()| rng.gen()).take(128).collect();

benches/seq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use test::Bencher;
1515

1616
use rand::prelude::*;
1717
use rand::seq::*;
18-
use std::mem::size_of;
18+
use core::mem::size_of;
1919

2020
// We force use of 32-bit RNG since seq code is optimised for use with 32-bit
2121
// generators on all platforms.
@@ -116,7 +116,7 @@ impl<I: ExactSizeIterator + Iterator + Clone> Iterator for WindowHintedIterator<
116116
}
117117

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

rand_distr/benches/src/distributions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use criterion::{criterion_group, criterion_main, Criterion,
1717
Throughput};
1818
use criterion_cycles_per_byte::CyclesPerByte;
1919

20-
use std::mem::size_of;
20+
use core::mem::size_of;
2121

2222
use rand::prelude::*;
2323
use rand_distr::*;

rand_distr/tests/uniformity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn unit_sphere() {
4848

4949
#[test]
5050
fn unit_circle() {
51-
use std::f64::consts::PI;
51+
use core::f64::consts::PI;
5252
let mut h = Histogram100::with_const_width(-PI, PI);
5353
let dist = rand_distr::UnitCircle;
5454
let mut rng = rand_pcg::Pcg32::from_entropy();

src/distributions/distribution.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub trait DistString {
209209

210210
#[cfg(test)]
211211
mod tests {
212-
use crate::distributions::{Alphanumeric, Distribution, Standard, Uniform};
212+
use crate::distributions::{Distribution, Uniform};
213213
use crate::Rng;
214214

215215
#[test]
@@ -258,7 +258,7 @@ mod tests {
258258
#[cfg(feature = "alloc")]
259259
fn test_dist_string() {
260260
use core::str;
261-
use crate::distributions::DistString;
261+
use crate::distributions::{Alphanumeric, DistString, Standard};
262262
let mut rng = crate::test::rng(213);
263263

264264
let s1 = Alphanumeric.sample_string(&mut rng, 20);

src/distributions/other.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::Rng;
2121
#[cfg(feature = "serde1")]
2222
use serde::{Serialize, Deserialize};
2323
#[cfg(feature = "min_const_gen")]
24-
use std::mem::{self, MaybeUninit};
24+
use core::mem::{self, MaybeUninit};
2525

2626

2727
// ----- Sampling distributions -----
@@ -189,8 +189,8 @@ tuple_impl! {A, B, C, D, E, F, G, H, I, J}
189189
tuple_impl! {A, B, C, D, E, F, G, H, I, J, K}
190190
tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L}
191191

192-
#[cfg_attr(doc_cfg, doc(cfg(feature = "min_const_gen")))]
193192
#[cfg(feature = "min_const_gen")]
193+
#[cfg_attr(doc_cfg, doc(cfg(feature = "min_const_gen")))]
194194
impl<T, const N: usize> Distribution<[T; N]> for Standard
195195
where Standard: Distribution<T>
196196
{

src/distributions/uniform.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@
103103
//! [`UniformDuration`]: crate::distributions::uniform::UniformDuration
104104
//! [`SampleBorrow::borrow`]: crate::distributions::uniform::SampleBorrow::borrow
105105
106-
#[cfg(not(feature = "std"))] use core::time::Duration;
107-
#[cfg(feature = "std")] use std::time::Duration;
106+
use core::time::Duration;
108107
use core::ops::{Range, RangeInclusive};
109108

110109
use crate::distributions::float::IntoFloat;
@@ -1153,7 +1152,7 @@ mod tests {
11531152
#[test]
11541153
#[cfg(feature = "serde1")]
11551154
fn test_serialization_uniform_duration() {
1156-
let distr = UniformDuration::new(std::time::Duration::from_secs(10), std::time::Duration::from_secs(60));
1155+
let distr = UniformDuration::new(Duration::from_secs(10), Duration::from_secs(60));
11571156
let de_distr: UniformDuration = bincode::deserialize(&bincode::serialize(&distr).unwrap()).unwrap();
11581157
assert_eq!(
11591158
distr.offset, de_distr.offset
@@ -1503,9 +1502,6 @@ mod tests {
15031502
#[test]
15041503
#[cfg_attr(miri, ignore)] // Miri is too slow
15051504
fn test_durations() {
1506-
#[cfg(not(feature = "std"))] use core::time::Duration;
1507-
#[cfg(feature = "std")] use std::time::Duration;
1508-
15091505
let mut rng = crate::test::rng(253);
15101506

15111507
let v = &[

src/seq/index.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use alloc::collections::BTreeSet;
1717
#[cfg(feature = "std")] use std::collections::HashSet;
1818

19-
#[cfg(feature = "alloc")]
20-
use crate::distributions::{uniform::SampleUniform, Distribution, Uniform};
2119
#[cfg(feature = "std")]
2220
use crate::distributions::WeightedError;
23-
use crate::Rng;
21+
22+
#[cfg(feature = "alloc")]
23+
use crate::{Rng, distributions::{uniform::SampleUniform, Distribution, Uniform}};
2424

2525
#[cfg(feature = "serde1")]
2626
use serde::{Serialize, Deserialize};
@@ -380,7 +380,7 @@ where
380380

381381
#[cfg(not(feature = "nightly"))]
382382
{
383-
use std::collections::BinaryHeap;
383+
use alloc::collections::BinaryHeap;
384384

385385
// Partially sort the array such that the `amount` elements with the largest
386386
// keys are first using a binary max heap.

0 commit comments

Comments
 (0)