task-notify

Crates.iotask-notify
lib.rstask-notify
version1.0.0
sourcesrc
created_at2021-01-28 22:12:13.905343
updated_at2023-02-15 14:23:14.702172
descriptionNotify async tasks when someone mutates data they're interested in.
homepage
repository
max_upload_size
id347789
size5,724
Katharina Fey (spacekookie)

documentation

README

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
        }
    }
}
Commit count: 0

cargo fmt