Path: blob/main/crates/wasi-http/src/p3/mod.rs
1692 views
//! Experimental, unstable and incomplete implementation of wasip3 version of `wasi:http`.1//!2//! This module is under heavy development.3//! It is not compliant with semver and is not ready4//! for production use.5//!6//! Bug and security fixes limited to wasip3 will not be given patch releases.7//!8//! Documentation of this module may be incorrect or out-of-sync with the implementation.910pub mod bindings;11mod conv;12#[expect(unused, reason = "work in progress")] // TODO: implement13mod host;1415use bindings::http::{handler, types};16use wasmtime::component::{HasData, Linker, ResourceTable};1718pub(crate) struct WasiHttp;1920impl HasData for WasiHttp {21type Data<'a> = WasiHttpCtxView<'a>;22}2324#[derive(Clone, Default)]25pub struct WasiHttpCtx {}2627pub struct WasiHttpCtxView<'a> {28pub ctx: &'a mut WasiHttpCtx,29pub table: &'a mut ResourceTable,30}3132pub trait WasiHttpView: Send {33fn http(&mut self) -> WasiHttpCtxView<'_>;34}3536/// Add all interfaces from this module into the `linker` provided.37///38/// This function will add all interfaces implemented by this module to the39/// [`Linker`], which corresponds to the `wasi:http/imports` world supported by40/// this module.41///42/// # Example43///44/// ```45/// use wasmtime::{Engine, Result, Store, Config};46/// use wasmtime::component::{Linker, ResourceTable};47/// use wasmtime_wasi_http::p3::{WasiHttpCtx, WasiHttpCtxView, WasiHttpView};48///49/// fn main() -> Result<()> {50/// let mut config = Config::new();51/// config.async_support(true);52/// config.wasm_component_model_async(true);53/// let engine = Engine::new(&config)?;54///55/// let mut linker = Linker::<MyState>::new(&engine);56/// wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;57/// // ... add any further functionality to `linker` if desired ...58///59/// let mut store = Store::new(60/// &engine,61/// MyState::default(),62/// );63///64/// // ... use `linker` to instantiate within `store` ...65///66/// Ok(())67/// }68///69/// #[derive(Default)]70/// struct MyState {71/// http: WasiHttpCtx,72/// table: ResourceTable,73/// }74///75/// impl WasiHttpView for MyState {76/// fn http(&mut self) -> WasiHttpCtxView<'_> {77/// WasiHttpCtxView {78/// ctx: &mut self.http,79/// table: &mut self.table,80/// }81/// }82/// }83/// ```84pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>85where86T: WasiHttpView + 'static,87{88handler::add_to_linker::<_, WasiHttp>(linker, T::http)?;89types::add_to_linker::<_, WasiHttp>(linker, T::http)?;90Ok(())91}929394