|
| 1 | +// run-pass |
| 2 | +// edition:2021 |
| 3 | + |
| 4 | +#![feature(async_fn_in_trait)] |
| 5 | +//~^ WARN the feature `async_fn_in_trait` is incomplete and may not be safe to use |
| 6 | + |
| 7 | +use std::future::Future; |
| 8 | + |
| 9 | +trait AsyncTrait { |
| 10 | + async fn default_impl() { |
| 11 | + assert!(false); |
| 12 | + } |
| 13 | + |
| 14 | + async fn call_default_impl() { |
| 15 | + Self::default_impl().await |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +struct AsyncType; |
| 20 | + |
| 21 | +impl AsyncTrait for AsyncType { |
| 22 | + async fn default_impl() { |
| 23 | + // :) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +async fn async_main() { |
| 28 | + // Should not assert false |
| 29 | + AsyncType::call_default_impl().await; |
| 30 | +} |
| 31 | + |
| 32 | +// ------------------------------------------------------------------------- // |
| 33 | +// Implementation Details Below... |
| 34 | + |
| 35 | +use std::pin::Pin; |
| 36 | +use std::task::*; |
| 37 | + |
| 38 | +pub fn noop_waker() -> Waker { |
| 39 | + let raw = RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE); |
| 40 | + |
| 41 | + // SAFETY: the contracts for RawWaker and RawWakerVTable are upheld |
| 42 | + unsafe { Waker::from_raw(raw) } |
| 43 | +} |
| 44 | + |
| 45 | +const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop); |
| 46 | + |
| 47 | +unsafe fn noop_clone(_p: *const ()) -> RawWaker { |
| 48 | + RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE) |
| 49 | +} |
| 50 | + |
| 51 | +unsafe fn noop(_p: *const ()) {} |
| 52 | + |
| 53 | +fn main() { |
| 54 | + let mut fut = async_main(); |
| 55 | + |
| 56 | + // Poll loop, just to test the future... |
| 57 | + let waker = noop_waker(); |
| 58 | + let ctx = &mut Context::from_waker(&waker); |
| 59 | + |
| 60 | + loop { |
| 61 | + match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } { |
| 62 | + Poll::Pending => {} |
| 63 | + Poll::Ready(()) => break, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments