Skip to content

Commit

Permalink
Merge pull request #121 from poliorcetics/less-recursive-macro-calls
Browse files Browse the repository at this point in the history
nit: use constant to call recursive macro once instead of several times
  • Loading branch information
yoshuawuyts authored Apr 5, 2023
2 parents 26d24c2 + 9054a57 commit 7d6bdc1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/future/race_ok/tuple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub(crate) use error::AggregateError;

macro_rules! impl_race_ok_tuple {
($StructName:ident $($F:ident)+) => {
/// A workaround to avoid calling the recursive macro several times. Since it's for private
/// use only, we don't case about capitalization so we reuse `$StructName` for simplicity
/// (renaming it as `const LEN: usize = ...`) when in a function for clarity.
#[allow(non_upper_case_globals)]
const $StructName: usize = utils::tuple_len!($($F,)*);

/// Wait for the first successful future to complete.
///
/// This `struct` is created by the [`race_ok`] method on the [`RaceOk`] trait. See
Expand All @@ -33,8 +39,8 @@ macro_rules! impl_race_ok_tuple {
completed: usize,
done: bool,
indexer: utils::Indexer,
errors: [MaybeUninit<ERR>; { utils::tuple_len!($($F,)*) }],
errors_states: PollArray<{ utils::tuple_len!($($F,)*) }>,
errors: [MaybeUninit<ERR>; $StructName],
errors_states: PollArray<{ $StructName }>,
$( #[pin] $F: $F, )*
}

Expand All @@ -56,15 +62,15 @@ macro_rules! impl_race_ok_tuple {
ERR: fmt::Debug,
{
type Output = T;
type Error = AggregateError<ERR, {utils::tuple_len!($($F,)*)}>;
type Error = AggregateError<ERR, { $StructName }>;
type Future = $StructName<T, ERR, $($F::IntoFuture),*>;

fn race_ok(self) -> Self::Future {
let ($($F,)*): ($($F,)*) = self;
$StructName {
completed: 0,
done: false,
indexer: utils::Indexer::new(utils::tuple_len!($($F,)*)),
indexer: utils::Indexer::new($StructName),
errors: array::from_fn(|_| MaybeUninit::uninit()),
errors_states: PollArray::new(),
$($F: $F.into_future()),*
Expand All @@ -77,12 +83,12 @@ macro_rules! impl_race_ok_tuple {
$( $F: Future<Output = Result<T, ERR>>, )*
ERR: fmt::Debug,
{
type Output = Result<T, AggregateError<ERR, {utils::tuple_len!($($F,)*)}>>;
type Output = Result<T, AggregateError<ERR, { $StructName }>>;

fn poll(
self: Pin<&mut Self>, cx: &mut Context<'_>
) -> Poll<Self::Output> {
const LEN: usize = utils::tuple_len!($($F,)*);
const LEN: usize = $StructName;

let mut this = self.project();

Expand Down

0 comments on commit 7d6bdc1

Please sign in to comment.