summaryrefslogtreecommitdiffstats
path: root/tests/ui/async-await/auxiliary/issue-72470-lib.rs
blob: 8383eba89124a50b934cb3d3e0556e5b8461c96d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// compile-flags: -C opt-level=3
// edition:2018

use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use std::task::Poll::{Pending, Ready};
use std::task::Waker;
use std::task::{Context, Poll};
use std::{
    ptr,
    task::{RawWaker, RawWakerVTable},
};

/// Future for the [`poll_fn`] function.
pub struct PollFn<F> {
    f: F,
}

impl<F> Unpin for PollFn<F> {}

/// Creates a new future wrapping around a function returning [`Poll`].
pub fn poll_fn<T, F>(f: F) -> PollFn<F>
where
    F: FnMut(&mut Context<'_>) -> Poll<T>,
{
    PollFn { f }
}

impl<T, F> Future for PollFn<F>
where
    F: FnMut(&mut Context<'_>) -> Poll<T>,
{
    type Output = T;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
        (&mut self.f)(cx)
    }
}
pub fn run<F: Future>(future: F) -> F::Output {
    BasicScheduler.block_on(future)
}

pub(crate) struct BasicScheduler;

impl BasicScheduler {
    pub(crate) fn block_on<F>(&mut self, mut future: F) -> F::Output
    where
        F: Future,
    {
        let waker = unsafe { Waker::from_raw(raw_waker()) };
        let mut cx = std::task::Context::from_waker(&waker);

        let mut future = unsafe { Pin::new_unchecked(&mut future) };

        loop {
            if let Ready(v) = future.as_mut().poll(&mut cx) {
                return v;
            }
        }
    }
}

// ===== impl Spawner =====

fn raw_waker() -> RawWaker {
    RawWaker::new(ptr::null(), waker_vtable())
}

fn waker_vtable() -> &'static RawWakerVTable {
    &RawWakerVTable::new(
        clone_arc_raw,
        wake_arc_raw,
        wake_by_ref_arc_raw,
        drop_arc_raw,
    )
}

unsafe fn clone_arc_raw(_: *const ()) -> RawWaker {
    raw_waker()
}

unsafe fn wake_arc_raw(_: *const ()) {}

unsafe fn wake_by_ref_arc_raw(_: *const ()) {}

unsafe fn drop_arc_raw(_: *const ()) {}

struct AtomicWaker {}

impl AtomicWaker {
    /// Create an `AtomicWaker`
    fn new() -> AtomicWaker {
        AtomicWaker {}
    }

    fn register_by_ref(&self, _waker: &Waker) {}
}

#[allow(dead_code)]
struct Tx<T> {
    inner: Arc<Chan<T>>,
}

struct Rx<T> {
    inner: Arc<Chan<T>>,
}

#[allow(dead_code)]
struct Chan<T> {
    tx: PhantomData<T>,
    semaphore: Sema,
    rx_waker: AtomicWaker,
    rx_closed: bool,
}

fn channel<T>() -> (Tx<T>, Rx<T>) {
    let chan = Arc::new(Chan {
        tx: PhantomData,
        semaphore: Sema(AtomicUsize::new(0)),
        rx_waker: AtomicWaker::new(),
        rx_closed: false,
    });

    (
        Tx {
            inner: chan.clone(),
        },
        Rx { inner: chan },
    )
}

// ===== impl Rx =====

impl<T> Rx<T> {
    /// Receive the next value
    fn recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
        self.inner.rx_waker.register_by_ref(cx.waker());

        if self.inner.rx_closed && self.inner.semaphore.is_idle() {
            Ready(None)
        } else {
            Pending
        }
    }
}

struct Sema(AtomicUsize);

impl Sema {
    fn is_idle(&self) -> bool {
        false
    }
}

pub struct UnboundedReceiver<T> {
    chan: Rx<T>,
}

pub fn unbounded_channel<T>() -> UnboundedReceiver<T> {
    let (tx, rx) = channel();

    drop(tx);
    let rx = UnboundedReceiver { chan: rx };

    rx
}

impl<T> UnboundedReceiver<T> {
    pub async fn recv(&mut self) -> Option<T> {
        poll_fn(|cx| self.chan.recv(cx)).await
    }
}