|
| 1 | +use std::sync::atomic::AtomicU32; |
| 2 | + |
1 | 3 | mod bindings { |
2 | 4 | use super::Component; |
3 | 5 | wit_bindgen::generate!({ |
4 | 6 | world: "stream-tx", |
| 7 | + with: { |
| 8 | + "jco:test-components/get-stream-async/example-guest-resource": generate, |
| 9 | + } |
5 | 10 | }); |
6 | 11 | export!(Component); |
7 | 12 | } |
8 | 13 |
|
9 | | -use bindings::wit_stream; |
10 | 14 | use wit_bindgen::StreamReader; |
11 | 15 |
|
12 | | -use crate::bindings::exports::jco::test_components::get_stream_async; |
13 | | -use crate::bindings::wit_stream::StreamPayload; |
| 16 | +use bindings::exports::jco::test_components::get_stream_async; |
| 17 | +use bindings::exports::jco::test_components::get_stream_async::GuestExampleGuestResource; |
| 18 | +use bindings::jco::test_components::resources::ExampleResource; |
| 19 | +use bindings::wit_stream; |
| 20 | +use bindings::wit_stream::StreamPayload; |
14 | 21 |
|
15 | 22 | struct Component; |
16 | 23 |
|
17 | | -// /// Guest-local implementation of `example-resource` |
18 | | -// /// |
19 | | -// /// This resource is returned by component exported fucntions, but note |
20 | | -// /// that is is *distinct* from the resource that is provided by the host |
21 | | -// /// and passed in. |
22 | | -// /// |
23 | | -// /// This component can look and behave and be linked to the external resource |
24 | | -// /// implementation (forwarding calls to it), but it is *not* the same. |
25 | | -// struct ExR(u32); |
26 | | - |
27 | | -// impl get_stream_async::GuestExampleResource for ExR { |
28 | | -// fn new(id: u32) -> Self { |
29 | | -// Self(id) |
30 | | -// } |
31 | | - |
32 | | -// fn get_id(&self) -> u32 { |
33 | | -// return self.0; |
34 | | -// } |
35 | | -// } |
| 24 | +static RESOURCE_DISPOSE_COUNT: AtomicU32 = AtomicU32::new(0); |
| 25 | + |
| 26 | +/// Guest-local implementation of `example-resource` |
| 27 | +/// |
| 28 | +/// This resource is returned by component exported fucntions, but note |
| 29 | +/// that is is *distinct* from the resource that is provided by the host |
| 30 | +/// and passed in. |
| 31 | +/// |
| 32 | +/// This component can look and behave and be linked to the external resource |
| 33 | +/// implementation (forwarding calls to it), but it is *not* the same. |
| 34 | +/// |
| 35 | +pub struct ExR(u32); |
| 36 | + |
| 37 | +impl get_stream_async::GuestExampleGuestResource for ExR { |
| 38 | + fn new(id: u32) -> Self { |
| 39 | + ExR(id) |
| 40 | + } |
| 41 | + |
| 42 | + fn get_id(&self) -> u32 { |
| 43 | + self.0 |
| 44 | + } |
| 45 | + |
| 46 | + async fn get_id_async(&self) -> u32 { |
| 47 | + self.0 |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl Drop for ExR { |
| 52 | + fn drop(&mut self) { |
| 53 | + RESOURCE_DISPOSE_COUNT.fetch_add(1, std::sync::atomic::Ordering::Release); |
| 54 | + } |
| 55 | +} |
36 | 56 |
|
37 | 57 | impl get_stream_async::Guest for Component { |
| 58 | + type ExampleGuestResource = ExR; |
| 59 | + |
38 | 60 | async fn get_stream_u32(vals: Vec<u32>) -> Result<StreamReader<u32>, String> { |
39 | 61 | stream_values_async(vals) |
40 | 62 | } |
@@ -152,18 +174,27 @@ impl get_stream_async::Guest for Component { |
152 | 174 | stream_values_async(vals) |
153 | 175 | } |
154 | 176 |
|
155 | | - // async fn get_stream_example_resource_own( |
156 | | - // vals: Vec<u32>, |
157 | | - // ) -> Result<StreamReader<Self::ExampleResource>, String> { |
158 | | - // let resources: Vec<ExR> = vals |
159 | | - // .iter() |
160 | | - // .map(|v| <ExR as get_stream_async::GuestExampleResource>::new(*v)) |
161 | | - // .collect(); |
| 177 | + // async fn get_stream_example_resource_own(vals: Vec<u32>) -> Result<StreamReader<ExR>, String> { |
| 178 | + // let resources: Vec<ExR> = vals.iter().map(|v| ExR::new(*v)).collect(); |
162 | 179 | // stream_values_async(resources) |
163 | 180 | // } |
164 | 181 |
|
| 182 | + async fn get_stream_example_resource_own( |
| 183 | + vals: Vec<u32>, |
| 184 | + ) -> Result<StreamReader<get_stream_async::ExampleGuestResource>, String> { |
| 185 | + let resources = vals |
| 186 | + .iter() |
| 187 | + .map(|v| get_stream_async::ExampleGuestResource::new(ExR::new(*v))) |
| 188 | + .collect::<Vec<_>>(); |
| 189 | + stream_values_async(resources) |
| 190 | + } |
| 191 | + |
| 192 | + fn get_example_resource_own_disposes() -> u32 { |
| 193 | + RESOURCE_DISPOSE_COUNT.load(std::sync::atomic::Ordering::Acquire) |
| 194 | + } |
| 195 | + |
165 | 196 | async fn get_stream_example_resource_own_attr( |
166 | | - vals: Vec<get_stream_async::ExampleResource>, |
| 197 | + vals: Vec<ExampleResource>, |
167 | 198 | ) -> Result<StreamReader<u32>, String> { |
168 | 199 | let (mut tx, rx) = wit_stream::new(); |
169 | 200 | wit_bindgen::spawn(async move { |
|
0 commit comments