Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/misc/component-async-tests/src/resource_stream.rs
1692 views
1
use crate::util::PipeProducer;
2
use anyhow::Result;
3
use futures::channel::mpsc;
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 {
25
fn foo(&mut self, x: Resource<ResourceStreamX>) -> Result<()> {
26
self.table.get(&x)?;
27
Ok(())
28
}
29
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 {
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
let instance = access.instance();
48
Ok(StreamReader::new(instance, access, PipeProducer::new(rx)))
49
})
50
}
51
}
52
53
impl bindings::local::local::resource_stream::Host for Ctx {}
54
55