Skip to content

Commit 367d7b7

Browse files
committed
move rotator types into /macros/join and re-export, fix formatting, remove comment inside macro invocation in example
1 parent 3cc5e26 commit 367d7b7

File tree

3 files changed

+81
-74
lines changed

3 files changed

+81
-74
lines changed

tokio/src/macros/join.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ macro_rules! doc {
8888
/// #[tokio::main]
8989
/// async fn main() {
9090
/// let (first, second) = tokio::join!(
91-
/// // do_stuff_async() will always be polled first when woken
9291
/// biased;
9392
/// do_stuff_async(),
94-
/// more_async_work());
93+
/// more_async_work()
94+
/// );
9595
///
9696
/// // do something with the values
9797
/// }
@@ -224,3 +224,34 @@ doc! {macro_rules! join {
224224

225225
() => { async {}.await }
226226
}}
227+
228+
#[derive(Default, Debug)]
229+
/// Rotates by one every [`Self::num_skip`] call up to COUNT - 1.
230+
pub struct Rotator<const COUNT: u32> {
231+
next: u32,
232+
}
233+
234+
impl<const COUNT: u32> Rotator<COUNT> {
235+
/// Rotates by one every [`Self::num_skip`] call up to COUNT - 1
236+
#[inline]
237+
pub fn num_skip(&mut self) -> u32 {
238+
let num_skip = self.next;
239+
self.next += 1;
240+
if self.next == COUNT {
241+
self.next = 0;
242+
}
243+
num_skip
244+
}
245+
}
246+
247+
/// [`Self::num_skip`] always returns 0.
248+
#[derive(Default, Debug)]
249+
pub struct BiasedRotator {}
250+
251+
impl BiasedRotator {
252+
/// Always returns 0
253+
#[inline]
254+
pub fn num_skip(&mut self) -> u32 {
255+
0
256+
}
257+
}

tokio/src/macros/support.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ cfg_macros! {
33

44
pub use std::future::poll_fn;
55

6+
// Used with `join!` and `try_join!`
7+
pub use crate::macros::join::Rotator;
8+
// Used with `join!` and `try_join!`
9+
pub use crate::macros::join::BiasedRotator;
10+
611
#[doc(hidden)]
712
pub fn thread_rng_n(n: u32) -> u32 {
813
crate::runtime::context::thread_rng_n(n)
@@ -23,39 +28,10 @@ cfg_macros! {
2328
Poll::Ready(())
2429
}
2530
}
31+
32+
2633
}
2734

2835
pub use std::future::{Future, IntoFuture};
2936
pub use std::pin::Pin;
3037
pub use std::task::{Context, Poll};
31-
32-
#[doc(hidden)]
33-
#[derive(Default, Debug)]
34-
pub struct Rotator<const COUNT: u32> {
35-
next: u32,
36-
}
37-
38-
impl<const COUNT: u32> Rotator<COUNT> {
39-
#[doc(hidden)]
40-
#[inline]
41-
pub fn num_skip(&mut self) -> u32 {
42-
let num_skip = self.next;
43-
self.next += 1;
44-
if self.next == COUNT {
45-
self.next = 0;
46-
}
47-
num_skip
48-
}
49-
}
50-
51-
#[doc(hidden)]
52-
#[derive(Default, Debug)]
53-
pub struct BiasedRotator {}
54-
55-
impl BiasedRotator {
56-
#[doc(hidden)]
57-
#[inline]
58-
pub fn num_skip(&mut self) -> u32 {
59-
0
60-
}
61-
}

tokio/src/macros/try_join.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ macro_rules! doc {
135135
/// #[tokio::main]
136136
/// async fn main() {
137137
/// let res = tokio::try_join!(
138-
/// //do_stuff_async() will always be polled first when woken
139138
/// biased;
140139
/// do_stuff_async(),
141-
/// more_async_work());
140+
/// more_async_work()
141+
/// );
142142
///
143143
/// match res {
144144
/// Ok((first, second)) => {
@@ -215,53 +215,53 @@ doc! {macro_rules! try_join {
215215
// This loop runs twice and the first `skip` futures
216216
// are not polled in the first iteration.
217217
loop {
218-
$(
219-
if skip == 0 {
220-
if to_run == 0 {
221-
// Every future has been polled
222-
break;
223-
}
224-
to_run -= 1;
218+
$(
219+
if skip == 0 {
220+
if to_run == 0 {
221+
// Every future has been polled
222+
break;
223+
}
224+
to_run -= 1;
225225

226-
// Extract the future for this branch from the tuple.
227-
let ( $($skip,)* fut, .. ) = &mut *futures;
226+
// Extract the future for this branch from the tuple.
227+
let ( $($skip,)* fut, .. ) = &mut *futures;
228228

229-
// Safety: future is stored on the stack above
230-
// and never moved.
231-
let mut fut = unsafe { Pin::new_unchecked(fut) };
229+
// Safety: future is stored on the stack above
230+
// and never moved.
231+
let mut fut = unsafe { Pin::new_unchecked(fut) };
232232

233-
// Try polling
234-
if fut.as_mut().poll(cx).is_pending() {
235-
is_pending = true;
236-
} else if fut.as_mut().output_mut().expect("expected completed future").is_err() {
237-
return Ready(Err(fut.take_output().expect("expected completed future").err().unwrap()))
238-
}
239-
} else {
240-
// Future skipped, one less future to skip in the next iteration
241-
skip -= 1;
233+
// Try polling
234+
if fut.as_mut().poll(cx).is_pending() {
235+
is_pending = true;
236+
} else if fut.as_mut().output_mut().expect("expected completed future").is_err() {
237+
return Ready(Err(fut.take_output().expect("expected completed future").err().unwrap()))
242238
}
243-
)*
239+
} else {
240+
// Future skipped, one less future to skip in the next iteration
241+
skip -= 1;
244242
}
243+
)*
244+
}
245245

246-
if is_pending {
247-
Pending
248-
} else {
249-
Ready(Ok(($({
250-
// Extract the future for this branch from the tuple.
251-
let ( $($skip,)* fut, .. ) = &mut futures;
246+
if is_pending {
247+
Pending
248+
} else {
249+
Ready(Ok(($({
250+
// Extract the future for this branch from the tuple.
251+
let ( $($skip,)* fut, .. ) = &mut futures;
252252

253-
// Safety: future is stored on the stack above
254-
// and never moved.
255-
let mut fut = unsafe { Pin::new_unchecked(fut) };
253+
// Safety: future is stored on the stack above
254+
// and never moved.
255+
let mut fut = unsafe { Pin::new_unchecked(fut) };
256256

257-
fut
258-
.take_output()
259-
.expect("expected completed future")
260-
.ok()
261-
.expect("expected Ok(_)")
262-
},)*)))
263-
}
264-
}).await
257+
fut
258+
.take_output()
259+
.expect("expected completed future")
260+
.ok()
261+
.expect("expected Ok(_)")
262+
},)*)))
263+
}
264+
}).await
265265
}};
266266

267267
// ===== Normalize =====

0 commit comments

Comments
 (0)