Skip to content

Commit df6583d

Browse files
committed
remove rand dependency, use internal prng
1 parent ceb1595 commit df6583d

File tree

7 files changed

+24
-40
lines changed

7 files changed

+24
-40
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"]
@@ -43,7 +42,6 @@ futures_01 = { version = "0.1.25", optional = true, package = "futures" }
4342
tokio-io = { version = "0.1.9", optional = true }
4443
pin-utils = "0.1.0"
4544
pin-project-lite = "0.2.6"
46-
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
4745

4846
[dev-dependencies]
4947
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

+9-5
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,13 +112,21 @@ where
116112
};
117113
}
118114

119-
if self.rng.gen::<bool>() {
115+
#[cfg(feature = "std")]
116+
if crate::gen_index(2) == 0 {
120117
poll_wrap!(a, b, Either::Left);
121118
poll_wrap!(b, a, Either::Right);
122119
} else {
123120
poll_wrap!(b, a, Either::Right);
124121
poll_wrap!(a, b, Either::Left);
125122
}
123+
124+
#[cfg(not(feature = "std"))]
125+
{
126+
poll_wrap!(a, b, Either::Left);
127+
poll_wrap!(b, a, Either::Right);
128+
}
129+
126130
self.inner = Some((a, b));
127131
Poll::Pending
128132
}

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
@@ -29,8 +29,6 @@ extern crate alloc;
2929
// Macro re-exports
3030
pub use futures_core::ready;
3131
pub use pin_utils::pin_mut;
32-
use rand::rngs::SmallRng;
33-
use rand::SeedableRng;
3432

3533
#[cfg(feature = "async-await")]
3634
#[macro_use]
@@ -336,18 +334,3 @@ mod abortable;
336334

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

0 commit comments

Comments
 (0)