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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use crate::bindings::http::types::{
    Error, Fields, FutureIncomingResponse, Headers, IncomingRequest, IncomingResponse,
    IncomingStream, Method, OutgoingRequest, OutgoingResponse, OutgoingStream, ResponseOutparam,
    Scheme, StatusCode, Trailers,
};
use crate::http_impl::WasiHttpViewExt;
use crate::types::{ActiveFields, ActiveRequest, HttpRequest, TableHttpExt};
use crate::WasiHttpView;
use anyhow::{anyhow, bail, Context};
use bytes::Bytes;
use wasmtime_wasi::preview2::{bindings::poll::poll::Pollable, HostPollable, TablePollableExt};

#[async_trait::async_trait]
impl<T: WasiHttpView + WasiHttpViewExt> crate::bindings::http::types::Host for T {
    async fn drop_fields(&mut self, fields: Fields) -> wasmtime::Result<()> {
        self.table_mut()
            .delete_fields(fields)
            .context("[drop_fields] deleting fields")?;
        Ok(())
    }
    async fn new_fields(&mut self, entries: Vec<(String, String)>) -> wasmtime::Result<Fields> {
        let mut map = ActiveFields::new();
        for (key, value) in entries {
            map.insert(key, vec![value.clone().into_bytes()]);
        }

        let id = self
            .table_mut()
            .push_fields(Box::new(map))
            .context("[new_fields] pushing fields")?;
        Ok(id)
    }
    async fn fields_get(&mut self, fields: Fields, name: String) -> wasmtime::Result<Vec<Vec<u8>>> {
        let res = self
            .table_mut()
            .get_fields(fields)
            .context("[fields_get] getting fields")?
            .get(&name)
            .ok_or_else(|| anyhow!("key not found: {name}"))?
            .clone();
        Ok(res)
    }
    async fn fields_set(
        &mut self,
        fields: Fields,
        name: String,
        value: Vec<Vec<u8>>,
    ) -> wasmtime::Result<()> {
        match self.table_mut().get_fields_mut(fields) {
            Ok(m) => {
                m.insert(name, value.clone());
                Ok(())
            }
            Err(_) => bail!("fields not found"),
        }
    }
    async fn fields_delete(&mut self, fields: Fields, name: String) -> wasmtime::Result<()> {
        match self.table_mut().get_fields_mut(fields) {
            Ok(m) => m.remove(&name),
            Err(_) => None,
        };
        Ok(())
    }
    async fn fields_append(
        &mut self,
        fields: Fields,
        name: String,
        value: Vec<u8>,
    ) -> wasmtime::Result<()> {
        let m = self
            .table_mut()
            .get_fields_mut(fields)
            .context("[fields_append] getting mutable fields")?;
        match m.get_mut(&name) {
            Some(v) => v.push(value),
            None => {
                let mut vec = std::vec::Vec::new();
                vec.push(value);
                m.insert(name, vec);
            }
        };
        Ok(())
    }
    async fn fields_entries(&mut self, fields: Fields) -> wasmtime::Result<Vec<(String, Vec<u8>)>> {
        let field_map = match self.table().get_fields(fields) {
            Ok(m) => m.iter(),
            Err(_) => bail!("fields not found."),
        };
        let mut result = Vec::new();
        for (name, value) in field_map {
            result.push((name.clone(), value[0].clone()));
        }
        Ok(result)
    }
    async fn fields_clone(&mut self, fields: Fields) -> wasmtime::Result<Fields> {
        let table = self.table_mut();
        let m = table
            .get_fields(fields)
            .context("[fields_clone] getting fields")?;
        let id = table
            .push_fields(Box::new(m.clone()))
            .context("[fields_clone] pushing fields")?;
        Ok(id)
    }
    async fn finish_incoming_stream(
        &mut self,
        stream_id: IncomingStream,
    ) -> wasmtime::Result<Option<Trailers>> {
        for (_, stream) in self.http_ctx().streams.iter() {
            if stream_id == stream.incoming() {
                let response = self
                    .table()
                    .get_response(stream.parent_id())
                    .context("[finish_incoming_stream] get trailers from response")?;
                return Ok(response.trailers());
            }
        }
        bail!("unknown stream!")
    }
    async fn finish_outgoing_stream(
        &mut self,
        _s: OutgoingStream,
        _trailers: Option<Trailers>,
    ) -> wasmtime::Result<()> {
        bail!("unimplemented: finish_outgoing_stream")
    }
    async fn drop_incoming_request(&mut self, _request: IncomingRequest) -> wasmtime::Result<()> {
        bail!("unimplemented: drop_incoming_request")
    }
    async fn drop_outgoing_request(&mut self, request: OutgoingRequest) -> wasmtime::Result<()> {
        let r = self
            .table_mut()
            .get_request(request)
            .context("[drop_outgoing_request] getting fields")?;

        // Cleanup dependent resources
        let body = r.body();
        let headers = r.headers();
        if let Some(b) = body {
            self.table_mut().delete_stream(b).ok();
        }
        if let Some(h) = headers {
            self.table_mut().delete_fields(h).ok();
        }

        self.table_mut()
            .delete_request(request)
            .context("[drop_outgoing_request] deleting request")?;

        Ok(())
    }
    async fn incoming_request_method(
        &mut self,
        _request: IncomingRequest,
    ) -> wasmtime::Result<Method> {
        bail!("unimplemented: incoming_request_method")
    }
    async fn incoming_request_path_with_query(
        &mut self,
        _request: IncomingRequest,
    ) -> wasmtime::Result<Option<String>> {
        bail!("unimplemented: incoming_request_path")
    }
    async fn incoming_request_scheme(
        &mut self,
        _request: IncomingRequest,
    ) -> wasmtime::Result<Option<Scheme>> {
        bail!("unimplemented: incoming_request_scheme")
    }
    async fn incoming_request_authority(
        &mut self,
        _request: IncomingRequest,
    ) -> wasmtime::Result<Option<String>> {
        bail!("unimplemented: incoming_request_authority")
    }
    async fn incoming_request_headers(
        &mut self,
        _request: IncomingRequest,
    ) -> wasmtime::Result<Headers> {
        bail!("unimplemented: incoming_request_headers")
    }
    async fn incoming_request_consume(
        &mut self,
        _request: IncomingRequest,
    ) -> wasmtime::Result<Result<IncomingStream, ()>> {
        bail!("unimplemented: incoming_request_consume")
    }
    async fn new_outgoing_request(
        &mut self,
        method: Method,
        path_with_query: Option<String>,
        scheme: Option<Scheme>,
        authority: Option<String>,
        headers: Headers,
    ) -> wasmtime::Result<OutgoingRequest> {
        let mut req = ActiveRequest::new();
        req.path_with_query = path_with_query.unwrap_or("".to_string());
        req.authority = authority.unwrap_or("".to_string());
        req.method = method;
        req.headers = Some(headers);
        req.scheme = scheme;
        let id = self
            .table_mut()
            .push_request(Box::new(req))
            .context("[new_outgoing_request] pushing request")?;
        Ok(id)
    }
    async fn outgoing_request_write(
        &mut self,
        request: OutgoingRequest,
    ) -> wasmtime::Result<Result<OutgoingStream, ()>> {
        let req = self
            .table()
            .get_request(request)
            .context("[outgoing_request_write] getting request")?;
        let stream_id = if let Some(stream_id) = req.body() {
            stream_id
        } else {
            let (new, stream) = self
                .table_mut()
                .push_stream(Bytes::new(), request)
                .await
                .expect("[outgoing_request_write] valid output stream");
            self.http_ctx_mut().streams.insert(new, stream);
            let req = self
                .table_mut()
                .get_request_mut(request)
                .expect("[outgoing_request_write] request to be found");
            req.set_body(new);
            new
        };
        let stream = self
            .table()
            .get_stream(stream_id)
            .context("[outgoing_request_write] getting stream")?;
        Ok(Ok(stream.outgoing()))
    }
    async fn drop_response_outparam(
        &mut self,
        _response: ResponseOutparam,
    ) -> wasmtime::Result<()> {
        bail!("unimplemented: drop_response_outparam")
    }
    async fn set_response_outparam(
        &mut self,
        _outparam: ResponseOutparam,
        _response: Result<OutgoingResponse, Error>,
    ) -> wasmtime::Result<Result<(), ()>> {
        bail!("unimplemented: set_response_outparam")
    }
    async fn drop_incoming_response(&mut self, response: IncomingResponse) -> wasmtime::Result<()> {
        let r = self
            .table()
            .get_response(response)
            .context("[drop_incoming_response] getting response")?;

        // Cleanup dependent resources
        let body = r.body();
        let headers = r.headers();
        if let Some(id) = body {
            let stream = self
                .table()
                .get_stream(id)
                .context("[drop_incoming_response] getting stream")?;
            let incoming_id = stream.incoming();
            if let Some(trailers) = self.finish_incoming_stream(incoming_id).await? {
                self.table_mut()
                    .delete_fields(trailers)
                    .context("[drop_incoming_response] deleting trailers")
                    .unwrap_or_else(|_| ());
            }
            self.table_mut().delete_stream(id).ok();
        }
        if let Some(h) = headers {
            self.table_mut().delete_fields(h).ok();
        }

        self.table_mut()
            .delete_response(response)
            .context("[drop_incoming_response] deleting response")?;
        Ok(())
    }
    async fn drop_outgoing_response(
        &mut self,
        _response: OutgoingResponse,
    ) -> wasmtime::Result<()> {
        bail!("unimplemented: drop_outgoing_response")
    }
    async fn incoming_response_status(
        &mut self,
        response: IncomingResponse,
    ) -> wasmtime::Result<StatusCode> {
        let r = self
            .table()
            .get_response(response)
            .context("[incoming_response_status] getting response")?;
        Ok(r.status())
    }
    async fn incoming_response_headers(
        &mut self,
        response: IncomingResponse,
    ) -> wasmtime::Result<Headers> {
        let r = self
            .table()
            .get_response(response)
            .context("[incoming_response_headers] getting response")?;
        Ok(r.headers().unwrap_or(0 as Headers))
    }
    async fn incoming_response_consume(
        &mut self,
        response: IncomingResponse,
    ) -> wasmtime::Result<Result<IncomingStream, ()>> {
        let table = self.table_mut();
        let r = table
            .get_response(response)
            .context("[incoming_response_consume] getting response")?;
        Ok(Ok(r
            .body()
            .map(|id| {
                table
                    .get_stream(id)
                    .map(|stream| stream.incoming())
                    .expect("[incoming_response_consume] response body stream")
            })
            .unwrap_or(0 as IncomingStream)))
    }
    async fn new_outgoing_response(
        &mut self,
        _status_code: StatusCode,
        _headers: Headers,
    ) -> wasmtime::Result<OutgoingResponse> {
        bail!("unimplemented: new_outgoing_response")
    }
    async fn outgoing_response_write(
        &mut self,
        _response: OutgoingResponse,
    ) -> wasmtime::Result<Result<OutgoingStream, ()>> {
        bail!("unimplemented: outgoing_response_write")
    }
    async fn drop_future_incoming_response(
        &mut self,
        future: FutureIncomingResponse,
    ) -> wasmtime::Result<()> {
        self.table_mut()
            .delete_future(future)
            .context("[drop_future_incoming_response] deleting future")?;
        Ok(())
    }
    async fn future_incoming_response_get(
        &mut self,
        future: FutureIncomingResponse,
    ) -> wasmtime::Result<Option<Result<IncomingResponse, Error>>> {
        let f = self
            .table()
            .get_future(future)
            .context("[future_incoming_response_get] getting future")?;
        Ok(match f.pollable_id() {
            Some(_) => {
                let result = match f.response_id() {
                    Some(id) => Ok(id),
                    None => {
                        let response = self.handle_async(f.request_id(), f.options()).await;
                        match response {
                            Ok(id) => {
                                tracing::debug!(
                                    "including response id to future incoming response"
                                );
                                let future_mut = self.table_mut().get_future_mut(future)?;
                                future_mut.set_response_id(id);
                                tracing::trace!(
                                    "future incoming response details {:?}",
                                    *future_mut
                                );
                            }
                            _ => {}
                        }
                        response
                    }
                };
                Some(result)
            }
            None => None,
        })
    }
    async fn listen_to_future_incoming_response(
        &mut self,
        future: FutureIncomingResponse,
    ) -> wasmtime::Result<Pollable> {
        let f = self
            .table()
            .get_future(future)
            .context("[listen_to_future_incoming_response] getting future")?;
        Ok(match f.pollable_id() {
            Some(pollable_id) => pollable_id,
            None => {
                tracing::debug!("including pollable id to future incoming response");
                let pollable =
                    HostPollable::Closure(Box::new(|| Box::pin(futures::future::ready(Ok(())))));
                let pollable_id = self
                    .table_mut()
                    .push_host_pollable(pollable)
                    .context("[listen_to_future_incoming_response] pushing host pollable")?;
                let f = self
                    .table_mut()
                    .get_future_mut(future)
                    .context("[listen_to_future_incoming_response] getting future")?;
                f.set_pollable_id(pollable_id);
                tracing::trace!("future incoming response details {:?}", *f);
                pollable_id
            }
        })
    }
}

