1use alloc::boxed::Box;
2use core::fmt;
3use core::future::Future;
4use core::pin::Pin;
5use core::sync::atomic::{AtomicU32, Ordering};
6use core::task::{Context, Poll};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
9pub(crate) struct AsyncTaskId(u32);
10
11impl AsyncTaskId {
12 pub const fn into(self) -> u32 {
13 self.0
14 }
15
16 pub const fn from(x: u32) -> Self {
17 AsyncTaskId(x)
18 }
19}
20
21impl fmt::Display for AsyncTaskId {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 write!(f, "{}", self.0)
24 }
25}
26
27impl AsyncTaskId {
28 fn new() -> Self {
29 static NEXT_ID: AtomicU32 = AtomicU32::new(0);
30 AsyncTaskId(NEXT_ID.fetch_add(1, Ordering::Relaxed))
31 }
32}
33
34pub(crate) struct AsyncTask {
35 id: AsyncTaskId,
36 future: Pin<Box<dyn Future<Output = ()>>>,
37}
38
39impl AsyncTask {
40 pub fn new(future: impl Future<Output = ()> + 'static) -> AsyncTask {
41 AsyncTask {
42 id: AsyncTaskId::new(),
43 future: Box::pin(future),
44 }
45 }
46
47 pub fn id(&self) -> AsyncTaskId {
48 self.id
49 }
50
51 pub fn poll(&mut self, context: &mut Context<'_>) -> Poll<()> {
52 self.future.as_mut().poll(context)
53 }
54}