Skip to content

Commit 1544c72

Browse files
committed
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
1 parent 7c5ceea commit 1544c72

File tree

10 files changed

+23
-24
lines changed

10 files changed

+23
-24
lines changed

benches/distributions.rs

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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 -----

src/distributions/uniform.rs

Lines changed: 4 additions & 5 deletions
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,8 @@ 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+
use core::time::Duration;
1156+
let distr = UniformDuration::new(Duration::from_secs(10), Duration::from_secs(60));
11571157
let de_distr: UniformDuration = bincode::deserialize(&bincode::serialize(&distr).unwrap()).unwrap();
11581158
assert_eq!(
11591159
distr.offset, de_distr.offset
@@ -1503,8 +1503,7 @@ mod tests {
15031503
#[test]
15041504
#[cfg_attr(miri, ignore)] // Miri is too slow
15051505
fn test_durations() {
1506-
#[cfg(not(feature = "std"))] use core::time::Duration;
1507-
#[cfg(feature = "std")] use std::time::Duration;
1506+
use core::time::Duration;
15081507

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

src/seq/index.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ where R: Rng + ?Sized {
272272
/// `O(length + amount * log length)` time otherwise.
273273
///
274274
/// Panics if `amount > length`.
275-
#[cfg(feature = "std")]
276-
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
275+
#[cfg(feature = "alloc")]
276+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
277277
pub fn sample_weighted<R, F, X>(
278278
rng: &mut R, length: usize, weight: F, amount: usize,
279279
) -> Result<IndexVec, WeightedError>
@@ -305,7 +305,7 @@ where
305305
/// + amount * log length)` time otherwise.
306306
///
307307
/// Panics if `amount > length`.
308-
#[cfg(feature = "std")]
308+
#[cfg(feature = "alloc")]
309309
fn sample_efraimidis_spirakis<R, F, X, N>(
310310
rng: &mut R, length: N, weight: F, amount: N,
311311
) -> Result<IndexVec, WeightedError>
@@ -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.
@@ -621,7 +621,7 @@ mod test {
621621
assert_eq!(v1, v2);
622622
}
623623

624-
#[cfg(feature = "std")]
624+
#[cfg(feature = "alloc")]
625625
#[test]
626626
fn test_sample_weighted() {
627627
let seed_rng = crate::test::rng;

0 commit comments

Comments
 (0)