Skip to main content

async_selector/selector/
wrappers.rs

1use std::{
2    ops::{Deref, DerefMut},
3    pin::Pin,
4};
5
6use crate::{list, selector::Id};
7
8/// Immutable reference to a task stored in a [`Selector`](super::Selector).
9pub struct Borrowed<'a, T>(pub(super) list::Borrowed<&'a T>);
10
11impl<'a, T> Borrowed<'a, T> {
12    /// Returns the ID of the task.
13    pub fn id(&self) -> &Id<T> {
14        Id::new(self.0.node())
15    }
16
17    /// Returns a pinned reference to the task.
18    pub fn get_pin(&self) -> Pin<&T> {
19        self.0.get_pin()
20    }
21
22    /// Consumes this wrapper, returning a pinned reference to the task.
23    pub fn into_pin(self) -> Pin<&'a T> {
24        self.0.into_pin()
25    }
26}
27
28impl<C> Deref for Borrowed<'_, C> {
29    type Target = C;
30
31    fn deref(&self) -> &Self::Target {
32        self.get_pin().get_ref()
33    }
34}
35
36/// Mutable reference to a task stored in a [`Selector`](super::Selector).
37pub struct BorrowedMut<'a, T>(pub(super) list::Borrowed<&'a mut T>);
38
39impl<'a, T> BorrowedMut<'a, T> {
40    /// Returns the ID of the task.
41    pub fn id(&self) -> &Id<T> {
42        Id::new(self.0.node())
43    }
44
45    /// Returns a pinned reference to the task.
46    pub fn get_pin(&self) -> Pin<&T> {
47        self.0.get_pin()
48    }
49
50    /// Consumes this wrapper, returning a pinned reference to the task.
51    pub fn into_pin(self) -> Pin<&'a T> {
52        self.0.into_pin_mut().into_ref()
53    }
54
55    /// Returns a mutable pinned reference to the task.
56    pub fn get_pin_mut(&mut self) -> Pin<&mut T> {
57        self.0.get_pin_mut()
58    }
59
60    /// Consumes this wrapper, returning a mutable pinned reference to the task.
61    pub fn into_pin_mut(self) -> Pin<&'a mut T> {
62        self.0.into_pin_mut()
63    }
64}
65
66impl<C> Deref for BorrowedMut<'_, C> {
67    type Target = C;
68
69    fn deref(&self) -> &Self::Target {
70        self.get_pin().get_ref()
71    }
72}
73
74impl<C: Unpin> DerefMut for BorrowedMut<'_, C> {
75    fn deref_mut(&mut self) -> &mut Self::Target {
76        self.get_pin_mut().get_mut()
77    }
78}
79
80/// Task removed from a [`Selector`](super::Selector).
81pub struct Removed<T>(pub(super) list::Removed<T>);
82
83impl<T> Removed<T> {
84    /// Returns the ID of the task.
85    pub fn id(&self) -> &Id<T> {
86        Id::new(self.0.node())
87    }
88
89    /// Returns a pinned reference to the task.
90    pub fn get_pin(&self) -> Pin<&T> {
91        self.0.get_pin()
92    }
93
94    /// Returns a mutable pinned reference to the task.
95    pub fn get_pin_mut(&mut self) -> Pin<&mut T> {
96        self.0.get_pin_mut()
97    }
98
99    /// Consumes this wrapper, returning the task.
100    ///
101    /// Since this method moves the task in memory,
102    /// it is only available when `T` is [`Unpin`].
103    pub fn into_inner(self) -> T
104    where
105        T: Unpin,
106    {
107        self.0.into_inner()
108    }
109}
110
111impl<T> Deref for Removed<T> {
112    type Target = T;
113
114    fn deref(&self) -> &Self::Target {
115        self.get_pin().get_ref()
116    }
117}
118
119impl<T: Unpin> DerefMut for Removed<T> {
120    fn deref_mut(&mut self) -> &mut Self::Target {
121        self.get_pin_mut().get_mut()
122    }
123}