Path: blob/main/crates/misc/component-async-tests/src/resource_stream.rs
1692 views
use crate::util::PipeProducer;1use anyhow::Result;2use futures::channel::mpsc;3use wasmtime::component::{Accessor, Resource, StreamReader};45use super::Ctx;67pub mod bindings {8wasmtime::component::bindgen!({9path: "wit",10world: "read-resource-stream",11with: {12"local:local/resource-stream/x": super::ResourceStreamX,13},14imports: {15"local:local/resource-stream/foo": async | store | trappable,16default: trappable,17},18});19}2021pub struct ResourceStreamX;2223impl bindings::local::local::resource_stream::HostX for Ctx {24fn foo(&mut self, x: Resource<ResourceStreamX>) -> Result<()> {25self.table.get(&x)?;26Ok(())27}2829fn drop(&mut self, x: Resource<ResourceStreamX>) -> Result<()> {30self.table.delete(x)?;31Ok(())32}33}3435impl bindings::local::local::resource_stream::HostWithStore for Ctx {36async fn foo<T: 'static>(37accessor: &Accessor<T, Self>,38count: u32,39) -> wasmtime::Result<StreamReader<Resource<ResourceStreamX>>> {40accessor.with(|mut access| {41let (mut tx, rx) = mpsc::channel(usize::try_from(count).unwrap());42for _ in 0..count {43tx.try_send(access.get().table.push(ResourceStreamX)?)44.unwrap()45}46let instance = access.instance();47Ok(StreamReader::new(instance, access, PipeProducer::new(rx)))48})49}50}5152impl bindings::local::local::resource_stream::Host for Ctx {}535455