task-notify 1.0.0

Notify async tasks when someone mutates data they're interested in.
Documentation
  • Coverage
  • 88.89%
    8 out of 9 items documented0 out of 8 items with examples
  • Size
  • Source code size: 5.41 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 388.96 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • spacekookie

task-notify

A wrapper which wakes tasks on mutable accesses to the wrapped value.

This can be used to transparently notify an asyncronous task that it should, for example, check for more work in a queue or try again to acquire a lock.

let c = Arc::new(Mutex::new(Notify::new(VecDeque::new())));
// task 1
async fn task_1() {
  let mut mg = c.lock().await;
  mg.push_back(42); // access via DerefMut
}
// task 2
impl Future for Task2 {
    type Output = u64;
    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let mut lock = Box::pin(t.lock());
        match Pin::new(&mut lock).poll(cx) {
            Poll::Ready(ref mut mg) => match mg.pop_front() {
                Some(v) => Poll::Ready(v),
                None => {
                    // Insert a Waker when we can't make progress for the first time!
                    Notify::add_waker(mg, cx.waker().clone());
                    Poll::Pending
                }
            },
            p => p, // The Mutex will wake us if we can make progress
        }
    }
}