Skip to content

Commit 714f217

Browse files
committed
remove rand dependency, use internal prng
1 parent 1be1a61 commit 714f217

File tree

7 files changed

+23
-41
lines changed

7 files changed

+23
-41
lines changed

futures-util/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ Common utilities and extension traits for the futures-rs library.
1212

1313
[features]
1414
default = ["std", "async-await", "async-await-macro"]
15-
std = ["alloc", "futures-core/std", "futures-task/std", "rand/std", "rand/std_rng", "slab"]
16-
getrandom = ["rand/getrandom"]
15+
std = ["alloc", "futures-core/std", "futures-task/std", "slab"]
1716
alloc = ["futures-core/alloc", "futures-task/alloc"]
1817
async-await = []
1918
async-await-macro = ["async-await", "futures-macro"]
@@ -44,7 +43,6 @@ futures_01 = { version = "0.1.25", optional = true, package = "futures" }
4443
tokio-io = { version = "0.1.9", optional = true }
4544
pin-utils = "0.1.0"
4645
pin-project-lite = "0.2.6"
47-
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
4846

4947
[dev-dependencies]
5048
futures = { path = "../futures", features = ["async-await", "thread-pool"] }

futures-util/src/async_await/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ mod stream_select_mod;
3838
pub use self::stream_select_mod::*;
3939

4040
#[cfg(feature = "std")]
41-
#[cfg(feature = "async-await-macro")]
4241
mod random;
4342
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/64762
4443
#[cfg(feature = "std")]
45-
#[cfg(feature = "async-await-macro")]
4644
pub use self::random::*;
4745

4846
#[doc(hidden)]

futures-util/src/async_await/random.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn shuffle<T>(slice: &mut [T]) {
1717
}
1818

1919
/// Return a value from `0..n`.
20-
fn gen_index(n: usize) -> usize {
20+
pub(crate) fn gen_index(n: usize) -> usize {
2121
(random() % n as u64) as usize
2222
}
2323

futures-util/src/future/select.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ use crate::future::{Either, FutureExt};
33
use core::pin::Pin;
44
use futures_core::future::{FusedFuture, Future};
55
use futures_core::task::{Context, Poll};
6-
use rand::rngs::SmallRng;
7-
use rand::Rng;
86

97
/// Future for the [`select()`] function.
108
#[must_use = "futures do nothing unless you `.await` or poll them"]
119
#[derive(Debug)]
1210
pub struct Select<A, B> {
1311
inner: Option<(A, B)>,
14-
rng: SmallRng,
1512
}
1613

1714
impl<A: Unpin, B: Unpin> Unpin for Select<A, B> {}
@@ -94,7 +91,6 @@ where
9491
{
9592
assert_future::<Either<(A::Output, B), (B::Output, A)>, _>(Select {
9693
inner: Some((future1, future2)),
97-
rng: crate::gen_rng(),
9894
})
9995
}
10096

@@ -116,7 +112,6 @@ where
116112
}
117113
}
118114

119-
let a_polls_first = self.rng.gen::<bool>();
120115
let (a, b) = self.inner.as_mut().expect("cannot poll Select twice");
121116

122117
macro_rules! poll_wrap {
@@ -127,13 +122,20 @@ where
127122
};
128123
}
129124

130-
if a_polls_first {
125+
#[cfg(feature = "std")]
126+
if crate::gen_index(2) == 0 {
131127
poll_wrap!(a, unwrap_option(self.inner.take()).1, Either::Left);
132128
poll_wrap!(b, unwrap_option(self.inner.take()).0, Either::Right);
133129
} else {
134130
poll_wrap!(b, unwrap_option(self.inner.take()).0, Either::Right);
135131
poll_wrap!(a, unwrap_option(self.inner.take()).1, Either::Left);
136132
}
133+
134+
#[cfg(not(feature = "std"))]
135+
{
136+
poll_wrap!(a, unwrap_option(self.inner.take()).1, Either::Left);
137+
poll_wrap!(b, unwrap_option(self.inner.take()).0, Either::Right);
138+
}
137139
Poll::Pending
138140
}
139141
}

futures-util/src/future/select_ok.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ use core::mem;
66
use core::pin::Pin;
77
use futures_core::future::{Future, TryFuture};
88
use futures_core::task::{Context, Poll};
9-
use rand::prelude::SliceRandom;
10-
use rand::rngs::SmallRng;
119

