Skip to content

Commit b4c7517

Browse files
committed
Final fixes to enable testing with no_std
1 parent 7ccf19d commit b4c7517

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/distributions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ mod tests {
405405
}
406406
#[test] #[should_panic]
407407
fn test_weighted_choice_weight_overflows() {
408-
let x = ::std::u32::MAX / 2; // x + x + 2 is the overflow
408+
let x = ::core::u32::MAX / 2; // x + x + 2 is the overflow
409409
WeightedChoice::new(&mut [Weighted { weight: x, item: 0 },
410410
Weighted { weight: 1, item: 1 },
411411
Weighted { weight: x, item: 2 },

src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ use core::mem;
255255
#[cfg(feature="std")] use std::cell::RefCell;
256256
#[cfg(feature="std")] use std::io;
257257
#[cfg(feature="std")] use std::rc::Rc;
258+
#[cfg(all(feature="alloc", not(feature="std")))] use alloc::boxed::Box;
258259

259260
// external rngs
260261
pub use jitter::JitterRng;
@@ -624,8 +625,6 @@ impl<'a, R: ?Sized> Rng for &'a mut R where R: Rng {
624625
}
625626
}
626627

627-
#[cfg(all(feature="alloc", not(feature="std")))]
628-
use alloc::boxed::Box;
629628
#[cfg(any(feature="std", feature="alloc"))]
630629
impl<R: ?Sized> Rng for Box<R> where R: Rng {
631630
fn next_u32(&mut self) -> u32 {
@@ -984,8 +983,11 @@ pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>
984983
#[cfg(test)]
985984
mod test {
986985
use impls;
987-
use super::{Rng, thread_rng, random, SeedableRng, StdRng, weak_rng};
988-
use std::iter::repeat;
986+
#[cfg(feature="std")]
987+
use super::{random, thread_rng, weak_rng};
988+
use super::{Rng, SeedableRng, StdRng};
989+
#[cfg(feature="alloc")]
990+
use alloc::boxed::Box;
989991

990992
pub struct TestRng<R> { inner: R }
991993

@@ -1041,8 +1043,9 @@ mod test {
10411043
let lengths = [0, 1, 2, 3, 4, 5, 6, 7,
10421044
80, 81, 82, 83, 84, 85, 86, 87];
10431045
for &n in lengths.iter() {
1044-
let mut v = repeat(0u8).take(n).collect::<Vec<_>>();
1045-
r.fill_bytes(&mut v);
1046+
let mut buffer = [0u8; 87];
1047+
let mut v = &mut buffer[0..n];
1048+
r.fill_bytes(v);
10461049

10471050
// use this to get nicer error messages.
10481051
for (i, &byte) in v.iter().enumerate() {
@@ -1139,6 +1142,7 @@ mod test {
11391142
}
11401143

11411144
#[test]
1145+
#[cfg(feature="std")]
11421146
fn test_thread_rng() {
11431147
let mut r = thread_rng();
11441148
r.gen::<i32>();
@@ -1150,6 +1154,7 @@ mod test {
11501154
}
11511155

11521156
#[test]
1157+
#[cfg(any(feature="std", feature="alloc"))]
11531158
fn test_rng_trait_object() {
11541159
let mut rng = rng(109);
11551160
{
@@ -1175,6 +1180,7 @@ mod test {
11751180
}
11761181

11771182
#[test]
1183+
#[cfg(feature="std")]
11781184
fn test_random() {
11791185
// not sure how to test this aside from just getting some values
11801186
let _n : usize = random();
@@ -1214,6 +1220,7 @@ mod test {
12141220
}
12151221

12161222
#[test]
1223+
#[cfg(feature="std")]
12171224
fn test_weak_rng() {
12181225
let s = weak_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
12191226
let mut ra: StdRng = SeedableRng::from_seed(&s[..]);

0 commit comments

Comments
 (0)