]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dyn-star/dispatch-on-pin-mut.rs
Properly handle `Pin<&mut dyn* Trait>` receiver in codegen
[rust.git] / src / test / ui / dyn-star / dispatch-on-pin-mut.rs
1 // build-pass
2 // edition:2021
3
4 #![feature(dyn_star)]
5 //~^ WARN the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
6
7 use std::future::Future;
8
9 async fn foo(f: dyn* Future<Output = i32>) {
10     println!("value: {}", f.await);
11 }
12
13 async fn async_main() {
14     foo(Box::pin(async { 1 })).await
15 }
16
17 // ------------------------------------------------------------------------- //
18 // Implementation Details Below...
19
20 use std::pin::Pin;
21 use std::task::*;
22
23 pub fn noop_waker() -> Waker {
24     let raw = RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE);
25
26     // SAFETY: the contracts for RawWaker and RawWakerVTable are upheld
27     unsafe { Waker::from_raw(raw) }
28 }
29
30 const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop);
31
32 unsafe fn noop_clone(_p: *const ()) -> RawWaker {
33     RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE)
34 }
35
36 unsafe fn noop(_p: *const ()) {}
37
38 fn main() {
39     let mut fut = async_main();
40
41     // Poll loop, just to test the future...
42     let waker = noop_waker();
43     let ctx = &mut Context::from_waker(&waker);
44
45     loop {
46         match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } {
47             Poll::Pending => {}
48             Poll::Ready(()) => break,
49         }
50     }
51 }