Skip to content

Commit

Permalink
Switch from no_std feature to default-on std feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Beekman committed Jul 10, 2016
1 parent 3bad2cb commit 6896e25
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ keywords = ["random", "rng"]

[dependencies]
libc = { version = "0.2", optional = true }
core_io = { version = "0.0", optional = true } # enable use of read module on no_std
core_io = { version = "0.0", optional = true } # enable use of read module on not(std)

[features]
default = ["libc"]
no_std = []
box = [] # enable use of Box on no_std, requires alloc crate and feature
vec = [] # enable use of Vec on no_std, requires collections crate and feature
default = ["std"]
std = ["libc"]
box = [] # enable use of Box on not(std), requires alloc crate and feature
vec = [] # enable use of Vec on not(std), requires collections crate and feature

[dev-dependencies]
log = "0.3.0"
16 changes: 8 additions & 8 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use core::marker;
use {Rng, Rand};

pub use self::range::Range;
#[cfg(not(feature="no_std"))] pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
#[cfg(not(feature="no_std"))] pub use self::normal::{Normal, LogNormal};
#[cfg(not(feature="no_std"))] pub use self::exponential::Exp;
#[cfg(feature="std")] pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
#[cfg(feature="std")] pub use self::normal::{Normal, LogNormal};
#[cfg(feature="std")] pub use self::exponential::Exp;

pub mod range;
#[cfg(not(feature="no_std"))] pub mod gamma;
#[cfg(not(feature="no_std"))] pub mod normal;
#[cfg(not(feature="no_std"))] pub mod exponential;
#[cfg(feature="std")] pub mod gamma;
#[cfg(feature="std")] pub mod normal;
#[cfg(feature="std")] pub mod exponential;

/// Types that can be used to create a random instance of `Support`.
pub trait Sample<Support> {
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T> {
}
}

#[cfg(not(feature="no_std"))] mod ziggurat_tables;
#[cfg(feature="std")] mod ziggurat_tables;

