wasmtime_wasi_io/lib.rs
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
//! # Wasmtime's wasi-io Implementation
//!
//! This crate provides a Wasmtime host implementation of the WASI 0.2 (aka
//! WASIp2 aka Preview 2) wasi-io package. The host implementation is
//! abstract: it is exposed as a set of traits which other crates provide
//! impls of.
//!
//! The wasi-io package is the foundation which defines how WASI programs
//! interact with the scheduler. It provides the `pollable`, `input-stream`,
//! and `output-stream` Component Model resources, which other packages
//! (including wasi-filesystem, wasi-sockets, wasi-cli, and wasi-http)
//! expose as the standard way to wait for readiness, and asynchronously read
//! and write to streams.
//!
//! This crate is designed to have no unnecessary dependencies and, in
//! particular, to be #![no_std].
#![no_std]
extern crate alloc;
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
pub mod bindings;
mod impls;
pub mod poll;
pub mod streams;
#[doc(no_inline)]
pub use async_trait::async_trait;
use alloc::boxed::Box;
use wasmtime::component::ResourceTable;
/// A trait which provides access to the [`ResourceTable`] inside the
/// embedder's `T` of [`Store<T>`][`Store`].
///
/// This crate's WASI Host implementations depend on the contents of
/// [`ResourceTable`]. The `T` type [`Store<T>`][`Store`] is defined in each
/// embedding of Wasmtime. These implementations is connected to the
/// [`Linker<T>`][`Linker`] by the
/// [`add_to_linker_async`] function.
///
/// # Example
///
/// ```
/// use wasmtime::{Config, Engine};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi_io::{IoView, add_to_linker_async};
///
/// struct MyState {
/// table: ResourceTable,
/// }
///
/// impl IoView for MyState {
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// let mut config = Config::new();
/// config.async_support(true);
/// let engine = Engine::new(&config).unwrap();
/// let mut linker: Linker<MyState> = Linker::new(&engine);
/// add_to_linker_async(&mut linker).unwrap();
/// ```
/// [`Store`]: wasmtime::Store
/// [`Linker`]: wasmtime::component::Linker
/// [`ResourceTable`]: wasmtime::component::ResourceTable
///
pub trait IoView: Send {
/// Yields mutable access to the internal resource management that this
/// context contains.
///
/// Embedders can add custom resources to this table as well to give
/// resources to wasm as well.
fn table(&mut self) -> &mut ResourceTable;
}
impl<T: ?Sized + IoView> IoView for &mut T {
fn table(&mut self) -> &mut ResourceTable {
T::table(self)
}
}
impl<T: ?Sized + IoView> IoView for Box<T> {
fn table(&mut self) -> &mut ResourceTable {
T::table(self)
}
}
/// A small newtype wrapper which serves as the basis for implementations of
/// `Host` WASI traits in this crate.
///
/// This type is used as the basis for the implementation of all `Host` traits
/// generated by `bindgen!` for WASI interfaces.
///
/// You don't need to use this type if you are using the root
/// [`add_to_linker_async`] in this crate.
///
/// If you're calling the `add_to_linker` functions generated by `bindgen!`
/// from the [`bindings` module](crate::bindings), you'll want to create a
/// value of this type in the closures added to a [`Linker`].
///
/// [`Linker`]: wasmtime::component::Linker
#[repr(transparent)]
pub struct IoImpl<T>(pub T);
impl<T: IoView> IoView for IoImpl<T> {
fn table(&mut self) -> &mut ResourceTable {
T::table(&mut self.0)
}
}
/// Add the wasi-io host implementation from this crate into the `linker`
/// provided.
///
/// This function will add the `async` variant of all interfaces into the
/// [`Linker`] provided. By `async` this means that this function is only
/// compatible with [`Config::async_support(true)`][async]. For embeddings
/// with async support disabled, you'll need to use other crates, such as the
/// [`wasmtime-wasi`] crate, which provides an [`add_to_linker_sync`] that
/// includes an appropriate wasi-io implementation based on this crate's.
///
/// This function will add all interfaces implemented by this crate to the
/// [`Linker`], which corresponds to the `wasi:io/imports` world supported by
/// this crate.
///
/// [async]: wasmtime::Config::async_support
/// [`Linker`]: wasmtime::component::Linker
/// [`wasmtime-wasi`]: https://crates.io/crates/wasmtime-wasi
/// [`add_to_linker_sync`]: https://docs.rs/wasmtime-wasi/latest/wasmtime_wasi/fn.add_to_linker_sync.html
///
///
/// # Example
///
/// ```
/// use wasmtime::{Engine, Result, Store, Config};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi_io::IoView;
///
/// fn main() -> Result<()> {
/// let mut config = Config::new();
/// config.async_support(true);
/// let engine = Engine::new(&config)?;
///
/// let mut linker = Linker::<MyState>::new(&engine);
/// wasmtime_wasi_io::add_to_linker_async(&mut linker)?;
/// // ... add any further functionality to `linker` if desired ...
///
/// let mut store = Store::new(
/// &engine,
/// MyState {
/// table: ResourceTable::new(),
/// },
/// );
///
/// // ... use `linker` to instantiate within `store` ...
///
/// Ok(())
/// }
///
/// struct MyState {
/// table: ResourceTable,
/// }
///
/// impl IoView for MyState {
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// ```
pub fn add_to_linker_async<T: IoView>(
l: &mut wasmtime::component::Linker<T>,
) -> wasmtime::Result<()> {
let closure = io_type_annotate::<T, _>(|t| IoImpl(t));
crate::bindings::wasi::io::error::add_to_linker_get_host(l, closure)?;
crate::bindings::wasi::io::poll::add_to_linker_get_host(l, closure)?;
crate::bindings::wasi::io::streams::add_to_linker_get_host(l, closure)?;
Ok(())
}
// NB: workaround some rustc inference - a future refactoring may make this
// obsolete.
fn io_type_annotate<T: IoView, F>(val: F) -> F
where
F: Fn(&mut T) -> IoImpl<&mut T>,
{
val
}