1 use crate::util::PipeProducer; 2 use futures::channel::mpsc; 3 use wasmtime::Result; 4 use wasmtime::component::{Accessor, Resource, StreamReader}; 5 6 use super::Ctx; 7 8 pub mod bindings { 9 wasmtime::component::bindgen!({ 10 path: "wit", 11 world: "read-resource-stream", 12 with: { 13 "local:local/resource-stream.x": super::ResourceStreamX, 14 }, 15 imports: { 16 "local:local/resource-stream.foo": async | store | trappable, 17 default: trappable, 18 }, 19 }); 20 } 21 22 pub struct ResourceStreamX; 23 24 impl bindings::local::local::resource_stream::HostX for Ctx { foo(&mut self, x: Resource<ResourceStreamX>) -> Result<()>25 fn foo(&mut self, x: Resource<ResourceStreamX>) -> Result<()> { 26 self.table.get(&x)?; 27 Ok(()) 28 } 29 drop(&mut self, x: Resource<ResourceStreamX>) -> Result<()>30 fn drop(&mut self, x: Resource<ResourceStreamX>) -> Result<()> { 31 self.table.delete(x)?; 32 Ok(()) 33 } 34 } 35 36 impl bindings::local::local::resource_stream::HostWithStore for Ctx { foo<T: 'static>( accessor: &Accessor<T, Self>, count: u32, ) -> wasmtime::Result<StreamReader<Resource<ResourceStreamX>>>37 async fn foo<T: 'static>( 38 accessor: &Accessor<T, Self>, 39 count: u32, 40 ) -> wasmtime::Result<StreamReader<Resource<ResourceStreamX>>> { 41 accessor.with(|mut access| { 42 let (mut tx, rx) = mpsc::channel(usize::try_from(count).unwrap()); 43 for _ in 0..count { 44 tx.try_send(access.get().table.push(ResourceStreamX)?) 45 .unwrap() 46 } 47 StreamReader::new(access, PipeProducer::new(rx)) 48 }) 49 } 50 } 51 52 impl bindings::local::local::resource_stream::Host for Ctx {} 53