2 use std::marker::PhantomPinned;
12 fn new(delay: usize) -> Self {
17 impl Future for Delay {
19 fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
29 fn mk_waker() -> Waker {
33 impl Wake for MyWaker {
34 fn wake(self: Arc<Self>) {
39 Waker::from(Arc::new(MyWaker))
43 (&mut Delay::new(1)).await;
46 // Same thing implemented by hand
50 delay_ref: *mut Delay,
51 _marker: PhantomPinned,
59 delay_ref: ptr::null_mut(),
60 _marker: PhantomPinned,
65 impl Future for DoStuff {
67 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
69 let this = self.get_unchecked_mut();
73 this.delay_ref = &mut this.delay;
74 // Move to next state.
79 let delay = &mut *this.delay_ref;
80 Pin::new_unchecked(delay).poll(cx)
88 fn run_fut<T>(fut: impl Future<Output = T>) -> T {
89 let waker = mk_waker();
90 let mut context = Context::from_waker(&waker);
92 let mut pinned = pin!(fut);
94 match pinned.as_mut().poll(&mut context) {
95 Poll::Pending => continue,
96 Poll::Ready(v) => return v,
101 fn self_referential_box() {
102 let waker = mk_waker();
103 let cx = &mut Context::from_waker(&waker);
105 async fn my_fut() -> i32 {
109 let _ = Delay::new(1).await;
114 fn box_poll<F: Future>(
116 cx: &mut Context<'_>,
117 ) -> (Pin<Box<F>>, Poll<F::Output>) {
118 let p = f.as_mut().poll(cx);
122 let my_fut = Box::pin(my_fut());
123 let (my_fut, p1) = box_poll(my_fut, cx);
124 assert!(p1.is_pending());
125 let (my_fut, p2) = box_poll(my_fut, cx);
126 assert!(p2.is_ready());
132 run_fut(DoStuff::new());
133 self_referential_box();