Stream: general

Topic: Wit file


view this post on Zulip celine santosh (Apr 21 2025 at 16:15):

I want to convert a crate into wasip2 and want to export a function which is not in lib.rs but another file. How can I export that function through wit. What I understood is I can only export the functions from lib.rs . How can I export a function which is not in lib.rs but another file

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

view this post on Zulip celine santosh (Apr 24 2025 at 18:47):

HI
Just rephrasing it incase its not clear.
I’m turning an existing Rust library into a WASI-Preview 2 component with cargo build --target wasm32-wasip2.
I’d like to expose one specific API function that lives in src/digest.rsnot in src/lib.rs.

How can I write wit file so that I can export that api which is in src/digest.rs.
To be more clear , I am converting this library https://github.com/RustCrypto/traits/tree/master/digest into wasip2 component and I want to export finalize_reset() of https://github.com/RustCrypto/traits/blob/master/digest/src/digest.rs.
How can I export this using wit?

Collection of cryptography-related traits. Contribute to RustCrypto/traits development by creating an account on GitHub.
Collection of cryptography-related traits. Contribute to RustCrypto/traits development by creating an account on GitHub.

view this post on Zulip Chris Fallin (Apr 24 2025 at 18:57):

This is a Rust programming question, I think, not specifically a wit question. You'll want to refer to the exported functions or types using their module path, e.g. digest::foo rather than foo. Or if you prefer, alias them into the toplevel namespace with use digest::foo;.

view this post on Zulip celine santosh (Apr 24 2025 at 21:32):

Yes, but for converting to wasip2 target, I need to write a wit file which exports the function I want from digest.rs, and according to the rust bindings generated, this exported function should have implementation in lib.rs inside guest trait implementation https://component-model.bytecodealliance.org/language-support/rust.html.
From the docs I can only see how we can build a component from scratch. But I want to convert an existing library into wasip2 and export an api. I think this will have modification inside the library.

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

view this post on Zulip Chris Fallin (Apr 24 2025 at 21:35):

That is a tutorial walkthrough that instructs you to place code in lib.rs as an example. It is in no way a prescriptive document that says you must put code only in lib.rs. Did you try what I suggested?

view this post on Zulip celine santosh (Apr 25 2025 at 17:04):

Actually I want to convert this library into wasip2 target and plug it with another component. In that case I should write a wit for this and export the function right.
But also this function finalise_reset() is inside a trait implementation so I don't know how far its possible to export that function.

view this post on Zulip Pat Hickey (Apr 25 2025 at 17:06):

https://github.com/bytecodealliance/wit-bindgen?tab=readme-ov-file#guest-rust

A language binding generator for WebAssembly interface types - bytecodealliance/wit-bindgen

view this post on Zulip Pat Hickey (Apr 25 2025 at 17:06):

to implement an export function you'll need to follow the pattern here.

view this post on Zulip celine santosh (Apr 25 2025 at 17:32):

Thanks for the reply, I understood this, this is for creating a component from scratch right?

I want to convert and export function from existing rust crate.

https://github.com/RustCrypto/traits/blob/master/digest/src/digest.rs
Any idea how I can export implementation of finalize_reset() using wit?
This function is inside implementation block
impl<D: FixedOutput + Default + Update + HashMarker> Digest for D

If it's a standalone function I know how to export. But since its inside this implementation block I am confused how to export this and write wit for this.

Collection of cryptography-related traits. Contribute to RustCrypto/traits development by creating an account on GitHub.

view this post on Zulip Pat Hickey (Apr 25 2025 at 17:33):

you'll need to take that rust crate and compile it to a component, and bindgen creates the glue code you need to do that

view this post on Zulip Pat Hickey (Apr 25 2025 at 17:33):

you'll need to take that boilerplate, and have it refer to a particular value that impls Digest

view this post on Zulip Pat Hickey (Apr 25 2025 at 17:34):

Digest on its own is just an interface, its impossible to execute without a given concrete implementation.

view this post on Zulip celine santosh (Apr 26 2025 at 12:14):

Pat Hickey said:

you'll need to take that rust crate and compile it to a component, and bindgen creates the glue code you need to do that

bindgen creates the bindings based on the wit file I write right? I am stuck how to write wit to export that function.

view this post on Zulip Chris Fallin (Apr 26 2025 at 22:53):

Pat is right -- this is a generic interface (a set of traits). There is a provided impl for Digest for types that implement lower-level traits (Update, HashMarker, etc) but in the end, there still needs to be a concrete implementation. Presumably you'll want to either pick or implement some digest yourself and then export that. You can't export just a trait on its own -- what code would the exported functions execute?


Last updated: Dec 06 2025 at 05:03 UTC