Stream: general

Topic: How to write wit ?


view this post on Zulip celine santosh (Mar 26 2025 at 13:32):

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.

List of Rust libraries and applications. An unofficial experimental opinionated alternative to crates.io

view this post on Zulip Colin D Murphy (Mar 26 2025 at 18:12):

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.

WASI P2 component for C2PA. Contribute to cdmurph32/c2pa-component development by creating an account on GitHub.

view this post on Zulip celine santosh (Mar 26 2025 at 18:41):

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.

view this post on Zulip Colin D Murphy (Mar 27 2025 at 13:47):

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?

view this post on Zulip Colin D Murphy (Mar 27 2025 at 13:51):

In your implementation on either side of the wit interface you can have an array.

view this post on Zulip celine santosh (Mar 27 2025 at 14:53):

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.

List of Rust libraries and applications. An unofficial experimental opinionated alternative to crates.io

view this post on Zulip celine santosh (Mar 27 2025 at 14:54):

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.

view this post on Zulip Joel Dice (Mar 27 2025 at 15:39):

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.

view this post on Zulip celine santosh (Mar 30 2025 at 15:20):

Thanks
So even if I compose both wasm components together with wac plug , both components will have isolated memory right?

view this post on Zulip Joel Dice (Mar 31 2025 at 14:41):

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.

Repository for design and specification of the Component Model - WebAssembly/component-model

Last updated: Apr 07 2025 at 20:03 UTC