wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Add framing for `TcpSocket`

Open segeljakt opened this issue 11 months ago • 1 comments

Hi, I would like to add framing to WASI TCP streams. Tokio provides this functionality with tokio_util::codec::{FramedRead, FramedWrite}, which relies on tokio::io::{AsyncRead, AsyncWrite}. Would it be possible to implement these traits for wit_bindgen::async_support::{StreamReader, StreamWriter} or TcpSocket?

I think it would look like this:

impl tokio::io::AsyncRead for wit_bindgen::async_support::StreamWriter<u8> {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        ...
    }
}

impl tokio::io::AsyncWrite for wit_bindgen::async_support::StreamWriter<u8> {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<Result<usize, std::io::Error>> {
        ...
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), std::io::Error>> {
        ...
    }

    fn poll_shutdown(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), std::io::Error>> {
        ...
    }
}

segeljakt avatar Apr 07 '25 08:04 segeljakt

This repo is just for raw bindings generation so likely isn't a good spot for implementations such as this. My recommendation for now would be a wrapper type or something like that.

alexcrichton avatar Apr 07 '25 14:04 alexcrichton