MCPcopy Create free account
hub / github.com/drogus/crows / wrap_async

Method wrap_async

wasm/src/lib.rs:250–296  ·  view source on GitHub ↗
(
        mut caller: Caller<'a, Self>,
        ptr: u32,
        len: u32,
        f: F,
    )

Source from the content-addressed store, hash-verified

248 }
249
250 pub async fn wrap_async<'a, T, U, F, E>(
251 mut caller: Caller<'a, Self>,
252 ptr: u32,
253 len: u32,
254 f: F,
255 ) -> anyhow::Result<u64>
256 where
257 F: for<'b> FnOnce(
258 &'b mut Caller<'_, Self>,
259 T,
260 ) -> Pin<Box<dyn Future<Output = Result<U, E>> + 'b + Send>>,
261 U: Serialize,
262 E: Serialize,
263 T: DeserializeOwned,
264 {
265 let memory = get_memory(&mut caller)?;
266
267 let slice = memory
268 .data(&caller)
269 .get(ptr as usize..(ptr + len) as usize)
270 .ok_or(anyhow!("Could not get memory slice"))?;
271
272 let arg = from_slice(slice)?;
273
274 let result = f(&mut caller, arg).await;
275
276 let (_, store) = { memory.data_and_store_mut(&mut caller) };
277
278 match result {
279 Ok(ret) => {
280 let encoded = to_vec(&ret)?;
281
282 let length = encoded.len();
283 let index = store.buffers.insert(encoded.into_boxed_slice());
284
285 Ok(create_return_value(0, length as u32, index as u32))
286 }
287 Err(err) => {
288 let encoded = to_vec(&err)?;
289
290 let length = encoded.len();
291 let index = store.buffers.insert(encoded.into_boxed_slice());
292
293 Ok(create_return_value(1, length as u32, index as u32))
294 }
295 }
296 }
297
298 pub fn http<'a>(
299 mut caller: &'a mut Caller<'_, Self>,

Callers

nothing calls this directly

Calls 2

get_memoryFunction · 0.85
create_return_valueFunction · 0.85

Tested by

no test coverage detected