1210
/// Future for the [`select_ok`] function.
1311
#[derive(Debug)]
1412
#[must_use = "futures do nothing unless you `.await` or poll them"]
1513
pub struct SelectOk<Fut> {
1614
inner: Vec<Fut>,
17-
rng: SmallRng,
1815
}
1916

2017
impl<Fut: Unpin> Unpin for SelectOk<Fut> {}
@@ -48,7 +45,7 @@ where
4845
I: IntoIterator,
4946
I::Item: TryFuture + Unpin,
5047
{
51-
let ret = SelectOk { inner: iter.into_iter().collect(), rng: crate::gen_rng() };
48+
let ret = SelectOk { inner: iter.into_iter().collect() };
5249
assert!(!ret.inner.is_empty(), "iterator provided to select_ok was empty");
5350
assert_future::<
5451
Result<(<I::Item as TryFuture>::Ok, Vec<I::Item>), <I::Item as TryFuture>::Error>,
@@ -60,8 +57,9 @@ impl<Fut: TryFuture + Unpin> Future for SelectOk<Fut> {
6057
type Output = Result<(Fut::Ok, Vec<Fut>), Fut::Error>;
6158

6259
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
63-
let Self { inner, rng } = &mut *self;
64-
inner.shuffle(rng);
60+
let Self { inner } = &mut *self;
61+
#[cfg(feature = "std")]
62+
crate::shuffle(inner);
6563
// loop until we've either exhausted all errors, a success was hit, or nothing is ready
6664
loop {
6765
let item = inner.iter_mut().enumerate().find_map(|(i, f)| match f.try_poll_unpin(cx) {
@@ -74,7 +72,7 @@ impl<Fut: TryFuture + Unpin> Future for SelectOk<Fut> {
7472
drop(inner.remove(idx));
7573
match res {
7674
Ok(e) => {
77-
let rest = mem::take(&mut self.inner);
75+
let rest = mem::take(inner);
7876
return Poll::Ready(Ok((e, rest)));
7977
}
8078
Err(e) => {

futures-util/src/future/try_select.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ use crate::future::{Either, TryFutureExt};
22
use core::pin::Pin;
33
use futures_core::future::{Future, TryFuture};
44
use futures_core::task::{Context, Poll};
5-
use rand::rngs::SmallRng;
6-
use rand::Rng;
75

86
/// Future for the [`try_select()`] function.
97
#[must_use = "futures do nothing unless you `.await` or poll them"]
108
#[derive(Debug)]
119
pub struct TrySelect<A, B> {
1210
inner: Option<(A, B)>,
13-
rng: SmallRng,
1411
}
1512

1613
impl<A: Unpin, B: Unpin> Unpin for TrySelect<A, B> {}
@@ -63,7 +60,6 @@ where
6360
{
6461
super::assert_future::<Result<EitherOk<A, B>, EitherErr<A, B>>, _>(TrySelect {
6562
inner: Some((future1, future2)),
66-
rng: crate::gen_rng(),
6763
})
6864
}
6965

@@ -92,10 +88,17 @@ where
9288
}
9389
};
9490
}
95-
if self.rng.gen::<bool>() {
91+
92+
#[cfg(feature = "std")]
93+
if crate::gen_index(2) == 0 {
9694
poll_wrap!(a, b, Either::Left, Either::Right)
9795
} else {
9896
poll_wrap!(b, a, Either::Right, Either::Left)
9997
}
98+
99+
#[cfg(not(feature = "std"))]
100+
{
101+
poll_wrap!(a, b, Either::Left, Either::Right)
102+
}
100103
}
101104
}

futures-util/src/lib.rs

-17
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ extern crate alloc;
3131
// Macro re-exports
3232
pub use futures_core::ready;
3333
pub use pin_utils::pin_mut;
34-
use rand::rngs::SmallRng;
35-
use rand::SeedableRng;
3634

3735
#[cfg(feature = "async-await")]
3836
#[macro_use]
@@ -338,18 +336,3 @@ mod abortable;
338336

339337
mod fns;
340338
mod unfold_state;
341-
342-
fn gen_rng() -> SmallRng {
343-
#[cfg(feature = "std")]
344-
{
345-
SmallRng::from_rng(rand::thread_rng()).expect("generating SmallRng via thread_rng failed")
346-
}
347-
#[cfg(all(feature = "getrandom", not(feature = "std")))]
348-
{
349-
SmallRng::from_entropy()
350-
}
351-
#[cfg(not(any(feature = "getrandom", feature = "std")))]
352-
{
353-
SmallRng::seed_from_u64(0)
354-
}
355-
}

0 commit comments

Comments
 (0)