Skip to main content

async_selector/selector/
id.rs

1use std::{
2    fmt, hash,
3    sync::{Arc, Weak},
4};
5
6use crate::list::Node;
7
8/// Unique ID of a task pushed into a [`Selector`](crate::selector::Selector).
9///
10/// This ID is always unique relative to all other IDs,
11/// including IDs obtained from other selectors.
12#[repr(transparent)]
13pub struct Id<C>(Arc<Node<C>>);
14
15impl<C> Id<C> {
16    pub(super) fn new(node: &Arc<Node<C>>) -> &Self {
17        let ptr = std::ptr::from_ref(node) as *const Self;
18        unsafe { &*ptr }
19    }
20
21    pub(super) fn get(&self) -> &Arc<Node<C>> {
22        &self.0
23    }
24
25    /// Manually wakes this task.
26    ///
27    /// Does nothing if the task is no longer stored in the selector.
28    pub fn wake(&self) {
29        self.0.enqueue_by_ref();
30    }
31}
32
33impl<C> Clone for Id<C> {
34    fn clone(&self) -> Self {
35        Self(self.0.clone())
36    }
37}
38
39impl<C> PartialEq for Id<C> {
40    fn eq(&self, other: &Self) -> bool {
41        let this = Arc::as_ptr(&self.0);
42        let other = Arc::as_ptr(&other.0);
43        this.eq(&other)
44    }
45}
46
47impl<C> Eq for Id<C> {}
48
49impl<C> hash::Hash for Id<C> {
50    fn hash<H: hash::Hasher>(&self, state: &mut H) {
51        Arc::as_ptr(&self.0).hash(state);
52    }
53}
54
55impl<C> fmt::Debug for Id<C> {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.debug_struct("Id")
58            .field("task_ptr", &Arc::as_ptr(&self.0))
59            .field("queue_ptr", &Weak::as_ptr(self.0.queue()))
60            .finish()
61    }
62}