may_waiter

Crates.iomay_waiter
lib.rsmay_waiter
version0.1.16
sourcesrc
created_at2018-05-10 02:00:26.947623
updated_at2024-07-12 06:11:43.394625
descriptionSimple coroutine communication blocker
homepagehttps://github.com/Xudong-Huang/may_waiter.git
repositoryhttps://github.com/Xudong-Huang/may_waiter.git
max_upload_size
id64621
size35,349
Xudong Huang (Xudong-Huang)

documentation

https://docs.rs/may_waiter

README

Build Status Current Crates.io Version Document

coroutine waiter library

This library provide a map associated blocking primitive that waiting for a response produced by another coroutine

Usage

  • the map associated interface is through WaiterMap
fn test_waiter_map() {
    let req_map = Arc::new(WaiterMap::<usize, usize>::new());
    let req_map_1 = req_map.clone();

    let key = 1234;

    // one coroutine wait data send from another coroutine
    // prepare the waiter first
    let waiter = req_map.new_waiter(key);

    // trigger the rsp in another coroutine
    go!(move || req_map_1.set_rsp(&key, 100).ok());

    // this will block until the rsp was set
    let result = waiter.wait_rsp(None).unwrap();
    assert_eq!(result, 100);
}
  • the token associated interface is through TokenWaiter, which not need a map
fn test_token_waiter() {
    for j in 0..100 {
        let result = go!(move || {
            let waiter = TokenWaiter::<usize>::new();
            let waiter = Pin::new(&waiter);
            let id = waiter.id().unwrap();
            // trigger the rsp in another coroutine
            go!(move || TokenWaiter::set_rsp(id, j + 100));
            // this will block until the rsp was set
            assert_eq!(waiter.wait_rsp(None).unwrap(), j + 100);
            // after wait we can get the id again
            let id = waiter.id().unwrap();
            go!(move || TokenWaiter::set_rsp(id, j));
            waiter.wait_rsp(std::time::Duration::from_secs(2)).unwrap()
        })
        .join()
        .unwrap();

        assert_eq!(result, j);
    }
}
Commit count: 55

cargo fmt