Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: select! macro for no-std environments #2903

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: select! macro for no std environments
Currently, `select!` macro works on only 64 bit architectures.

Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
ytakano committed Dec 25, 2024
commit 1e0e7d83eae4e02a11d5d1525e21c9e68844b916
2 changes: 0 additions & 2 deletions futures-util/src/async_await/mod.rs
Original file line number Diff line number Diff line change
@@ -34,10 +34,8 @@ mod stream_select_mod;
#[cfg(feature = "async-await-macro")]
pub use self::stream_select_mod::*;

#[cfg(feature = "std")]
#[cfg(feature = "async-await-macro")]
mod random;
#[cfg(feature = "std")]
#[cfg(feature = "async-await-macro")]
pub use self::random::*;

34 changes: 26 additions & 8 deletions futures-util/src/async_await/random.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
use std::{
cell::Cell,
collections::hash_map::DefaultHasher,
hash::Hasher,
num::Wrapping,
sync::atomic::{AtomicUsize, Ordering},
};

// Based on [Fisher–Yates shuffle].
//
// [Fisher–Yates shuffle]: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
@@ -24,7 +16,16 @@ fn gen_index(n: usize) -> usize {
/// Pseudorandom number generator based on [xorshift*].
///
/// [xorshift*]: https://en.wikipedia.org/wiki/Xorshift#xorshift*
#[cfg(feature = "std")]
fn random() -> u64 {
use std::{
cell::Cell,
collections::hash_map::DefaultHasher,
hash::Hasher,
num::Wrapping,
sync::atomic::{AtomicUsize, Ordering},
};

std::thread_local! {
static RNG: Cell<Wrapping<u64>> = Cell::new(Wrapping(prng_seed()));
}
@@ -52,3 +53,20 @@ fn random() -> u64 {
x.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
})
}

#[cfg(not(feature = "std"))]
fn random() -> u64 {
use core::sync::atomic::{AtomicU64, Ordering};

static RNG: AtomicU64 = AtomicU64::new(1);

let mut x = RNG.load(Ordering::Relaxed);

x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
let result = x.wrapping_mul(0x2545_f491_4f6c_dd1d) as u64;
RNG.store(result, Ordering::Relaxed);

result
}
2 changes: 0 additions & 2 deletions futures-util/src/async_await/select_mod.rs
Original file line number Diff line number Diff line change
@@ -305,15 +305,13 @@ macro_rules! document_select_macro {
};
}

#[cfg(feature = "std")]
#[doc(hidden)]
pub use futures_macro::select_internal;

#[doc(hidden)]
pub use futures_macro::select_biased_internal;

document_select_macro! {
#[cfg(feature = "std")]
#[macro_export]
macro_rules! select {
($($tokens:tt)*) => {{
1 change: 0 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
@@ -120,7 +120,6 @@ pub use futures_util::{AsyncBufReadExt, AsyncReadExt, AsyncSeekExt, AsyncWriteEx
// Macro reexports
pub use futures_core::ready; // Readiness propagation
pub use futures_util::pin_mut;
#[cfg(feature = "std")]
#[cfg(feature = "async-await")]
pub use futures_util::select;
#[cfg(feature = "async-await")]