| 14 | |
| 15 | impl Guest for Component { |
| 16 | async fn run() { |
| 17 | // cancel from the other end |
| 18 | let (tx, rx) = wit_future::new(|| unreachable!()); |
| 19 | let f1 = async { tx.write("hello".into()).await }; |
| 20 | let f2 = async { take_then_drop(rx) }; |
| 21 | let (result, ()) = futures::join!(f1, f2); |
| 22 | assert_eq!(result.unwrap_err().value, "hello"); |
| 23 | |
| 24 | // cancel before we actually hit the intrinsic |
| 25 | let (tx, _rx) = wit_future::new::<String>(|| String::new()); |
| 26 | let mut future = Box::pin(tx.write("hello2".into())); |
| 27 | let tx = match future.as_mut().cancel() { |
| 28 | FutureWriteCancel::Cancelled(val, tx) => { |
| 29 | assert_eq!(val, "hello2"); |
| 30 | tx |
| 31 | } |
| 32 | _ => unreachable!(), |
| 33 | }; |
| 34 | |
| 35 | // cancel after we hit the intrinsic |
| 36 | let mut future = Box::pin(tx.write("hello3".into())); |
| 37 | assert!(future |
| 38 | .as_mut() |
| 39 | .poll(&mut Context::from_waker(noop_waker_ref())) |
| 40 | .is_pending()); |
| 41 | match future.as_mut().cancel() { |
| 42 | FutureWriteCancel::Cancelled(val, _) => { |
| 43 | assert_eq!(val, "hello3"); |
| 44 | } |
| 45 | _ => unreachable!(), |
| 46 | }; |
| 47 | |
| 48 | // cancel after we hit the intrinsic and then drop the other end |
| 49 | let (tx, rx) = wit_future::new::<String>(|| unreachable!()); |
| 50 | let mut future = Box::pin(tx.write("hello3".into())); |
| 51 | assert!(future |
| 52 | .as_mut() |
| 53 | .poll(&mut Context::from_waker(noop_waker_ref())) |
| 54 | .is_pending()); |
| 55 | drop(rx); |
| 56 | match future.as_mut().cancel() { |
| 57 | FutureWriteCancel::Dropped(val) => assert_eq!(val, "hello3"), |
| 58 | other => panic!("expected dropped, got: {other:?}"), |
| 59 | }; |
| 60 | |
| 61 | // Start a write, wait for it to be pending, then go complete the write |
| 62 | // in some async work, then cancel it and witness that it was written, |
| 63 | // not cancelled. |
| 64 | let (tx, rx) = wit_future::new::<String>(|| unreachable!()); |
| 65 | let mut future = Box::pin(tx.write("hello3".into())); |
| 66 | assert!(future |
| 67 | .as_mut() |
| 68 | .poll(&mut Context::from_waker(noop_waker_ref())) |
| 69 | .is_pending()); |
| 70 | read_and_drop(rx).await; |
| 71 | match future.as_mut().cancel() { |
| 72 | FutureWriteCancel::AlreadySent => {} |
| 73 | other => panic!("expected sent, got: {other:?}"), |