#[cfg(feature = "sync")]
pub mod sync {
    use crate::bindings::http::types::{
        Error as AsyncError, Host as AsyncHost, Method as AsyncMethod, Scheme as AsyncScheme,
    };
    use crate::bindings::sync::http::types::{
        Error, Fields, FutureIncomingResponse, Headers, IncomingRequest, IncomingResponse,
        IncomingStream, Method, OutgoingRequest, OutgoingResponse, OutgoingStream,
        ResponseOutparam, Scheme, StatusCode, Trailers,
    };
    use crate::http_impl::WasiHttpViewExt;
    use crate::WasiHttpView;
    use wasmtime_wasi::preview2::{bindings::poll::poll::Pollable, in_tokio};

    // same boilerplate everywhere, converting between two identical types with different
    // definition sites. one day wasmtime-wit-bindgen will make all this unnecessary
    impl From<AsyncError> for Error {
        fn from(other: AsyncError) -> Self {
            match other {
                AsyncError::InvalidUrl(v) => Self::InvalidUrl(v),
                AsyncError::ProtocolError(v) => Self::ProtocolError(v),
                AsyncError::TimeoutError(v) => Self::TimeoutError(v),
                AsyncError::UnexpectedError(v) => Self::UnexpectedError(v),
            }
        }
    }

