|
2 | 2 |
|
3 | 3 | use std::convert::{Infallible, TryFrom};
|
4 | 4 | use std::pin::Pin;
|
5 |
| -use std::task::{Context, Poll}; |
6 | 5 |
|
7 | 6 | use futures::prelude::*;
|
| 7 | +use send_wrapper::SendWrapper; |
8 | 8 |
|
9 | 9 | use crate::Config;
|
10 | 10 |
|
@@ -42,7 +42,7 @@ impl HttpClient for WasmClient {
|
42 | 42 | {
|
43 | 43 | let config = self.config.clone();
|
44 | 44 |
|
45 |
| - InnerFuture::new(async move { |
| 45 | + wrap_send(async move { |
46 | 46 | let req: fetch::Request = fetch::Request::new(req).await?;
|
47 | 47 | let conn = req.send();
|
48 | 48 | let mut res = if let Some(timeout) = config.timeout {
|
@@ -87,29 +87,13 @@ impl TryFrom<Config> for WasmClient {
|
87 | 87 | }
|
88 | 88 | }
|
89 | 89 |
|
90 |
| -struct InnerFuture { |
91 |
| - fut: Pin<Box<dyn Future<Output = Result<Response, Error>> + 'static>>, |
92 |
| -} |
93 |
| - |
94 |
| -impl InnerFuture { |
95 |
| - fn new<F: Future<Output = Result<Response, Error>> + 'static>(fut: F) -> Pin<Box<Self>> { |
96 |
| - Box::pin(Self { fut: Box::pin(fut) }) |
97 |
| - } |
98 |
| -} |
99 |
| - |
100 |
| -// This is safe because WASM doesn't have threads yet. Once WASM supports threads we should use a |
101 |
| -// thread to park the blocking implementation until it's been completed. |
102 |
| -unsafe impl Send for InnerFuture {} |
103 |
| - |
104 |
| -impl Future for InnerFuture { |
105 |
| - type Output = Result<Response, Error>; |
106 |
| - |
107 |
| - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
108 |
| - // This is safe because we're only using this future as a pass-through for the inner |
109 |
| - // future, in order to implement `Send`. If it's safe to poll the inner future, it's safe |
110 |
| - // to proxy it too. |
111 |
| - unsafe { Pin::new_unchecked(&mut self.fut).poll(cx) } |
112 |
| - } |
| 90 | +// This should not panic because WASM doesn't have threads yet. Once WASM supports threads |
| 91 | +// we can use a thread to park the blocking implementation until it's been completed. |
| 92 | +fn wrap_send<Fut, O>(f: Fut) -> Pin<Box<dyn Future<Output = O> + Send + Sync + 'static>> |
| 93 | +where |
| 94 | + Fut: Future<Output = O> + 'static, |
| 95 | +{ |
| 96 | + Box::pin(SendWrapper::new(f)) |
113 | 97 | }
|
114 | 98 |
|
115 | 99 | mod fetch {
|
|
0 commit comments