spin_factor_wasi/
wasi_2023_11_10.rs

1use super::wasi_2023_10_18::{convert, convert_result};
2use spin_factors::anyhow::{self, Result};
3use wasmtime::component::{Linker, Resource, ResourceTable};
4use wasmtime_wasi::cli::{WasiCli, WasiCliCtxView};
5use wasmtime_wasi::clocks::{WasiClocks, WasiClocksCtxView};
6use wasmtime_wasi::filesystem::{WasiFilesystem, WasiFilesystemCtxView};
7use wasmtime_wasi::random::{WasiRandom, WasiRandomCtx};
8use wasmtime_wasi::sockets::{WasiSockets, WasiSocketsCtxView};
9
10mod latest {
11    pub use wasmtime_wasi::p2::bindings::*;
12}
13
14mod bindings {
15    use super::latest;
16
17    wasmtime::component::bindgen!({
18        path: "../../wit",
19        world: "wasi:cli/reactor@0.2.0-rc-2023-11-10",
20        imports: {
21            "wasi:io/streams/[drop]input-stream": async | trappable,
22            "wasi:io/streams/[drop]output-stream": async | trappable,
23            "wasi:filesystem/types/[method]descriptor.advise": async | trappable,
24            "wasi:filesystem/types/[method]descriptor.create-directory-at": async | trappable,
25            "wasi:filesystem/types/[method]descriptor.get-flags": async | trappable,
26            "wasi:filesystem/types/[method]descriptor.get-type": async | trappable,
27            "wasi:filesystem/types/[method]descriptor.is-same-object": async | trappable,
28            "wasi:filesystem/types/[method]descriptor.link-at": async | trappable,
29            "wasi:filesystem/types/[method]descriptor.metadata-hash": async | trappable,
30            "wasi:filesystem/types/[method]descriptor.metadata-hash-at": async | trappable,
31            "wasi:filesystem/types/[method]descriptor.open-at": async | trappable,
32            "wasi:filesystem/types/[method]descriptor.read": async | trappable,
33            "wasi:filesystem/types/[method]descriptor.read-directory": async | trappable,
34            "wasi:filesystem/types/[method]descriptor.readlink-at": async | trappable,
35            "wasi:filesystem/types/[method]descriptor.remove-directory-at": async | trappable,
36            "wasi:filesystem/types/[method]descriptor.rename-at": async | trappable,
37            "wasi:filesystem/types/[method]descriptor.set-size": async | trappable,
38            "wasi:filesystem/types/[method]descriptor.set-times": async | trappable,
39            "wasi:filesystem/types/[method]descriptor.set-times-at": async | trappable,
40            "wasi:filesystem/types/[method]descriptor.stat": async | trappable,
41            "wasi:filesystem/types/[method]descriptor.stat-at": async | trappable,
42            "wasi:filesystem/types/[method]descriptor.symlink-at": async | trappable,
43            "wasi:filesystem/types/[method]descriptor.sync": async | trappable,
44            "wasi:filesystem/types/[method]descriptor.sync-data": async | trappable,
45            "wasi:filesystem/types/[method]descriptor.unlink-file-at": async | trappable,
46            "wasi:filesystem/types/[method]descriptor.write": async | trappable,
47            "wasi:io/streams/[method]input-stream.blocking-read": async | trappable,
48            "wasi:io/streams/[method]input-stream.blocking-skip": async | trappable,
49            "wasi:io/streams/[method]output-stream.blocking-splice": async | trappable,
50            "wasi:io/streams/[method]output-stream.blocking-flush": async | trappable,
51            "wasi:io/streams/[method]output-stream.blocking-write-and-flush": async | trappable,
52            "wasi:io/streams/[method]output-stream.blocking-write-zeroes-and-flush": async | trappable,
53            "wasi:filesystem/types/[method]directory-entry-stream.read-directory-entry": async | trappable,
54            "wasi:io/poll/[method]pollable.block": async | trappable,
55            "wasi:io/poll/[method]pollable.ready": async | trappable,
56            "wasi:io/poll/poll": async | trappable,
57
58            "wasi:sockets/tcp/[method]tcp-socket.start-bind": async | trappable,
59            "wasi:sockets/tcp/[method]tcp-socket.start-connect": async | trappable,
60            "wasi:sockets/udp/[method]udp-socket.start-bind": async | trappable,
61            "wasi:sockets/udp/[method]udp-socket.stream": async | trappable,
62            "wasi:sockets/udp/[method]outgoing-datagram-stream.send": async | trappable,
63            default: trappable,
64        },
65        with: {
66            "wasi:io/poll/pollable": latest::io::poll::Pollable,
67            "wasi:io/streams/input-stream": latest::io::streams::InputStream,
68            "wasi:io/streams/output-stream": latest::io::streams::OutputStream,
69            "wasi:io/error/error": latest::io::error::Error,
70            "wasi:filesystem/types/directory-entry-stream": latest::filesystem::types::DirectoryEntryStream,
71            "wasi:filesystem/types/descriptor": latest::filesystem::types::Descriptor,
72            "wasi:cli/terminal-input/terminal-input": latest::cli::terminal_input::TerminalInput,
73            "wasi:cli/terminal-output/terminal-output": latest::cli::terminal_output::TerminalOutput,
74            "wasi:sockets/tcp/tcp-socket": latest::sockets::tcp::TcpSocket,
75            "wasi:sockets/udp/udp-socket": latest::sockets::udp::UdpSocket,
76            "wasi:sockets/udp/outgoing-datagram-stream": latest::sockets::udp::OutgoingDatagramStream,
77            "wasi:sockets/udp/incoming-datagram-stream": latest::sockets::udp::IncomingDatagramStream,
78            "wasi:sockets/network/network": latest::sockets::network::Network,
79            "wasi:sockets/ip-name-lookup/resolve-address-stream": latest::sockets::ip_name_lookup::ResolveAddressStream,
80        },
81    });
82}
83
84mod wasi {
85    pub use super::bindings::wasi::{
86        cli0_2_0_rc_2023_11_10 as cli, clocks0_2_0_rc_2023_11_10 as clocks,
87        filesystem0_2_0_rc_2023_11_10 as filesystem, io0_2_0_rc_2023_11_10 as io,
88        random0_2_0_rc_2023_11_10 as random, sockets0_2_0_rc_2023_11_10 as sockets,
89    };
90}
91
92use wasi::cli::terminal_input::TerminalInput;
93use wasi::cli::terminal_output::TerminalOutput;
94use wasi::clocks::monotonic_clock::{Duration, Instant};
95use wasi::clocks::wall_clock::Datetime;
96use wasi::filesystem::types::{
97    Advice, Descriptor, DescriptorFlags, DescriptorStat, DescriptorType, DirectoryEntry,
98    DirectoryEntryStream, ErrorCode as FsErrorCode, Filesize, MetadataHashValue, NewTimestamp,
99    OpenFlags, PathFlags,
100};
101use wasi::io::poll::Pollable;
102use wasi::io::streams::{Error as IoError, InputStream, OutputStream, StreamError};
103use wasi::sockets::ip_name_lookup::{IpAddress, ResolveAddressStream};
104use wasi::sockets::network::{Ipv4SocketAddress, Ipv6SocketAddress};
105use wasi::sockets::tcp::{
106    ErrorCode as SocketErrorCode, IpAddressFamily, IpSocketAddress, Network, ShutdownType,
107    TcpSocket,
108};
109use wasi::sockets::udp::{
110    IncomingDatagram, IncomingDatagramStream, OutgoingDatagram, OutgoingDatagramStream, UdpSocket,
111};
112
113use crate::HasIo;
114
115pub fn add_to_linker<T>(
116    linker: &mut Linker<T>,
117    io_closure: fn(&mut T) -> &mut ResourceTable,
118    random_closure: fn(&mut T) -> &mut WasiRandomCtx,
119    clocks_closure: fn(&mut T) -> WasiClocksCtxView<'_>,
120    cli_closure: fn(&mut T) -> WasiCliCtxView<'_>,
121    filesystem_closure: fn(&mut T) -> WasiFilesystemCtxView<'_>,
122    sockets_closure: fn(&mut T) -> WasiSocketsCtxView<'_>,
123) -> Result<()>
124where
125    T: Send + 'static,
126{
127    wasi::clocks::monotonic_clock::add_to_linker::<_, WasiClocks>(linker, clocks_closure)?;
128    wasi::clocks::wall_clock::add_to_linker::<_, WasiClocks>(linker, clocks_closure)?;
129    wasi::filesystem::types::add_to_linker::<_, WasiFilesystem>(linker, filesystem_closure)?;
130    wasi::filesystem::preopens::add_to_linker::<_, WasiFilesystem>(linker, filesystem_closure)?;
131    wasi::io::error::add_to_linker::<_, HasIo>(linker, io_closure)?;
132    wasi::io::poll::add_to_linker::<_, HasIo>(linker, io_closure)?;
133    wasi::io::streams::add_to_linker::<_, HasIo>(linker, io_closure)?;
134    wasi::random::random::add_to_linker::<_, WasiRandom>(linker, random_closure)?;
135    wasi::random::insecure::add_to_linker::<_, WasiRandom>(linker, random_closure)?;
136    wasi::random::insecure_seed::add_to_linker::<_, WasiRandom>(linker, random_closure)?;
137    wasi::cli::exit::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
138    wasi::cli::environment::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
139    wasi::cli::stdin::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
140    wasi::cli::stdout::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
141    wasi::cli::stderr::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
142    wasi::cli::terminal_input::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
143    wasi::cli::terminal_output::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
144    wasi::cli::terminal_stdin::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
145    wasi::cli::terminal_stdout::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
146    wasi::cli::terminal_stderr::add_to_linker::<_, WasiCli>(linker, cli_closure)?;
147    wasi::sockets::tcp::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
148    wasi::sockets::tcp_create_socket::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
149    wasi::sockets::udp::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
150    wasi::sockets::udp_create_socket::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
151    wasi::sockets::instance_network::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
152    wasi::sockets::network::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
153    wasi::sockets::ip_name_lookup::add_to_linker::<_, WasiSockets>(linker, sockets_closure)?;
154    Ok(())
155}
156
157impl wasi::clocks::monotonic_clock::Host for WasiClocksCtxView<'_> {
158    fn now(&mut self) -> wasmtime::Result<Instant> {
159        latest::clocks::monotonic_clock::Host::now(self)
160    }
161
162    fn resolution(&mut self) -> wasmtime::Result<Instant> {
163        latest::clocks::monotonic_clock::Host::resolution(self)
164    }
165
166    fn subscribe_instant(&mut self, when: Instant) -> wasmtime::Result<Resource<Pollable>> {
167        latest::clocks::monotonic_clock::Host::subscribe_instant(self, when)
168    }
169
170    fn subscribe_duration(&mut self, when: Duration) -> wasmtime::Result<Resource<Pollable>> {
171        latest::clocks::monotonic_clock::Host::subscribe_duration(self, when)
172    }
173}
174
175impl wasi::clocks::wall_clock::Host for WasiClocksCtxView<'_> {
176    fn now(&mut self) -> wasmtime::Result<Datetime> {
177        Ok(latest::clocks::wall_clock::Host::now(self)?.into())
178    }
179
180    fn resolution(&mut self) -> wasmtime::Result<Datetime> {
181        Ok(latest::clocks::wall_clock::Host::resolution(self)?.into())
182    }
183}
184
185impl wasi::filesystem::types::Host for WasiFilesystemCtxView<'_> {
186    fn filesystem_error_code(
187        &mut self,
188        err: Resource<wasi::filesystem::types::Error>,
189    ) -> wasmtime::Result<Option<FsErrorCode>> {
190        Ok(latest::filesystem::types::Host::filesystem_error_code(self, err)?.map(|e| e.into()))
191    }
192}
193
194impl wasi::filesystem::types::HostDescriptor for WasiFilesystemCtxView<'_> {
195    fn read_via_stream(
196        &mut self,
197        self_: Resource<Descriptor>,
198        offset: Filesize,
199    ) -> wasmtime::Result<Result<Resource<InputStream>, FsErrorCode>> {
200        convert_result(latest::filesystem::types::HostDescriptor::read_via_stream(
201            self, self_, offset,
202        ))
203    }
204
205    fn write_via_stream(
206        &mut self,
207        self_: Resource<Descriptor>,
208        offset: Filesize,
209    ) -> wasmtime::Result<Result<Resource<OutputStream>, FsErrorCode>> {
210        convert_result(latest::filesystem::types::HostDescriptor::write_via_stream(
211            self, self_, offset,
212        ))
213    }
214
215    fn append_via_stream(
216        &mut self,
217        self_: Resource<Descriptor>,
218    ) -> wasmtime::Result<Result<Resource<OutputStream>, FsErrorCode>> {
219        convert_result(latest::filesystem::types::HostDescriptor::append_via_stream(self, self_))
220    }
221
222    async fn advise(
223        &mut self,
224        self_: Resource<Descriptor>,
225        offset: Filesize,
226        length: Filesize,
227        advice: Advice,
228    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
229        convert_result(
230            latest::filesystem::types::HostDescriptor::advise(
231                self,
232                self_,
233                offset,
234                length,
235                advice.into(),
236            )
237            .await,
238        )
239    }
240
241    async fn sync_data(
242        &mut self,
243        self_: Resource<Descriptor>,
244    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
245        convert_result(latest::filesystem::types::HostDescriptor::sync_data(self, self_).await)
246    }
247
248    async fn get_flags(
249        &mut self,
250        self_: Resource<Descriptor>,
251    ) -> wasmtime::Result<Result<DescriptorFlags, FsErrorCode>> {
252        convert_result(latest::filesystem::types::HostDescriptor::get_flags(self, self_).await)
253    }
254
255    async fn get_type(
256        &mut self,
257        self_: Resource<Descriptor>,
258    ) -> wasmtime::Result<Result<DescriptorType, FsErrorCode>> {
259        convert_result(latest::filesystem::types::HostDescriptor::get_type(self, self_).await)
260    }
261
262    async fn set_size(
263        &mut self,
264        self_: Resource<Descriptor>,
265        size: Filesize,
266    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
267        convert_result(latest::filesystem::types::HostDescriptor::set_size(self, self_, size).await)
268    }
269
270    async fn set_times(
271        &mut self,
272        self_: Resource<Descriptor>,
273        data_access_timestamp: NewTimestamp,
274        data_modification_timestamp: NewTimestamp,
275    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
276        convert_result(
277            latest::filesystem::types::HostDescriptor::set_times(
278                self,
279                self_,
280                data_access_timestamp.into(),
281                data_modification_timestamp.into(),
282            )
283            .await,
284        )
285    }
286
287    async fn read(
288        &mut self,
289        self_: Resource<Descriptor>,
290        length: Filesize,
291        offset: Filesize,
292    ) -> wasmtime::Result<Result<(Vec<u8>, bool), FsErrorCode>> {
293        convert_result(
294            latest::filesystem::types::HostDescriptor::read(self, self_, length, offset).await,
295        )
296    }
297
298    async fn write(
299        &mut self,
300        self_: Resource<Descriptor>,
301        buffer: Vec<u8>,
302        offset: Filesize,
303    ) -> wasmtime::Result<Result<Filesize, FsErrorCode>> {
304        convert_result(
305            latest::filesystem::types::HostDescriptor::write(self, self_, buffer, offset).await,
306        )
307    }
308
309    async fn read_directory(
310        &mut self,
311        self_: Resource<Descriptor>,
312    ) -> wasmtime::Result<Result<Resource<DirectoryEntryStream>, FsErrorCode>> {
313        convert_result(latest::filesystem::types::HostDescriptor::read_directory(self, self_).await)
314    }
315
316    async fn sync(
317        &mut self,
318        self_: Resource<Descriptor>,
319    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
320        convert_result(latest::filesystem::types::HostDescriptor::sync(self, self_).await)
321    }
322
323    async fn create_directory_at(
324        &mut self,
325        self_: Resource<Descriptor>,
326        path: String,
327    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
328        convert_result(
329            latest::filesystem::types::HostDescriptor::create_directory_at(self, self_, path).await,
330        )
331    }
332
333    async fn stat(
334        &mut self,
335        self_: Resource<Descriptor>,
336    ) -> wasmtime::Result<Result<DescriptorStat, FsErrorCode>> {
337        convert_result(latest::filesystem::types::HostDescriptor::stat(self, self_).await)
338    }
339
340    async fn stat_at(
341        &mut self,
342        self_: Resource<Descriptor>,
343        path_flags: PathFlags,
344        path: String,
345    ) -> wasmtime::Result<Result<DescriptorStat, FsErrorCode>> {
346        convert_result(
347            latest::filesystem::types::HostDescriptor::stat_at(
348                self,
349                self_,
350                path_flags.into(),
351                path,
352            )
353            .await,
354        )
355    }
356
357    async fn set_times_at(
358        &mut self,
359        self_: Resource<Descriptor>,
360        path_flags: PathFlags,
361        path: String,
362        data_access_timestamp: NewTimestamp,
363        data_modification_timestamp: NewTimestamp,
364    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
365        convert_result(
366            latest::filesystem::types::HostDescriptor::set_times_at(
367                self,
368                self_,
369                path_flags.into(),
370                path,
371                data_access_timestamp.into(),
372                data_modification_timestamp.into(),
373            )
374            .await,
375        )
376    }
377
378    async fn link_at(
379        &mut self,
380        self_: Resource<Descriptor>,
381        old_path_flags: PathFlags,
382        old_path: String,
383        new_descriptor: Resource<Descriptor>,
384        new_path: String,
385    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
386        convert_result(
387            latest::filesystem::types::HostDescriptor::link_at(
388                self,
389                self_,
390                old_path_flags.into(),
391                old_path,
392                new_descriptor,
393                new_path,
394            )
395            .await,
396        )
397    }
398
399    async fn open_at(
400        &mut self,
401        self_: Resource<Descriptor>,
402        path_flags: PathFlags,
403        path: String,
404        open_flags: OpenFlags,
405        flags: DescriptorFlags,
406    ) -> wasmtime::Result<Result<Resource<Descriptor>, FsErrorCode>> {
407        convert_result(
408            latest::filesystem::types::HostDescriptor::open_at(
409                self,
410                self_,
411                path_flags.into(),
412                path,
413                open_flags.into(),
414                flags.into(),
415            )
416            .await,
417        )
418    }
419
420    async fn readlink_at(
421        &mut self,
422        self_: Resource<Descriptor>,
423        path: String,
424    ) -> wasmtime::Result<Result<String, FsErrorCode>> {
425        convert_result(
426            latest::filesystem::types::HostDescriptor::readlink_at(self, self_, path).await,
427        )
428    }
429
430    async fn remove_directory_at(
431        &mut self,
432        self_: Resource<Descriptor>,
433        path: String,
434    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
435        convert_result(
436            latest::filesystem::types::HostDescriptor::remove_directory_at(self, self_, path).await,
437        )
438    }
439
440    async fn rename_at(
441        &mut self,
442        self_: Resource<Descriptor>,
443        old_path: String,
444        new_descriptor: Resource<Descriptor>,
445        new_path: String,
446    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
447        convert_result(
448            latest::filesystem::types::HostDescriptor::rename_at(
449                self,
450                self_,
451                old_path,
452                new_descriptor,
453                new_path,
454            )
455            .await,
456        )
457    }
458
459    async fn symlink_at(
460        &mut self,
461        self_: Resource<Descriptor>,
462        old_path: String,
463        new_path: String,
464    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
465        convert_result(
466            latest::filesystem::types::HostDescriptor::symlink_at(self, self_, old_path, new_path)
467                .await,
468        )
469    }
470
471    async fn unlink_file_at(
472        &mut self,
473        self_: Resource<Descriptor>,
474        path: String,
475    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
476        convert_result(
477            latest::filesystem::types::HostDescriptor::unlink_file_at(self, self_, path).await,
478        )
479    }
480
481    async fn is_same_object(
482        &mut self,
483        self_: Resource<Descriptor>,
484        other: Resource<Descriptor>,
485    ) -> wasmtime::Result<bool> {
486        latest::filesystem::types::HostDescriptor::is_same_object(self, self_, other).await
487    }
488
489    async fn metadata_hash(
490        &mut self,
491        self_: Resource<Descriptor>,
492    ) -> wasmtime::Result<Result<MetadataHashValue, FsErrorCode>> {
493        convert_result(latest::filesystem::types::HostDescriptor::metadata_hash(self, self_).await)
494    }
495
496    async fn metadata_hash_at(
497        &mut self,
498        self_: Resource<Descriptor>,
499        path_flags: PathFlags,
500        path: String,
501    ) -> wasmtime::Result<Result<MetadataHashValue, FsErrorCode>> {
502        convert_result(
503            latest::filesystem::types::HostDescriptor::metadata_hash_at(
504                self,
505                self_,
506                path_flags.into(),
507                path,
508            )
509            .await,
510        )
511    }
512
513    fn drop(&mut self, rep: Resource<Descriptor>) -> wasmtime::Result<()> {
514        latest::filesystem::types::HostDescriptor::drop(self, rep)
515    }
516}
517
518impl wasi::filesystem::types::HostDirectoryEntryStream for WasiFilesystemCtxView<'_> {
519    async fn read_directory_entry(
520        &mut self,
521        self_: Resource<DirectoryEntryStream>,
522    ) -> wasmtime::Result<Result<Option<DirectoryEntry>, FsErrorCode>> {
523        convert_result(
524            latest::filesystem::types::HostDirectoryEntryStream::read_directory_entry(self, self_)
525                .await
526                .map(|e| e.map(DirectoryEntry::from)),
527        )
528    }
529
530    fn drop(&mut self, rep: Resource<DirectoryEntryStream>) -> wasmtime::Result<()> {
531        latest::filesystem::types::HostDirectoryEntryStream::drop(self, rep)
532    }
533}
534
535impl wasi::filesystem::preopens::Host for WasiFilesystemCtxView<'_> {
536    fn get_directories(&mut self) -> wasmtime::Result<Vec<(Resource<Descriptor>, String)>> {
537        latest::filesystem::preopens::Host::get_directories(self)
538    }
539}
540
541impl wasi::io::poll::Host for ResourceTable {
542    async fn poll(&mut self, list: Vec<Resource<Pollable>>) -> wasmtime::Result<Vec<u32>> {
543        latest::io::poll::Host::poll(self, list).await
544    }
545}
546
547impl wasi::io::poll::HostPollable for ResourceTable {
548    async fn block(&mut self, rep: Resource<Pollable>) -> wasmtime::Result<()> {
549        latest::io::poll::HostPollable::block(self, rep).await
550    }
551
552    async fn ready(&mut self, rep: Resource<Pollable>) -> wasmtime::Result<bool> {
553        latest::io::poll::HostPollable::ready(self, rep).await
554    }
555
556    fn drop(&mut self, rep: Resource<Pollable>) -> wasmtime::Result<()> {
557        latest::io::poll::HostPollable::drop(self, rep)
558    }
559}
560
561impl wasi::io::error::Host for ResourceTable {}
562
563impl wasi::io::error::HostError for ResourceTable {
564    fn to_debug_string(&mut self, self_: Resource<IoError>) -> wasmtime::Result<String> {
565        latest::io::error::HostError::to_debug_string(self, self_)
566    }
567
568    fn drop(&mut self, rep: Resource<IoError>) -> wasmtime::Result<()> {
569        latest::io::error::HostError::drop(self, rep)
570    }
571}
572
573fn convert_stream_result<T, T2>(
574    table: &mut ResourceTable,
575    result: Result<T, wasmtime_wasi::p2::StreamError>,
576) -> wasmtime::Result<Result<T2, StreamError>>
577where
578    T2: From<T>,
579{
580    match result {
581        Ok(e) => Ok(Ok(e.into())),
582        Err(wasmtime_wasi::p2::StreamError::Closed) => Ok(Err(StreamError::Closed)),
583        Err(wasmtime_wasi::p2::StreamError::LastOperationFailed(e)) => {
584            let e = table.push(e)?;
585            Ok(Err(StreamError::LastOperationFailed(e)))
586        }
587        Err(wasmtime_wasi::p2::StreamError::Trap(e)) => Err(e),
588    }
589}
590
591impl wasi::io::streams::Host for ResourceTable {}
592
593impl wasi::io::streams::HostInputStream for ResourceTable {
594    fn read(
595        &mut self,
596        self_: Resource<InputStream>,
597        len: u64,
598    ) -> wasmtime::Result<Result<Vec<u8>, StreamError>> {
599        let result = latest::io::streams::HostInputStream::read(self, self_, len);
600        convert_stream_result(self, result)
601    }
602
603    async fn blocking_read(
604        &mut self,
605        self_: Resource<InputStream>,
606        len: u64,
607    ) -> wasmtime::Result<Result<Vec<u8>, StreamError>> {
608        let result = latest::io::streams::HostInputStream::blocking_read(self, self_, len).await;
609        convert_stream_result(self, result)
610    }
611
612    fn skip(
613        &mut self,
614        self_: Resource<InputStream>,
615        len: u64,
616    ) -> wasmtime::Result<Result<u64, StreamError>> {
617        let result = latest::io::streams::HostInputStream::skip(self, self_, len);
618        convert_stream_result(self, result)
619    }
620
621    async fn blocking_skip(
622        &mut self,
623        self_: Resource<InputStream>,
624        len: u64,
625    ) -> wasmtime::Result<Result<u64, StreamError>> {
626        let result = latest::io::streams::HostInputStream::blocking_skip(self, self_, len).await;
627        convert_stream_result(self, result)
628    }
629
630    fn subscribe(&mut self, self_: Resource<InputStream>) -> wasmtime::Result<Resource<Pollable>> {
631        latest::io::streams::HostInputStream::subscribe(self, self_)
632    }
633
634    async fn drop(&mut self, rep: Resource<InputStream>) -> wasmtime::Result<()> {
635        latest::io::streams::HostInputStream::drop(self, rep).await
636    }
637}
638
639impl wasi::io::streams::HostOutputStream for ResourceTable {
640    fn check_write(
641        &mut self,
642        self_: Resource<OutputStream>,
643    ) -> wasmtime::Result<Result<u64, StreamError>> {
644        let result = latest::io::streams::HostOutputStream::check_write(self, self_);
645        convert_stream_result(self, result)
646    }
647
648    fn write(
649        &mut self,
650        self_: Resource<OutputStream>,
651        contents: Vec<u8>,
652    ) -> wasmtime::Result<Result<(), StreamError>> {
653        let result = latest::io::streams::HostOutputStream::write(self, self_, contents);
654        convert_stream_result(self, result)
655    }
656
657    async fn blocking_write_and_flush(
658        &mut self,
659        self_: Resource<OutputStream>,
660        contents: Vec<u8>,
661    ) -> wasmtime::Result<Result<(), StreamError>> {
662        let result =
663            latest::io::streams::HostOutputStream::blocking_write_and_flush(self, self_, contents)
664                .await;
665        convert_stream_result(self, result)
666    }
667
668    fn flush(
669        &mut self,
670        self_: Resource<OutputStream>,
671    ) -> wasmtime::Result<Result<(), StreamError>> {
672        let result = latest::io::streams::HostOutputStream::flush(self, self_);
673        convert_stream_result(self, result)
674    }
675
676    async fn blocking_flush(
677        &mut self,
678        self_: Resource<OutputStream>,
679    ) -> wasmtime::Result<Result<(), StreamError>> {
680        let result = latest::io::streams::HostOutputStream::blocking_flush(self, self_).await;
681        convert_stream_result(self, result)
682    }
683
684    fn subscribe(&mut self, self_: Resource<OutputStream>) -> wasmtime::Result<Resource<Pollable>> {
685        latest::io::streams::HostOutputStream::subscribe(self, self_)
686    }
687
688    fn write_zeroes(
689        &mut self,
690        self_: Resource<OutputStream>,
691        len: u64,
692    ) -> wasmtime::Result<Result<(), StreamError>> {
693        let result = latest::io::streams::HostOutputStream::write_zeroes(self, self_, len);
694        convert_stream_result(self, result)
695    }
696
697    async fn blocking_write_zeroes_and_flush(
698        &mut self,
699        self_: Resource<OutputStream>,
700        len: u64,
701    ) -> wasmtime::Result<Result<(), StreamError>> {
702        let result = latest::io::streams::HostOutputStream::blocking_write_zeroes_and_flush(
703            self, self_, len,
704        )
705        .await;
706        convert_stream_result(self, result)
707    }
708
709    fn splice(
710        &mut self,
711        self_: Resource<OutputStream>,
712        src: Resource<InputStream>,
713        len: u64,
714    ) -> wasmtime::Result<Result<u64, StreamError>> {
715        let result = latest::io::streams::HostOutputStream::splice(self, self_, src, len);
716        convert_stream_result(self, result)
717    }
718
719    async fn blocking_splice(
720        &mut self,
721        self_: Resource<OutputStream>,
722        src: Resource<InputStream>,
723        len: u64,
724    ) -> wasmtime::Result<Result<u64, StreamError>> {
725        let result =
726            latest::io::streams::HostOutputStream::blocking_splice(self, self_, src, len).await;
727        convert_stream_result(self, result)
728    }
729
730    async fn drop(&mut self, rep: Resource<OutputStream>) -> wasmtime::Result<()> {
731        latest::io::streams::HostOutputStream::drop(self, rep).await
732    }
733}
734
735impl wasi::random::random::Host for WasiRandomCtx {
736    fn get_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
737        latest::random::random::Host::get_random_bytes(self, len)
738    }
739
740    fn get_random_u64(&mut self) -> wasmtime::Result<u64> {
741        latest::random::random::Host::get_random_u64(self)
742    }
743}
744
745impl wasi::random::insecure::Host for WasiRandomCtx {
746    fn get_insecure_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
747        latest::random::insecure::Host::get_insecure_random_bytes(self, len)
748    }
749
750    fn get_insecure_random_u64(&mut self) -> wasmtime::Result<u64> {
751        latest::random::insecure::Host::get_insecure_random_u64(self)
752    }
753}
754
755impl wasi::random::insecure_seed::Host for WasiRandomCtx {
756    fn insecure_seed(&mut self) -> wasmtime::Result<(u64, u64)> {
757        latest::random::insecure_seed::Host::insecure_seed(self)
758    }
759}
760
761impl wasi::cli::exit::Host for WasiCliCtxView<'_> {
762    fn exit(&mut self, status: Result<(), ()>) -> wasmtime::Result<()> {
763        latest::cli::exit::Host::exit(self, status)
764    }
765}
766
767impl wasi::cli::environment::Host for WasiCliCtxView<'_> {
768    fn get_environment(&mut self) -> wasmtime::Result<Vec<(String, String)>> {
769        latest::cli::environment::Host::get_environment(self)
770    }
771
772    fn get_arguments(&mut self) -> wasmtime::Result<Vec<String>> {
773        latest::cli::environment::Host::get_arguments(self)
774    }
775
776    fn initial_cwd(&mut self) -> wasmtime::Result<Option<String>> {
777        latest::cli::environment::Host::initial_cwd(self)
778    }
779}
780
781impl wasi::cli::stdin::Host for WasiCliCtxView<'_> {
782    fn get_stdin(&mut self) -> wasmtime::Result<Resource<InputStream>> {
783        latest::cli::stdin::Host::get_stdin(self)
784    }
785}
786
787impl wasi::cli::stdout::Host for WasiCliCtxView<'_> {
788    fn get_stdout(&mut self) -> wasmtime::Result<Resource<OutputStream>> {
789        latest::cli::stdout::Host::get_stdout(self)
790    }
791}
792
793impl wasi::cli::stderr::Host for WasiCliCtxView<'_> {
794    fn get_stderr(&mut self) -> wasmtime::Result<Resource<OutputStream>> {
795        latest::cli::stderr::Host::get_stderr(self)
796    }
797}
798
799impl wasi::cli::terminal_stdin::Host for WasiCliCtxView<'_> {
800    fn get_terminal_stdin(&mut self) -> wasmtime::Result<Option<Resource<TerminalInput>>> {
801        latest::cli::terminal_stdin::Host::get_terminal_stdin(self)
802    }
803}
804
805impl wasi::cli::terminal_stdout::Host for WasiCliCtxView<'_> {
806    fn get_terminal_stdout(&mut self) -> wasmtime::Result<Option<Resource<TerminalOutput>>> {
807        latest::cli::terminal_stdout::Host::get_terminal_stdout(self)
808    }
809}
810
811impl wasi::cli::terminal_stderr::Host for WasiCliCtxView<'_> {
812    fn get_terminal_stderr(&mut self) -> wasmtime::Result<Option<Resource<TerminalOutput>>> {
813        latest::cli::terminal_stderr::Host::get_terminal_stderr(self)
814    }
815}
816
817impl wasi::cli::terminal_input::Host for WasiCliCtxView<'_> {}
818
819impl wasi::cli::terminal_input::HostTerminalInput for WasiCliCtxView<'_> {
820    fn drop(&mut self, rep: Resource<TerminalInput>) -> wasmtime::Result<()> {
821        latest::cli::terminal_input::HostTerminalInput::drop(self, rep)
822    }
823}
824
825impl wasi::cli::terminal_output::Host for WasiCliCtxView<'_> {}
826
827impl wasi::cli::terminal_output::HostTerminalOutput for WasiCliCtxView<'_> {
828    fn drop(&mut self, rep: Resource<TerminalOutput>) -> wasmtime::Result<()> {
829        latest::cli::terminal_output::HostTerminalOutput::drop(self, rep)
830    }
831}
832
833impl wasi::sockets::tcp::Host for WasiSocketsCtxView<'_> {}
834
835impl wasi::sockets::tcp::HostTcpSocket for WasiSocketsCtxView<'_> {
836    async fn start_bind(
837        &mut self,
838        self_: Resource<TcpSocket>,
839        network: Resource<Network>,
840        local_address: IpSocketAddress,
841    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
842        convert_result(
843            latest::sockets::tcp::HostTcpSocket::start_bind(
844                self,
845                self_,
846                network,
847                local_address.into(),
848            )
849            .await,
850        )
851    }
852
853    fn finish_bind(
854        &mut self,
855        self_: Resource<TcpSocket>,
856    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
857        convert_result(latest::sockets::tcp::HostTcpSocket::finish_bind(
858            self, self_,
859        ))
860    }
861
862    async fn start_connect(
863        &mut self,
864        self_: Resource<TcpSocket>,
865        network: Resource<Network>,
866        remote_address: IpSocketAddress,
867    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
868        convert_result(
869            latest::sockets::tcp::HostTcpSocket::start_connect(
870                self,
871                self_,
872                network,
873                remote_address.into(),
874            )
875            .await,
876        )
877    }
878
879    fn finish_connect(
880        &mut self,
881        self_: Resource<TcpSocket>,
882    ) -> wasmtime::Result<Result<(Resource<InputStream>, Resource<OutputStream>), SocketErrorCode>>
883    {
884        convert_result(latest::sockets::tcp::HostTcpSocket::finish_connect(
885            self, self_,
886        ))
887    }
888
889    fn start_listen(
890        &mut self,
891        self_: Resource<TcpSocket>,
892    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
893        convert_result(latest::sockets::tcp::HostTcpSocket::start_listen(
894            self, self_,
895        ))
896    }
897
898    fn finish_listen(
899        &mut self,
900        self_: Resource<TcpSocket>,
901    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
902        convert_result(latest::sockets::tcp::HostTcpSocket::finish_listen(
903            self, self_,
904        ))
905    }
906
907    fn accept(
908        &mut self,
909        self_: Resource<TcpSocket>,
910    ) -> wasmtime::Result<
911        Result<
912            (
913                Resource<TcpSocket>,
914                Resource<InputStream>,
915                Resource<OutputStream>,
916            ),
917            SocketErrorCode,
918        >,
919    > {
920        convert_result(latest::sockets::tcp::HostTcpSocket::accept(self, self_))
921    }
922
923    fn local_address(
924        &mut self,
925        self_: Resource<TcpSocket>,
926    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
927        convert_result(latest::sockets::tcp::HostTcpSocket::local_address(
928            self, self_,
929        ))
930    }
931
932    fn remote_address(
933        &mut self,
934        self_: Resource<TcpSocket>,
935    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
936        convert_result(latest::sockets::tcp::HostTcpSocket::remote_address(
937            self, self_,
938        ))
939    }
940
941    fn address_family(&mut self, self_: Resource<TcpSocket>) -> wasmtime::Result<IpAddressFamily> {
942        latest::sockets::tcp::HostTcpSocket::address_family(self, self_).map(|e| e.into())
943    }
944
945    fn ipv6_only(
946        &mut self,
947        _self_: Resource<TcpSocket>,
948    ) -> wasmtime::Result<Result<bool, SocketErrorCode>> {
949        anyhow::bail!("ipv6-only API no longer supported")
950    }
951
952    fn set_ipv6_only(
953        &mut self,
954        _self_: Resource<TcpSocket>,
955        _value: bool,
956    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
957        anyhow::bail!("ipv6-only API no longer supported")
958    }
959
960    fn set_listen_backlog_size(
961        &mut self,
962        self_: Resource<TcpSocket>,
963        value: u64,
964    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
965        convert_result(
966            latest::sockets::tcp::HostTcpSocket::set_listen_backlog_size(self, self_, value),
967        )
968    }
969
970    fn is_listening(&mut self, self_: Resource<TcpSocket>) -> wasmtime::Result<bool> {
971        latest::sockets::tcp::HostTcpSocket::is_listening(self, self_)
972    }
973
974    fn keep_alive_enabled(
975        &mut self,
976        self_: Resource<TcpSocket>,
977    ) -> wasmtime::Result<Result<bool, SocketErrorCode>> {
978        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_enabled(
979            self, self_,
980        ))
981    }
982
983    fn set_keep_alive_enabled(
984        &mut self,
985        self_: Resource<TcpSocket>,
986        value: bool,
987    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
988        convert_result(latest::sockets::tcp::HostTcpSocket::set_keep_alive_enabled(
989            self, self_, value,
990        ))
991    }
992
993    fn keep_alive_idle_time(
994        &mut self,
995        self_: Resource<TcpSocket>,
996    ) -> wasmtime::Result<Result<Duration, SocketErrorCode>> {
997        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_idle_time(
998            self, self_,
999        ))
1000    }
1001
1002    fn set_keep_alive_idle_time(
1003        &mut self,
1004        self_: Resource<TcpSocket>,
1005        value: Duration,
1006    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1007        convert_result(
1008            latest::sockets::tcp::HostTcpSocket::set_keep_alive_idle_time(self, self_, value),
1009        )
1010    }
1011
1012    fn keep_alive_interval(
1013        &mut self,
1014        self_: Resource<TcpSocket>,
1015    ) -> wasmtime::Result<Result<Duration, SocketErrorCode>> {
1016        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_interval(
1017            self, self_,
1018        ))
1019    }
1020
1021    fn set_keep_alive_interval(
1022        &mut self,
1023        self_: Resource<TcpSocket>,
1024        value: Duration,
1025    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1026        convert_result(
1027            latest::sockets::tcp::HostTcpSocket::set_keep_alive_interval(self, self_, value),
1028        )
1029    }
1030
1031    fn keep_alive_count(
1032        &mut self,
1033        self_: Resource<TcpSocket>,
1034    ) -> wasmtime::Result<Result<u32, SocketErrorCode>> {
1035        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_count(
1036            self, self_,
1037        ))
1038    }
1039
1040    fn set_keep_alive_count(
1041        &mut self,
1042        self_: Resource<TcpSocket>,
1043        value: u32,
1044    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1045        convert_result(latest::sockets::tcp::HostTcpSocket::set_keep_alive_count(
1046            self, self_, value,
1047        ))
1048    }
1049
1050    fn hop_limit(
1051        &mut self,
1052        self_: Resource<TcpSocket>,
1053    ) -> wasmtime::Result<Result<u8, SocketErrorCode>> {
1054        convert_result(latest::sockets::tcp::HostTcpSocket::hop_limit(self, self_))
1055    }
1056
1057    fn set_hop_limit(
1058        &mut self,
1059        self_: Resource<TcpSocket>,
1060        value: u8,
1061    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1062        convert_result(latest::sockets::tcp::HostTcpSocket::set_hop_limit(
1063            self, self_, value,
1064        ))
1065    }
1066
1067    fn receive_buffer_size(
1068        &mut self,
1069        self_: Resource<TcpSocket>,
1070    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1071        convert_result(latest::sockets::tcp::HostTcpSocket::receive_buffer_size(
1072            self, self_,
1073        ))
1074    }
1075
1076    fn set_receive_buffer_size(
1077        &mut self,
1078        self_: Resource<TcpSocket>,
1079        value: u64,
1080    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1081        convert_result(
1082            latest::sockets::tcp::HostTcpSocket::set_receive_buffer_size(self, self_, value),
1083        )
1084    }
1085
1086    fn send_buffer_size(
1087        &mut self,
1088        self_: Resource<TcpSocket>,
1089    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1090        convert_result(latest::sockets::tcp::HostTcpSocket::send_buffer_size(
1091            self, self_,
1092        ))
1093    }
1094
1095    fn set_send_buffer_size(
1096        &mut self,
1097        self_: Resource<TcpSocket>,
1098        value: u64,
1099    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1100        convert_result(latest::sockets::tcp::HostTcpSocket::set_send_buffer_size(
1101            self, self_, value,
1102        ))
1103    }
1104
1105    fn subscribe(&mut self, self_: Resource<TcpSocket>) -> wasmtime::Result<Resource<Pollable>> {
1106        latest::sockets::tcp::HostTcpSocket::subscribe(self, self_)
1107    }
1108
1109    fn shutdown(
1110        &mut self,
1111        self_: Resource<TcpSocket>,
1112        shutdown_type: ShutdownType,
1113    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1114        convert_result(latest::sockets::tcp::HostTcpSocket::shutdown(
1115            self,
1116            self_,
1117            shutdown_type.into(),
1118        ))
1119    }
1120
1121    fn drop(&mut self, rep: Resource<TcpSocket>) -> wasmtime::Result<()> {
1122        latest::sockets::tcp::HostTcpSocket::drop(self, rep)
1123    }
1124}
1125
1126impl wasi::sockets::tcp_create_socket::Host for WasiSocketsCtxView<'_> {
1127    fn create_tcp_socket(
1128        &mut self,
1129        address_family: IpAddressFamily,
1130    ) -> wasmtime::Result<Result<Resource<TcpSocket>, SocketErrorCode>> {
1131        convert_result(latest::sockets::tcp_create_socket::Host::create_tcp_socket(
1132            self,
1133            address_family.into(),
1134        ))
1135    }
1136}
1137
1138impl wasi::sockets::udp::Host for WasiSocketsCtxView<'_> {}
1139
1140impl wasi::sockets::udp::HostUdpSocket for WasiSocketsCtxView<'_> {
1141    async fn start_bind(
1142        &mut self,
1143        self_: Resource<UdpSocket>,
1144        network: Resource<Network>,
1145        local_address: IpSocketAddress,
1146    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1147        convert_result(
1148            latest::sockets::udp::HostUdpSocket::start_bind(
1149                self,
1150                self_,
1151                network,
1152                local_address.into(),
1153            )
1154            .await,
1155        )
1156    }
1157
1158    fn finish_bind(
1159        &mut self,
1160        self_: Resource<UdpSocket>,
1161    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1162        convert_result(latest::sockets::udp::HostUdpSocket::finish_bind(
1163            self, self_,
1164        ))
1165    }
1166
1167    fn local_address(
1168        &mut self,
1169        self_: Resource<UdpSocket>,
1170    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
1171        convert_result(latest::sockets::udp::HostUdpSocket::local_address(
1172            self, self_,
1173        ))
1174    }
1175
1176    fn remote_address(
1177        &mut self,
1178        self_: Resource<UdpSocket>,
1179    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
1180        convert_result(latest::sockets::udp::HostUdpSocket::remote_address(
1181            self, self_,
1182        ))
1183    }
1184
1185    fn address_family(&mut self, self_: Resource<UdpSocket>) -> wasmtime::Result<IpAddressFamily> {
1186        latest::sockets::udp::HostUdpSocket::address_family(self, self_).map(|e| e.into())
1187    }
1188
1189    fn ipv6_only(
1190        &mut self,
1191        _self_: Resource<UdpSocket>,
1192    ) -> wasmtime::Result<Result<bool, SocketErrorCode>> {
1193        anyhow::bail!("ipv6-only API no longer supported")
1194    }
1195
1196    fn set_ipv6_only(
1197        &mut self,
1198        _self_: Resource<UdpSocket>,
1199        _value: bool,
1200    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1201        anyhow::bail!("ipv6-only API no longer supported")
1202    }
1203
1204    fn unicast_hop_limit(
1205        &mut self,
1206        self_: Resource<UdpSocket>,
1207    ) -> wasmtime::Result<Result<u8, SocketErrorCode>> {
1208        convert_result(latest::sockets::udp::HostUdpSocket::unicast_hop_limit(
1209            self, self_,
1210        ))
1211    }
1212
1213    fn set_unicast_hop_limit(
1214        &mut self,
1215        self_: Resource<UdpSocket>,
1216        value: u8,
1217    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1218        convert_result(latest::sockets::udp::HostUdpSocket::set_unicast_hop_limit(
1219            self, self_, value,
1220        ))
1221    }
1222
1223    fn receive_buffer_size(
1224        &mut self,
1225        self_: Resource<UdpSocket>,
1226    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1227        convert_result(latest::sockets::udp::HostUdpSocket::receive_buffer_size(
1228            self, self_,
1229        ))
1230    }
1231
1232    fn set_receive_buffer_size(
1233        &mut self,
1234        self_: Resource<UdpSocket>,
1235        value: u64,
1236    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1237        convert_result(
1238            latest::sockets::udp::HostUdpSocket::set_receive_buffer_size(self, self_, value),
1239        )
1240    }
1241
1242    fn send_buffer_size(
1243        &mut self,
1244        self_: Resource<UdpSocket>,
1245    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1246        convert_result(latest::sockets::udp::HostUdpSocket::send_buffer_size(
1247            self, self_,
1248        ))
1249    }
1250
1251    fn set_send_buffer_size(
1252        &mut self,
1253        self_: Resource<UdpSocket>,
1254        value: u64,
1255    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1256        convert_result(latest::sockets::udp::HostUdpSocket::set_send_buffer_size(
1257            self, self_, value,
1258        ))
1259    }
1260
1261    async fn stream(
1262        &mut self,
1263        self_: Resource<UdpSocket>,
1264        remote_address: Option<IpSocketAddress>,
1265    ) -> wasmtime::Result<
1266        Result<
1267            (
1268                Resource<IncomingDatagramStream>,
1269                Resource<OutgoingDatagramStream>,
1270            ),
1271            SocketErrorCode,
1272        >,
1273    > {
1274        convert_result(
1275            latest::sockets::udp::HostUdpSocket::stream(
1276                self,
1277                self_,
1278                remote_address.map(|a| a.into()),
1279            )
1280            .await,
1281        )
1282    }
1283
1284    fn subscribe(&mut self, self_: Resource<UdpSocket>) -> wasmtime::Result<Resource<Pollable>> {
1285        latest::sockets::udp::HostUdpSocket::subscribe(self, self_)
1286    }
1287
1288    fn drop(&mut self, rep: Resource<UdpSocket>) -> wasmtime::Result<()> {
1289        latest::sockets::udp::HostUdpSocket::drop(self, rep)
1290    }
1291}
1292
1293impl wasi::sockets::udp::HostOutgoingDatagramStream for WasiSocketsCtxView<'_> {
1294    fn check_send(
1295        &mut self,
1296        self_: Resource<OutgoingDatagramStream>,
1297    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1298        convert_result(latest::sockets::udp::HostOutgoingDatagramStream::check_send(self, self_))
1299    }
1300
1301    async fn send(
1302        &mut self,
1303        self_: Resource<OutgoingDatagramStream>,
1304        datagrams: Vec<OutgoingDatagram>,
1305    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1306        convert_result(
1307            latest::sockets::udp::HostOutgoingDatagramStream::send(
1308                self,
1309                self_,
1310                datagrams.into_iter().map(|d| d.into()).collect(),
1311            )
1312            .await,
1313        )
1314    }
1315
1316    fn subscribe(
1317        &mut self,
1318        self_: Resource<OutgoingDatagramStream>,
1319    ) -> wasmtime::Result<Resource<Pollable>> {
1320        latest::sockets::udp::HostOutgoingDatagramStream::subscribe(self, self_)
1321    }
1322
1323    fn drop(&mut self, rep: Resource<OutgoingDatagramStream>) -> wasmtime::Result<()> {
1324        latest::sockets::udp::HostOutgoingDatagramStream::drop(self, rep)
1325    }
1326}
1327
1328impl wasi::sockets::udp::HostIncomingDatagramStream for WasiSocketsCtxView<'_> {
1329    fn receive(
1330        &mut self,
1331        self_: Resource<IncomingDatagramStream>,
1332        max_results: u64,
1333    ) -> wasmtime::Result<Result<Vec<IncomingDatagram>, SocketErrorCode>> {
1334        convert_result(latest::sockets::udp::HostIncomingDatagramStream::receive(
1335            self,
1336            self_,
1337            max_results,
1338        ))
1339        .map(|r| r.map(|r: Vec<_>| r.into_iter().map(|d| d.into()).collect()))
1340    }
1341
1342    fn subscribe(
1343        &mut self,
1344        self_: Resource<IncomingDatagramStream>,
1345    ) -> wasmtime::Result<Resource<Pollable>> {
1346        latest::sockets::udp::HostIncomingDatagramStream::subscribe(self, self_)
1347    }
1348
1349    fn drop(&mut self, rep: Resource<IncomingDatagramStream>) -> wasmtime::Result<()> {
1350        latest::sockets::udp::HostIncomingDatagramStream::drop(self, rep)
1351    }
1352}
1353
1354impl wasi::sockets::udp_create_socket::Host for WasiSocketsCtxView<'_> {
1355    fn create_udp_socket(
1356        &mut self,
1357        address_family: IpAddressFamily,
1358    ) -> wasmtime::Result<Result<Resource<UdpSocket>, SocketErrorCode>> {
1359        convert_result(latest::sockets::udp_create_socket::Host::create_udp_socket(
1360            self,
1361            address_family.into(),
1362        ))
1363    }
1364}
1365
1366impl wasi::sockets::instance_network::Host for WasiSocketsCtxView<'_> {
1367    fn instance_network(&mut self) -> wasmtime::Result<Resource<Network>> {
1368        latest::sockets::instance_network::Host::instance_network(self)
1369    }
1370}
1371
1372impl wasi::sockets::network::Host for WasiSocketsCtxView<'_> {}
1373
1374impl wasi::sockets::network::HostNetwork for WasiSocketsCtxView<'_> {
1375    fn drop(&mut self, rep: Resource<Network>) -> wasmtime::Result<()> {
1376        latest::sockets::network::HostNetwork::drop(self, rep)
1377    }
1378}
1379
1380impl wasi::sockets::ip_name_lookup::Host for WasiSocketsCtxView<'_> {
1381    fn resolve_addresses(
1382        &mut self,
1383        network: Resource<Network>,
1384        name: String,
1385    ) -> wasmtime::Result<Result<Resource<ResolveAddressStream>, SocketErrorCode>> {
1386        convert_result(latest::sockets::ip_name_lookup::Host::resolve_addresses(
1387            self, network, name,
1388        ))
1389    }
1390}
1391
1392impl wasi::sockets::ip_name_lookup::HostResolveAddressStream for WasiSocketsCtxView<'_> {
1393    fn resolve_next_address(
1394        &mut self,
1395        self_: Resource<ResolveAddressStream>,
1396    ) -> wasmtime::Result<Result<Option<IpAddress>, SocketErrorCode>> {
1397        convert_result(
1398            latest::sockets::ip_name_lookup::HostResolveAddressStream::resolve_next_address(
1399                self, self_,
1400            )
1401            .map(|e| e.map(|e| e.into())),
1402        )
1403    }
1404
1405    fn subscribe(
1406        &mut self,
1407        self_: Resource<ResolveAddressStream>,
1408    ) -> wasmtime::Result<Resource<Pollable>> {
1409        latest::sockets::ip_name_lookup::HostResolveAddressStream::subscribe(self, self_)
1410    }
1411
1412    fn drop(&mut self, rep: Resource<ResolveAddressStream>) -> wasmtime::Result<()> {
1413        latest::sockets::ip_name_lookup::HostResolveAddressStream::drop(self, rep)
1414    }
1415}
1416
1417convert! {
1418    struct latest::clocks::wall_clock::Datetime [<=>] Datetime {
1419        seconds,
1420        nanoseconds,
1421    }
1422
1423    enum latest::filesystem::types::ErrorCode => FsErrorCode {
1424        Access,
1425        WouldBlock,
1426        Already,
1427        BadDescriptor,
1428        Busy,
1429        Deadlock,
1430        Quota,
1431        Exist,
1432        FileTooLarge,
1433        IllegalByteSequence,
1434        InProgress,
1435        Interrupted,
1436        Invalid,
1437        Io,
1438        IsDirectory,
1439        Loop,
1440        TooManyLinks,
1441        MessageSize,
1442        NameTooLong,
1443        NoDevice,
1444        NoEntry,
1445        NoLock,
1446        InsufficientMemory,
1447        InsufficientSpace,
1448        NotDirectory,
1449        NotEmpty,
1450        NotRecoverable,
1451        Unsupported,
1452        NoTty,
1453        NoSuchDevice,
1454        Overflow,
1455        NotPermitted,
1456        Pipe,
1457        ReadOnly,
1458        InvalidSeek,
1459        TextFileBusy,
1460        CrossDevice,
1461    }
1462
1463    enum Advice => latest::filesystem::types::Advice {
1464        Normal,
1465        Sequential,
1466        Random,
1467        WillNeed,
1468        DontNeed,
1469        NoReuse,
1470    }
1471
1472    flags DescriptorFlags [<=>] latest::filesystem::types::DescriptorFlags {
1473        READ,
1474        WRITE,
1475        FILE_INTEGRITY_SYNC,
1476        DATA_INTEGRITY_SYNC,
1477        REQUESTED_WRITE_SYNC,
1478        MUTATE_DIRECTORY,
1479    }
1480
1481    enum DescriptorType [<=>] latest::filesystem::types::DescriptorType {
1482        Unknown,
1483        BlockDevice,
1484        CharacterDevice,
1485        Directory,
1486        Fifo,
1487        SymbolicLink,
1488        RegularFile,
1489        Socket,
1490    }
1491
1492    enum NewTimestamp => latest::filesystem::types::NewTimestamp {
1493        NoChange,
1494        Now,
1495        Timestamp(e),
1496    }
1497
1498    flags PathFlags => latest::filesystem::types::PathFlags {
1499        SYMLINK_FOLLOW,
1500    }
1501
1502    flags OpenFlags => latest::filesystem::types::OpenFlags {
1503        CREATE,
1504        DIRECTORY,
1505        EXCLUSIVE,
1506        TRUNCATE,
1507    }
1508
1509    struct latest::filesystem::types::MetadataHashValue => MetadataHashValue {
1510        lower,
1511        upper,
1512    }
1513
1514    struct latest::filesystem::types::DirectoryEntry => DirectoryEntry {
1515        type_,
1516        name,
1517    }
1518
1519
1520    enum latest::sockets::network::ErrorCode => SocketErrorCode {
1521        Unknown,
1522        AccessDenied,
1523        NotSupported,
1524        InvalidArgument,
1525        OutOfMemory,
1526        Timeout,
1527        ConcurrencyConflict,
1528        NotInProgress,
1529        WouldBlock,
1530        InvalidState,
1531        NewSocketLimit,
1532        AddressNotBindable,
1533        AddressInUse,
1534        RemoteUnreachable,
1535        ConnectionRefused,
1536        ConnectionReset,
1537        ConnectionAborted,
1538        DatagramTooLarge,
1539        NameUnresolvable,
1540        TemporaryResolverFailure,
1541        PermanentResolverFailure,
1542    }
1543
1544    enum latest::sockets::network::IpAddress [<=>] IpAddress {
1545        Ipv4(e),
1546        Ipv6(e),
1547    }
1548
1549    enum latest::sockets::network::IpSocketAddress [<=>] IpSocketAddress {
1550        Ipv4(e),
1551        Ipv6(e),
1552    }
1553
1554    struct latest::sockets::network::Ipv4SocketAddress [<=>] Ipv4SocketAddress {
1555        port,
1556        address,
1557    }
1558
1559    struct latest::sockets::network::Ipv6SocketAddress [<=>] Ipv6SocketAddress {
1560        port,
1561        flow_info,
1562        scope_id,
1563        address,
1564    }
1565
1566    enum latest::sockets::network::IpAddressFamily [<=>] IpAddressFamily {
1567        Ipv4,
1568        Ipv6,
1569    }
1570
1571    enum ShutdownType => latest::sockets::tcp::ShutdownType {
1572        Receive,
1573        Send,
1574        Both,
1575    }
1576
1577    struct latest::sockets::udp::IncomingDatagram => IncomingDatagram {
1578        data,
1579        remote_address,
1580    }
1581}
1582
1583impl From<latest::filesystem::types::DescriptorStat> for DescriptorStat {
1584    fn from(e: latest::filesystem::types::DescriptorStat) -> DescriptorStat {
1585        DescriptorStat {
1586            type_: e.type_.into(),
1587            link_count: e.link_count,
1588            size: e.size,
1589            data_access_timestamp: e.data_access_timestamp.map(|e| e.into()),
1590            data_modification_timestamp: e.data_modification_timestamp.map(|e| e.into()),
1591            status_change_timestamp: e.status_change_timestamp.map(|e| e.into()),
1592        }
1593    }
1594}
1595
1596impl From<OutgoingDatagram> for latest::sockets::udp::OutgoingDatagram {
1597    fn from(d: OutgoingDatagram) -> Self {
1598        Self {
1599            data: d.data,
1600            remote_address: d.remote_address.map(|a| a.into()),
1601        }
1602    }
1603}