/// Sample a random number using the Ziggurat method (specifically the
/// ZIGNOR variant from Doornik 2005). Most of the arguments are
Expand All @@ -218,7 +218,7 @@ impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T> {

// the perf improvement (25-50%) is definitely worth the extra code
// size from force-inlining.
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
#[inline(always)]
fn ziggurat<R: Rng, P, Z>(
rng: &mut R,
Expand Down
54 changes: 27 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,27 @@
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://doc.rust-lang.org/rand/")]

#![cfg_attr(feature="no_std",no_std)]
#![cfg_attr(feature="box",feature(alloc))]
#![cfg_attr(feature="vec",feature(collections))]
#![cfg_attr(not(feature="std"),no_std)]
#![cfg_attr(all(feature="box",not(feature="std")),feature(alloc))]
#![cfg_attr(all(feature="vec",not(feature="std")),feature(collections))]

#[cfg(test)] #[macro_use] extern crate log;

#[cfg(not(feature="no_std"))] extern crate std as core;
#[cfg(feature="core_io")] extern crate core_io as io;
#[cfg(feature="box")] extern crate alloc;
#[cfg(feature="vec")] extern crate collections;
#[cfg(feature="std")] extern crate std as core;
#[cfg(all(feature="core_io",not(feature="std")))] extern crate core_io as io;
#[cfg(all(feature="box",not(feature="std")))] extern crate alloc;
#[cfg(all(feature="vec",not(feature="std")))] extern crate collections;

#[cfg(not(feature="no_std"))] use core::cell::RefCell;
#[cfg(feature="std")] use core::cell::RefCell;
use core::marker;
use core::mem;
#[cfg(not(feature="no_std"))] use std::io;
#[cfg(not(feature="no_std"))] use std::rc::Rc;
#[cfg(feature="std")] use std::io;
#[cfg(feature="std")] use std::rc::Rc;
use core::num::Wrapping as w;
#[cfg(feature="box")] use alloc::boxed::Box;
#[cfg(feature="vec")] use collections::vec::Vec;
#[cfg(all(feature="box",not(feature="std")))] use alloc::boxed::Box;
#[cfg(all(feature="vec",not(feature="std")))] use collections::vec::Vec;

#[cfg(not(feature="no_std"))] pub use os::OsRng;
#[cfg(feature="std")] pub use os::OsRng;

pub use isaac::{IsaacRng, Isaac64Rng};
pub use chacha::ChaChaRng;
Expand All @@ -279,8 +279,8 @@ pub mod isaac;
pub mod chacha;
pub mod reseeding;
mod rand_impls;
#[cfg(not(feature="no_std"))] pub mod os;
#[cfg(any(not(feature="no_std"),feature="core_io"))] pub mod read;
#[cfg(feature="std")] pub mod os;
#[cfg(any(feature="std",feature="core_io"))] pub mod read;

#[allow(bad_style)]
type w64 = w<u64>;
Expand Down Expand Up @@ -587,7 +587,7 @@ impl<'a, R: ?Sized> Rng for &'a mut R where R: Rng {
}
}

#[cfg(any(feature="box",not(feature="no_std")))]
#[cfg(any(feature="box",feature="std"))]
impl<R: ?Sized> Rng for Box<R> where R: Rng {
fn next_u32(&mut self) -> u32 {
(**self).next_u32()
Expand Down Expand Up @@ -822,7 +822,7 @@ impl StdRng {
///
/// Reading the randomness from the OS may fail, and any error is
/// propagated via the `io::Result` return value.
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
pub fn new() -> io::Result<StdRng> {
OsRng::new().map(|mut r| StdRng { rng: r.gen() })
}
Expand Down Expand Up @@ -861,7 +861,7 @@ impl<'a> SeedableRng<&'a [usize]> for StdRng {
///
/// This will read randomness from the operating system to seed the
/// generator.
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
pub fn weak_rng() -> XorShiftRng {
match OsRng::new() {
Ok(mut r) => r.gen(),
Expand All @@ -870,10 +870,10 @@ pub fn weak_rng() -> XorShiftRng {
}

/// Controls how the thread-local RNG is reseeded.
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
struct ThreadRngReseeder;

#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
impl reseeding::Reseeder<StdRng> for ThreadRngReseeder {
fn reseed(&mut self, rng: &mut StdRng) {
*rng = match StdRng::new() {
Expand All @@ -882,14 +882,14 @@ impl reseeding::Reseeder<StdRng> for ThreadRngReseeder {
}
}
}
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
const THREAD_RNG_RESEED_THRESHOLD: u64 = 32_768;
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
type ThreadRngInner = reseeding::ReseedingRng<StdRng, ThreadRngReseeder>;

/// The thread-local RNG.
#[derive(Clone)]
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
pub struct ThreadRng {
rng: Rc<RefCell<ThreadRngInner>>,
}
Expand All @@ -905,7 +905,7 @@ pub struct ThreadRng {
/// if the operating system random number generator is rigged to give
/// the same sequence always. If absolute consistency is required,
/// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
pub fn thread_rng() -> ThreadRng {
// used to make space in TLS for a random number generator
thread_local!(static THREAD_RNG_KEY: Rc<RefCell<ThreadRngInner>> = {
Expand All @@ -922,7 +922,7 @@ pub fn thread_rng() -> ThreadRng {
ThreadRng { rng: THREAD_RNG_KEY.with(|t| t.clone()) }
}

#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
impl Rng for ThreadRng {
fn next_u32(&mut self) -> u32 {
self.rng.borrow_mut().next_u32()
Expand Down Expand Up @@ -980,7 +980,7 @@ impl Rng for ThreadRng {
/// *x = rng.gen();
/// }
/// ```
#[cfg(not(feature="no_std"))]
#[cfg(feature="std")]
#[inline]
pub fn random<T: Rand>() -> T {
thread_rng().gen()
Expand All @@ -997,7 +997,7 @@ pub fn random<T: Rand>() -> T {
/// let sample = sample(&mut rng, 1..100, 5);
/// println!("{:?}", sample);
/// ```
#[cfg(any(feature="vec",not(feature="no_std")))]
#[cfg(any(feature="vec",feature="std"))]
pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>
where I: IntoIterator<Item=T>,
R: Rng,
Expand Down
4 changes: 2 additions & 2 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

//! A wrapper around any Read to treat it as an RNG.

#[cfg(not(feature="no_std"))] use std::io::{self, Read};
#[cfg(feature="no_std")] use io::{self, Read};
#[cfg(not(feature="std"))] use io::{self, Read};
#[cfg(feature="std")] use std::io::{self, Read};
use core::mem;
use Rng;

Expand Down

0 comments on commit 6896e25

Please sign in to comment.