Stream: wit-bindgen

Topic: How to pass pre-opened file to wasm component?


view this post on Zulip mainrs (May 13 2025 at 17:48):

I am creating a custom wit world for my components. These components take a file as input and do some calculations, returning the result. My current design was using descriptor as the module's function argument:

world myworld {
    interface calc {
        use wasi:filesystem@0.2.5.preopen.{descriptor};
        calculate: func(file: descriptor) -> s64;
    }
}

However, I noticed that the descriptor resource does not implement std::io::{Read, Seek}, which I require.

Is there any way to make this work besides relying on std::fs::File and reading the file inside the component ? That would require me to provide the input at a static location, for example /input.bin.

Thank you!

view this post on Zulip Pat Hickey (May 13 2025 at 17:50):

wit-bindgen just knows how to create rust bindings to wit interfaces and types, it doesnt know how to map anything to the types in std.

view this post on Zulip Pat Hickey (May 13 2025 at 17:51):

wit-bindgen will create a struct Descriptor and that should have methods on it such as read_via_stream and write_via_stream

view this post on Zulip Pat Hickey (May 13 2025 at 17:52):

heres what wit-bindgen produces on just the wasi wits, in the wasi crate: https://docs.rs/wasi/latest/wasi/filesystem/types/struct.Descriptor.html

view this post on Zulip Pat Hickey (May 13 2025 at 17:54):

if you need std::io::Read you can impl that on https://docs.rs/wasi/latest/wasi/filesystem/types/type.InputStream.html using blocking_read.

view this post on Zulip Pat Hickey (May 13 2025 at 17:55):

if you need Seek, you'll have to make your own struct that contains a Descriptor and the offset, track changes to the offset as you seek or read, and open a new stream when you need to read at a new offset.

view this post on Zulip Pat Hickey (May 13 2025 at 17:55):

one day it may be easier to do all of that with std, but right now std isnt using Descriptors under the hood, so unfortunately you have to wire some of this up yourself.

view this post on Zulip Pat Hickey (May 13 2025 at 17:56):

this is a very sketchy overview, happy to dive more into the details of any of these points as you need.


Last updated: Dec 06 2025 at 07:03 UTC