Hi
I am trying to create a component using cargo component. In my lib.rs I have a function which I export with signature
fn compute_result(&mut self, out: &mut [u8])
I am confused how to write this in wit. Self comes in Resource I think . I don't have resources.
Also how to write the second parameter which is array.
Here is an example I'm working on:
https://github.com/cdmurph32/c2pa-component/blob/v24/wit/cai.wit
It has a couple of resources. The self is implied.
Thanks.
What about the second parameter which is an array.
I am not able to write the corresponding wit for it. I can only see list<u8> which is vec in rust.
That might be best implemented as an output stream depending on how big you intend it to be. Is there a reason it needs to be a pointer to an array?
In your implementation on either side of the wit interface you can have an array.
This is my implementation in lib.rs
fn result(&mut self, out: &mut [u8]) {
let hash = self.hasher.finalize_reset();
copy_slices!(hash.as_slice(), out)
}
It's a function in snow framework. I want to override this function, so I am trying to implement the same thing with same function signature.
So I was stuck at a point where I am writing the wit for it, because of second parameter. It's slice , sorry I thought it was an array.
The component model is designed around "shared-nothing" composition, meaning components do not have access to each others' memory. Consequently, WIT interfaces do not support passing pointers, and a slice is basically just a fat pointer. The callee couldn't do anything with such a pointer, since it's relative to a memory it doesn't have access to.
If you need to return a list of bytes to the caller, you'll want to model that as %result: func() -> list<u8>
(which translates to fn result(&self) -> Vec<u8>;
in Rust). The host will take care of copying the contents of that list<u8>
from the callee's memory to the caller's memory.
Thanks
So even if I compose both wasm components together with wac plug , both components will have isolated memory right?
Yes, correct. You also have the option of "shared-everything" module linking using wasm-tools component link
if you prefer. This document describes the various way static libraries, modules, and components can be composed.
Last updated: Apr 07 2025 at 20:03 UTC