waker.rs 1.1 KB

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