//! # Wasmtime's wasi-io Implementation1//!2//! This crate provides a Wasmtime host implementation of the WASI 0.2 (aka3//! WASIp2 aka Preview 2) wasi-io package. The host implementation is4//! abstract: it is exposed as a set of traits which other crates provide5//! impls of.6//!7//! The wasi-io package is the foundation which defines how WASI programs8//! interact with the scheduler. It provides the `pollable`, `input-stream`,9//! and `output-stream` Component Model resources, which other packages10//! (including wasi-filesystem, wasi-sockets, wasi-cli, and wasi-http)11//! expose as the standard way to wait for readiness, and asynchronously read12//! and write to streams.13//!14//! This crate is designed to have no unnecessary dependencies and, in15//! particular, to be #![no_std]. For an example no_std embedding, see16//! [`/examples/min-platform`](https://github.com/bytecodealliance/wasmtime/tree/main/examples/min-platform)17//! at the root of the wasmtime repo.1819#![no_std]2021extern crate alloc;22#[cfg(feature = "std")]23#[macro_use]24extern crate std;2526pub mod bindings;27mod impls;28pub mod poll;29pub mod streams;3031#[doc(no_inline)]32pub use async_trait::async_trait;3334#[doc(no_inline)]35pub use ::bytes;3637use alloc::boxed::Box;38use wasmtime::component::{HasData, ResourceTable};3940/// A trait which provides access to the [`ResourceTable`] inside the41/// embedder's `T` of [`Store<T>`][`Store`].42///43/// This crate's WASI Host implementations depend on the contents of44/// [`ResourceTable`]. The `T` type [`Store<T>`][`Store`] is defined in each45/// embedding of Wasmtime. These implementations is connected to the46/// [`Linker<T>`][`Linker`] by the47/// [`add_to_linker_async`] function.48///49/// # Example50///51/// ```52/// use wasmtime::Engine;53/// use wasmtime::component::{ResourceTable, Linker};54/// use wasmtime_wasi_io::{IoView, add_to_linker_async};55///56/// struct MyState {57/// table: ResourceTable,58/// }59///60/// impl IoView for MyState {61/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }62/// }63/// let engine = Engine::default();64/// let mut linker: Linker<MyState> = Linker::new(&engine);65/// add_to_linker_async(&mut linker).unwrap();66/// ```67/// [`Store`]: wasmtime::Store68/// [`Linker`]: wasmtime::component::Linker69/// [`ResourceTable`]: wasmtime::component::ResourceTable70///71pub trait IoView {72/// Yields mutable access to the internal resource management that this73/// context contains.74///75/// Embedders can add custom resources to this table as well to give76/// resources to wasm as well.77fn table(&mut self) -> &mut ResourceTable;78}7980impl<T: ?Sized + IoView> IoView for &mut T {81fn table(&mut self) -> &mut ResourceTable {82T::table(self)83}84}85impl<T: ?Sized + IoView> IoView for Box<T> {86fn table(&mut self) -> &mut ResourceTable {87T::table(self)88}89}9091/// Add the wasi-io host implementation from this crate into the `linker`92/// provided.93///94/// This function will add the `async` variant of all interfaces into the95/// [`Linker`] provided. For embeddings which don't want to use async, you'll96/// need to use other crates, such as the [`wasmtime-wasi`] crate, which97/// provides an [`add_to_linker_sync`] that includes an appropriate wasi-io98/// implementation based on this crate's.99///100/// This function will add all interfaces implemented by this crate to the101/// [`Linker`], which corresponds to the `wasi:io/imports` world supported by102/// this crate.103///104/// [`Linker`]: wasmtime::component::Linker105/// [`wasmtime-wasi`]: https://crates.io/crates/wasmtime-wasi106/// [`add_to_linker_sync`]: https://docs.rs/wasmtime-wasi/latest/wasmtime_wasi/p2/fn.add_to_linker_sync.html107///108///109/// # Example110///111/// ```112/// use wasmtime::{Engine, Result, Store};113/// use wasmtime::component::{ResourceTable, Linker};114/// use wasmtime_wasi_io::IoView;115///116/// fn main() -> Result<()> {117/// let engine = Engine::default();118///119/// let mut linker = Linker::<MyState>::new(&engine);120/// wasmtime_wasi_io::add_to_linker_async(&mut linker)?;121/// // ... add any further functionality to `linker` if desired ...122///123/// let mut store = Store::new(124/// &engine,125/// MyState {126/// table: ResourceTable::new(),127/// },128/// );129///130/// // ... use `linker` to instantiate within `store` ...131///132/// Ok(())133/// }134///135/// struct MyState {136/// table: ResourceTable,137/// }138///139/// impl IoView for MyState {140/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }141/// }142/// ```143pub fn add_to_linker_async<T: IoView + Send + 'static>(144l: &mut wasmtime::component::Linker<T>,145) -> wasmtime::Result<()> {146crate::bindings::wasi::io::error::add_to_linker::<T, WasiIo>(l, T::table)?;147crate::bindings::wasi::io::poll::add_to_linker::<T, WasiIo>(l, T::table)?;148crate::bindings::wasi::io::streams::add_to_linker::<T, WasiIo>(l, T::table)?;149Ok(())150}151152struct WasiIo;153154impl HasData for WasiIo {155type Data<'a> = &'a mut ResourceTable;156}157158159