smoltcp/socket/
waker.rs

1use core::task::Waker;
2
3/// Utility struct to register and wake a waker.
4#[derive(Debug)]
5pub struct WakerRegistration {
6    waker: Option<Waker>,
7}
8
9impl WakerRegistration {
10    pub const fn new() -> Self {
11        Self { waker: None }
12    }
13
14    /// Register a waker. Overwrites the previous waker, if any.
15    pub fn register(&mut self, w: &Waker) {
16        match self.waker {
17            // Optimization: If both the old and new Wakers wake the same task, we can simply
18            // keep the old waker, skipping the clone. (In most executor implementations,
19            // cloning a waker is somewhat expensive, comparable to cloning an Arc).
20            Some(ref w2) if (w2.will_wake(w)) => {}
21            // In all other cases
22            // - we have no waker registered
23            // - we have a waker registered but it's for a different task.
24            // then clone the new waker and store it
25            _ => self.waker = Some(w.clone()),
26        }
27    }
28
29    /// Wake the registered waker, if any.
30    pub fn wake(&mut self) {
31        self.waker.take().map(|w| w.wake());
32    }
33}