    impl From<Error> for AsyncError {
        fn from(other: Error) -> Self {
            match other {
                Error::InvalidUrl(v) => Self::InvalidUrl(v),
                Error::ProtocolError(v) => Self::ProtocolError(v),
                Error::TimeoutError(v) => Self::TimeoutError(v),
                Error::UnexpectedError(v) => Self::UnexpectedError(v),
            }
        }
    }

    impl From<AsyncMethod> for Method {
        fn from(other: AsyncMethod) -> Self {
            match other {
                AsyncMethod::Connect => Self::Connect,
                AsyncMethod::Delete => Self::Delete,
                AsyncMethod::Get => Self::Get,
                AsyncMethod::Head => Self::Head,
                AsyncMethod::Options => Self::Options,
                AsyncMethod::Patch => Self::Patch,
                AsyncMethod::Post => Self::Post,
                AsyncMethod::Put => Self::Put,
                AsyncMethod::Trace => Self::Trace,
                AsyncMethod::Other(v) => Self::Other(v),
            }
        }
    }

    impl From<Method> for AsyncMethod {
        fn from(other: Method) -> Self {
            match other {
                Method::Connect => Self::Connect,
                Method::Delete => Self::Delete,
                Method::Get => Self::Get,
                Method::Head => Self::Head,
                Method::Options => Self::Options,
                Method::Patch => Self::Patch,
                Method::Post => Self::Post,
                Method::Put => Self::Put,
                Method::Trace => Self::Trace,
                Method::Other(v) => Self::Other(v),
            }
        }
    }

