Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 2a88a42

Browse files
committed
fix doc tests; address review feedback
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent a29bb6c commit 2a88a42

5 files changed

Lines changed: 73 additions & 33 deletions

File tree

crates/wasi-http/src/p3/proxy.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Context as _;
22
use futures::FutureExt;
3-
use std::future::{self, Future};
3+
use std::future::Future;
44
use std::pin::Pin;
55
use wasmtime::component::Resource;
66
use wasmtime::AsContextMut;
@@ -16,27 +16,26 @@ impl Proxy {
1616
&self,
1717
mut store: S,
1818
req: R,
19-
) -> Pin<
20-
Box<
21-
dyn Future<Output = wasmtime::Result<Result<Resource<Response>, ErrorCode>>>
22-
+ Send
23-
+ 'static,
19+
) -> wasmtime::Result<
20+
Pin<
21+
Box<
22+
dyn Future<Output = wasmtime::Result<Result<Resource<Response>, ErrorCode>>>
23+
+ Send
24+
+ 'static,
25+
>,
2426
>,
2527
>
2628
where
2729
T: ResourceView + Send,
2830
{
2931
let mut store = store.as_context_mut();
3032
let table = store.data_mut().table();
31-
match table
33+
let req = table
3234
.push(req.into())
33-
.context("failed to push request to table")
34-
{
35-
Ok(req) => self
36-
.wasi_http_handler()
37-
.call_handle(&mut store, req)
38-
.boxed(),
39-
Err(e) => future::ready(Err(e)).boxed(),
40-
}
35+
.context("failed to push request to table")?;
36+
Ok(self
37+
.wasi_http_handler()
38+
.call_handle(&mut store, req)
39+
.boxed())
4140
}
4241
}

