nio_task/
id.rs

1use std::{fmt, future::poll_fn, num::NonZero, task::Poll};
2
3use super::raw::RawTask;
4use crate::thin_arc::ThinArc;
5
6#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
7pub struct TaskId(pub(crate) NonZero<usize>);
8
9impl TaskId {
10    #[inline]
11    pub(super) fn new(task: &RawTask) -> TaskId {
12        TaskId(unsafe { NonZero::new_unchecked(ThinArc::as_ptr(task).addr()) })
13    }
14
15    #[inline]
16    pub fn get(&self) -> NonZero<usize> {
17        self.0
18    }
19}
20
21pub fn id() -> impl Future<Output = TaskId> {
22    poll_fn(|cx| {
23        Poll::Ready(TaskId(unsafe {
24            NonZero::new_unchecked(cx.waker().data().addr())
25        }))
26    })
27}
28
29impl fmt::Display for TaskId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        self.0.fmt(f)
32    }
33}