    impl From<AsyncScheme> for Scheme {
        fn from(other: AsyncScheme) -> Self {
            match other {
                AsyncScheme::Http => Self::Http,
                AsyncScheme::Https => Self::Https,
                AsyncScheme::Other(v) => Self::Other(v),
            }
        }
    }

    impl From<Scheme> for AsyncScheme {
        fn from(other: Scheme) -> Self {
            match other {
                Scheme::Http => Self::Http,
                Scheme::Https => Self::Https,
                Scheme::Other(v) => Self::Other(v),
            }
        }
    }

    impl<T: WasiHttpView + WasiHttpViewExt> crate::bindings::sync::http::types::Host for T {
        fn drop_fields(&mut self, fields: Fields) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_fields(self, fields).await })
        }
        fn new_fields(&mut self, entries: Vec<(String, String)>) -> wasmtime::Result<Fields> {
            in_tokio(async { AsyncHost::new_fields(self, entries).await })
        }
        fn fields_get(&mut self, fields: Fields, name: String) -> wasmtime::Result<Vec<Vec<u8>>> {
            in_tokio(async { AsyncHost::fields_get(self, fields, name).await })
        }
        fn fields_set(
            &mut self,
            fields: Fields,
            name: String,
            value: Vec<Vec<u8>>,
        ) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::fields_set(self, fields, name, value).await })
        }
        fn fields_delete(&mut self, fields: Fields, name: String) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::fields_delete(self, fields, name).await })
        }
        fn fields_append(
            &mut self,
            fields: Fields,
            name: String,
            value: Vec<u8>,
        ) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::fields_append(self, fields, name, value).await })
        }
        fn fields_entries(&mut self, fields: Fields) -> wasmtime::Result<Vec<(String, Vec<u8>)>> {
            in_tokio(async { AsyncHost::fields_entries(self, fields).await })
        }
        fn fields_clone(&mut self, fields: Fields) -> wasmtime::Result<Fields> {
            in_tokio(async { AsyncHost::fields_clone(self, fields).await })
        }
        fn finish_incoming_stream(
            &mut self,
            stream_id: IncomingStream,
        ) -> wasmtime::Result<Option<Trailers>> {
            in_tokio(async { AsyncHost::finish_incoming_stream(self, stream_id).await })
        }
        fn finish_outgoing_stream(
            &mut self,
            stream: OutgoingStream,
            trailers: Option<Trailers>,
        ) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::finish_outgoing_stream(self, stream, trailers).await })
        }
        fn drop_incoming_request(&mut self, request: IncomingRequest) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_incoming_request(self, request).await })
        }
        fn drop_outgoing_request(&mut self, request: OutgoingRequest) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_outgoing_request(self, request).await })
        }
        fn incoming_request_method(
            &mut self,
            request: IncomingRequest,
        ) -> wasmtime::Result<Method> {
            in_tokio(async { AsyncHost::incoming_request_method(self, request).await })
                .map(Method::from)
        }
        fn incoming_request_path_with_query(
            &mut self,
            request: IncomingRequest,
        ) -> wasmtime::Result<Option<String>> {
            in_tokio(async { AsyncHost::incoming_request_path_with_query(self, request).await })
        }
        fn incoming_request_scheme(
            &mut self,
            request: IncomingRequest,
        ) -> wasmtime::Result<Option<Scheme>> {
            Ok(
                in_tokio(async { AsyncHost::incoming_request_scheme(self, request).await })?
                    .map(Scheme::from),
            )
        }
        fn incoming_request_authority(
            &mut self,
            request: IncomingRequest,
        ) -> wasmtime::Result<Option<String>> {
            in_tokio(async { AsyncHost::incoming_request_authority(self, request).await })
        }
        fn incoming_request_headers(
            &mut self,
            request: IncomingRequest,
        ) -> wasmtime::Result<Headers> {
            in_tokio(async { AsyncHost::incoming_request_headers(self, request).await })
        }
        fn incoming_request_consume(
            &mut self,
            request: IncomingRequest,
        ) -> wasmtime::Result<Result<IncomingStream, ()>> {
            in_tokio(async { AsyncHost::incoming_request_consume(self, request).await })
        }
        fn new_outgoing_request(
            &mut self,
            method: Method,
            path_with_query: Option<String>,
            scheme: Option<Scheme>,
            authority: Option<String>,
            headers: Headers,
        ) -> wasmtime::Result<OutgoingRequest> {
            in_tokio(async {
                AsyncHost::new_outgoing_request(
                    self,
                    method.into(),
                    path_with_query,
                    scheme.map(AsyncScheme::from),
                    authority,
                    headers,
                )
                .await
            })
        }
        fn outgoing_request_write(
            &mut self,
            request: OutgoingRequest,
        ) -> wasmtime::Result<Result<OutgoingStream, ()>> {
            in_tokio(async { AsyncHost::outgoing_request_write(self, request).await })
        }
        fn drop_response_outparam(&mut self, response: ResponseOutparam) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_response_outparam(self, response).await })
        }
        fn set_response_outparam(
            &mut self,
            outparam: ResponseOutparam,
            response: Result<OutgoingResponse, Error>,
        ) -> wasmtime::Result<Result<(), ()>> {
            in_tokio(async {
                AsyncHost::set_response_outparam(self, outparam, response.map_err(AsyncError::from))
                    .await
            })
        }
        fn drop_incoming_response(&mut self, response: IncomingResponse) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_incoming_response(self, response).await })
        }
        fn drop_outgoing_response(&mut self, response: OutgoingResponse) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_outgoing_response(self, response).await })
        }
        fn incoming_response_status(
            &mut self,
            response: IncomingResponse,
        ) -> wasmtime::Result<StatusCode> {
            in_tokio(async { AsyncHost::incoming_response_status(self, response).await })
        }
        fn incoming_response_headers(
            &mut self,
            response: IncomingResponse,
        ) -> wasmtime::Result<Headers> {
            in_tokio(async { AsyncHost::incoming_response_headers(self, response).await })
        }
        fn incoming_response_consume(
            &mut self,
            response: IncomingResponse,
        ) -> wasmtime::Result<Result<IncomingStream, ()>> {
            in_tokio(async { AsyncHost::incoming_response_consume(self, response).await })
        }
        fn new_outgoing_response(
            &mut self,
            status_code: StatusCode,
            headers: Headers,
        ) -> wasmtime::Result<OutgoingResponse> {
            in_tokio(async { AsyncHost::new_outgoing_response(self, status_code, headers).await })
        }
        fn outgoing_response_write(
            &mut self,
            response: OutgoingResponse,
        ) -> wasmtime::Result<Result<OutgoingStream, ()>> {
            in_tokio(async { AsyncHost::outgoing_response_write(self, response).await })
        }
        fn drop_future_incoming_response(
            &mut self,
            future: FutureIncomingResponse,
        ) -> wasmtime::Result<()> {
            in_tokio(async { AsyncHost::drop_future_incoming_response(self, future).await })
        }
        fn future_incoming_response_get(
            &mut self,
            future: FutureIncomingResponse,
        ) -> wasmtime::Result<Option<Result<IncomingResponse, Error>>> {
            Ok(
                in_tokio(async { AsyncHost::future_incoming_response_get(self, future).await })?
                    .map(|v| v.map_err(Error::from)),
            )
        }
        fn listen_to_future_incoming_response(
            &mut self,
            future: FutureIncomingResponse,
        ) -> wasmtime::Result<Pollable> {
            in_tokio(async { AsyncHost::listen_to_future_incoming_response(self, future).await })
        }
    }
}