crates/wasi-http/tests/all/p3/incoming.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async fn run_wasi_http<E: Into<ErrorCode> + 'static>(
3939
wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;
4040
let instance = linker.instantiate_async(&mut store, &component).await?;
4141
let proxy = Proxy::new(&mut store, &instance)?;
42-
let handle = proxy.handle(&mut store, req);
42+
let handle = proxy.handle(&mut store, req)?;
4343
let res = match instance.run(&mut store, handle).await?? {
4444
Ok(res) => res,
4545
Err(err) => return Ok(Err(Some(err))),

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,9 +2314,6 @@ impl Instance {
23142314
/// #
23152315
/// # async fn foo() -> Result<()> {
23162316
/// # let mut config = Config::new();
2317-
/// # config.wasm_component_model(true);
2318-
/// # config.wasm_component_model_async(true);
2319-
/// # config.async_support(true);
23202317
/// # let engine = Engine::new(&config)?;
23212318
/// # let mut store = Store::new(&engine, ());
23222319
/// # let mut linker = Linker::new(&engine);
@@ -2336,7 +2333,7 @@ impl Instance {
23362333
/// // let (future,) = call.await?;
23372334
///
23382335
/// // OK, since we use `Instance::run` to poll `call` inside the event loop:
2339-
/// let (future,) = instance.run(&mut store, call).await?;
2336+
/// let (future,) = instance.run(&mut store, call).await??;
23402337
///
23412338
/// let future = future.into_reader(&mut store);
23422339
///
@@ -2386,28 +2383,72 @@ impl Instance {
23862383
/// futures passed to `run`.
23872384
///
23882385
/// ```
2386+
/// # use {
2387+
/// # anyhow::Result,
2388+
/// # wasmtime::{
2389+
/// # component::{ Component, Linker, Resource, ResourceTable },
2390+
/// # Config, Engine, Store
2391+
/// # },
2392+
/// # };
2393+
/// #
2394+
/// # struct MyResource(u32);
2395+
/// # struct Ctx { table: ResourceTable }
2396+
/// #
2397+
/// # async fn foo() -> Result<()> {
2398+
/// # let mut config = Config::new();
2399+
/// # let engine = Engine::new(&config)?;
2400+
/// # let mut store = Store::new(&engine, Ctx { table: ResourceTable::new() });
2401+
/// # let mut linker = Linker::new(&engine);
2402+
/// # let component = Component::new(&engine, "")?;
2403+
/// # let instance = linker.instantiate_async(&mut store, &component).await?;
2404+
/// # let foo = instance.get_typed_func::<(Resource<MyResource>,), (Resource<MyResource>,)>(&mut store, "foo")?;
2405+
/// # let bar = instance.get_typed_func::<(u32,), ()>(&mut store, "bar")?;
23892406
/// let resource = store.data_mut().table.push(MyResource(42))?;
23902407
/// let call = foo.call_concurrent(&mut store, (resource,));
2391-
/// let (another_resource,) = instance.run(&mut store, call).await?;
2408+
/// let (another_resource,) = instance.run(&mut store, call).await??;
23922409
/// let value = store.data_mut().table.delete(another_resource)?;
23932410
/// let call = bar.call_concurrent(&mut store, (value.0,));
2394-
/// instance.run(&mut store, call).await?;
2411+
/// instance.run(&mut store, call).await??;
2412+
/// # Ok(())
2413+
/// # }
23952414
/// ```
23962415
///
23972416
/// - Call `run_with` once and use `Accessor::with` to access the store from
23982417
/// within the future.
23992418
///
24002419
/// ```
2401-
/// instance.run_with(&mut store, Box::pin(|accessor| async move {
2402-
/// let another_resource = accessor.with(|store| {
2403-
/// let resource = store.data_mut().table.push(MyResource(42))?;
2404-
/// Ok(foo.call_concurrent(store, (resource,)))
2420+
/// # use {
2421+
/// # anyhow::{Result, Error},
2422+
/// # wasmtime::{
2423+
/// # component::{ Component, Linker, Resource, ResourceTable, Accessor, Access },
2424+
/// # Config, Engine, Store
2425+
/// # },
2426+
/// # };
2427+
/// #
2428+
/// # struct MyResource(u32);
2429+
/// # struct Ctx { table: ResourceTable }
2430+
/// #
2431+
/// # async fn foo() -> Result<()> {
2432+
/// # let mut config = Config::new();
2433+
/// # let engine = Engine::new(&config)?;
2434+
/// # let mut store = Store::new(&engine, Ctx { table: ResourceTable::new() });
2435+
/// # let mut linker = Linker::new(&engine);
2436+
/// # let component = Component::new(&engine, "")?;
2437+
/// # let instance = linker.instantiate_async(&mut store, &component).await?;
2438+
/// # let foo = instance.get_typed_func::<(Resource<MyResource>,), (Resource<MyResource>,)>(&mut store, "foo")?;
2439+
/// # let bar = instance.get_typed_func::<(u32,), ()>(&mut store, "bar")?;
2440+
/// instance.run_with(&mut store, move |accessor: &mut Accessor<_, _>| Box::pin(async move {
2441+
/// let (another_resource,) = accessor.with(|mut access: Access<Ctx, Ctx>| {
2442+
/// let resource = access.table.push(MyResource(42))?;
2443+
/// Ok::<_, Error>(foo.call_concurrent(access, (resource,)))
24052444
/// })?.await?;
2406-
/// accessor.with(|store| {
2407-
/// let value = store.data_mut().table.delete(another_resource)?;
2408-
/// Ok(bar.call_concurrent(store, (value.0,)))
2445+
/// accessor.with(|mut access: Access<Ctx, Ctx>| {
2446+
/// let value = access.table.delete(another_resource)?;
2447+
/// Ok::<_, Error>(bar.call_concurrent(access, (value.0,)))
24092448
/// })?.await
2410-
/// })).await?;
2449+
/// })).await??;
2450+
/// # Ok(())
2451+
/// # }
24112452
/// ```
24122453
pub async fn run_with<U: Send, V: Send + Sync + 'static, F>(
24132454
&self,

crates/wasmtime/src/runtime/component/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ mod values;
116116
pub use self::component::{Component, ComponentExportIndex};
117117
#[cfg(feature = "component-model-async")]
118118
pub use self::concurrent::{
119-
AbortOnDropHandle, Accessor, AccessorTask, ErrorContext, FutureReader, FutureWriter,
119+
AbortOnDropHandle, Access, Accessor, AccessorTask, ErrorContext, FutureReader, FutureWriter,
120120
HostFuture, HostStream, ReadBuffer, StreamReader, StreamWriter, VMComponentAsyncStore,
121121
VecBuffer, Watch, WriteBuffer,
122122
};

src/commands/serve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ impl ServeCommand {
592592
)?;
593593
let (req, body) = req.into_parts();
594594
let body = body.map_err(wasmtime_wasi_http::p3::bindings::http::types::ErrorCode::from_hyper_request_error);
595-
let handle =
596-
proxy.handle(&mut store, http::Request::from_parts(req, body));
595+
let handle = proxy
596+
.handle(&mut store, http::Request::from_parts(req, body))?;
597597
let res = instance.run(&mut store, handle).await???;
598598
let (res, tx, io) =
599599
wasmtime_wasi_http::p3::Response::resource_into_http(

0 commit comments

Comments
 (0)