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
176
177
178
use core::future::Future;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod event_bus;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod mqtt;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod timer;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod ws;

#[cfg(feature = "alloc")]
pub use blocking_unblocker::*;

// Keep it GAT based for now so that it builds with stable Rust
// and therefore `crate::utils::asyncify` can also build with stable Rust
pub trait Unblocker {
    type UnblockFuture<'a, F, T>: Future<Output = T> + Send
    where
        Self: 'a,
        F: Send + 'a,
        T: Send + 'a;

    fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
    where
        F: FnOnce() -> T + Send + 'a,
        T: Send + 'a;
}

impl<U> Unblocker for &U
where
    U: Unblocker,
{
    type UnblockFuture<'a, F, T>
    = U::UnblockFuture<'a, F, T> where Self: 'a, F: Send + 'a, T: Send + 'a;

    fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
    where
        F: FnOnce() -> T + Send + 'a,
        T: Send + 'a,
    {
        (*self).unblock(f)
    }
}

impl<U> Unblocker for &mut U
where
    U: Unblocker,
{
    type UnblockFuture<'a, F, T>
    = U::UnblockFuture<'a, F, T> where Self: 'a, F: Send + 'a, T: Send + 'a;

    fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
    where
        F: FnOnce() -> T + Send + 'a,
        T: Send + 'a,
    {
        (**self).unblock(f)
    }
}

pub trait AsyncWrapper<S> {
    fn new(sync: S) -> Self;
}

pub trait Asyncify {
    type AsyncWrapper<S>: AsyncWrapper<S>;

    fn into_async(self) -> Self::AsyncWrapper<Self>
    where
        Self: Sized,
    {
        Self::AsyncWrapper::new(self)
    }

    fn as_async(&mut self) -> Self::AsyncWrapper<&mut Self> {
        Self::AsyncWrapper::new(self)
    }
}

pub trait UnblockingAsyncWrapper<U, S> {
    fn new(unblocker: U, sync: S) -> Self;
}

pub trait UnblockingAsyncify {
    type AsyncWrapper<U, S>: UnblockingAsyncWrapper<U, S>;

    fn unblock_into_async<U>(self, unblocker: U) -> Self::AsyncWrapper<U, Self>
    where
        Self: Sized,
    {
        Self::AsyncWrapper::new(unblocker, self)
    }

    fn unblock_as_async<U>(&mut self, unblocker: U) -> Self::AsyncWrapper<U, &mut Self> {
        Self::AsyncWrapper::new(unblocker, self)
    }
}

#[cfg(feature = "alloc")]
mod blocking_unblocker {
    use core::future::Future;
    use core::marker::PhantomData;
    use core::task::Poll;

    extern crate alloc;

    use alloc::boxed::Box;

    #[derive(Clone)]
    pub struct BlockingUnblocker(());

    impl BlockingUnblocker {
        pub fn unblock<'a, F, T>(&'a self, f: F) -> BlockingFuture<T>
        where
            F: FnOnce() -> T + Send + 'a,
            T: Send + 'a,
        {
            BlockingFuture::new(f)
        }
    }

    impl super::Unblocker for BlockingUnblocker {
        type UnblockFuture<'a, F, T> = BlockingFuture<'a, T> where Self: 'a, F: Send + 'a, T: Send + 'a;

        fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
        where
            F: FnOnce() -> T + Send + 'a,
            T: Send + 'a,
        {
            BlockingUnblocker::unblock(self, f)
        }
    }

    pub fn blocking_unblocker() -> BlockingUnblocker {
        BlockingUnblocker(())
    }

    pub struct BlockingFuture<'a, T> {
        // TODO: Need to box or else we get rustc error:
        // "type parameter `F` is part of concrete type but not used in parameter list for the `impl Trait` type alias"
        computation: Option<Box<dyn FnOnce() -> T + Send + 'a>>,
        _result: PhantomData<fn() -> T>,
    }

    impl<'a, T> BlockingFuture<'a, T> {
        fn new<F>(computation: F) -> Self
        where
            F: FnOnce() -> T + Send + 'a,
            T: Send + 'a,
        {
            Self {
                computation: Some(Box::new(computation)),
                _result: PhantomData,
            }
        }
    }

    impl<'a, T> Future for BlockingFuture<'a, T>
    where
        T: Send + 'a,
    {
        type Output = T;

        fn poll(
            mut self: core::pin::Pin<&mut Self>,
            _cx: &mut core::task::Context<'_>,
        ) -> Poll<Self::Output> {
            let computation = self.computation.take();

            if let Some(computation) = computation {
                Poll::Ready((computation)())
            } else {
                unreachable!()
            }
        }
    }
}