mod join;
mod select;
#[doc(hidden)]
pub mod support {
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
pub use core::future::poll_fn;
pub enum Branch<T, R> {
Taken(T),
Next(R),
}
pub struct AllDisabled;
#[inline]
pub fn start(branches: u32) -> u32 {
crate::rt::rng::random_below(branches as usize) as u32
}
#[inline]
pub fn proceed(cx: &mut Context<'_>) -> bool {
if crate::coop::has_budget_remaining() {
true
} else {
cx.waker().wake_by_ref();
false
}
}
pub enum MaybeDone<F: Future> {
Future(F),
Done(F::Output),
Gone,
}
impl<F: Future> MaybeDone<F> {
pub fn new(future: F) -> Self {
Self::Future(future)
}
#[expect(
clippy::panic,
reason = "polled after its output was taken, which the macros never do"
)]
pub fn poll_done(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool {
let output = match unsafe { self.as_mut().get_unchecked_mut() } {
Self::Future(future) => match unsafe { Pin::new_unchecked(future) }.poll(cx) {
Poll::Ready(output) => output,
Poll::Pending => return false,
},
Self::Done(_) => return true,
Self::Gone => panic!("a joined future was polled after its output was taken"),
};
self.set(Self::Done(output));
true
}
pub fn output_mut(self: Pin<&mut Self>) -> Option<&mut F::Output> {
match unsafe { self.get_unchecked_mut() } {
Self::Done(output) => Some(output),
_ => None,
}
}
pub fn take_output(self: Pin<&mut Self>) -> Option<F::Output> {
if !matches!(&*self, Self::Done(_)) {
return None;
}
match core::mem::replace(unsafe { self.get_unchecked_mut() }, Self::Gone) {
Self::Done(output) => Some(output),
_ => None,
}
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __count {
($($skip:tt)*) => {
(<[()]>::len(&[$($crate::__unit!($skip)),*]) as u32)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __unit {
($skip:tt) => {
()
};
}