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
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.rs, not 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?
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;.
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.
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?
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.
https://github.com/bytecodealliance/wit-bindgen?tab=readme-ov-file#guest-rust
to implement an export function you'll need to follow the pattern here.
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.
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
you'll need to take that boilerplate, and have it refer to a particular value that impls Digest
Digest on its own is just an interface, its impossible to execute without a given concrete implementation.
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.
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