12 releases

0.2.2 Mar 27, 2024
0.2.0 Nov 6, 2023
0.1.1 May 31, 2023
0.1.0 Mar 9, 2023
0.0.4 Dec 6, 2021

#460 in Asynchronous

Download history 164/week @ 2023-12-22 16/week @ 2023-12-29 25/week @ 2024-01-05 18/week @ 2024-01-12 26/week @ 2024-01-19 44/week @ 2024-01-26 42/week @ 2024-02-02 88/week @ 2024-02-09 46/week @ 2024-02-16 118/week @ 2024-02-23 65/week @ 2024-03-01 17/week @ 2024-03-08 33/week @ 2024-03-15 158/week @ 2024-03-22 130/week @ 2024-03-29 52/week @ 2024-04-05

374 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

765KB
17K SLoC

Monoio Compat

A compat wrapper for monoio.

Usage example:

use monoio_compat::{AsyncReadExt, AsyncWriteExt, TcpStreamCompat};

#[monoio::main]
async fn main() {
    const ADDRESS: &str = "127.0.0.1:50009";
    let (mut oneshot_tx, mut oneshot_rx) = local_sync::oneshot::channel::<()>();

    let server = async move {
        let listener = monoio::net::TcpListener::bind(ADDRESS).unwrap();
        oneshot_rx.close();
        let (conn, _) = listener.accept().await.unwrap();
        let mut compat_conn = unsafe { TcpStreamCompat::new(conn) };

        let mut buf = [0u8; 10];
        compat_conn.read_exact(&mut buf).await.unwrap();
        buf[0] += 1;
        compat_conn.write_all(&buf).await.unwrap();
    };
    let client = async {
        oneshot_tx.closed().await;
        let conn = monoio::net::TcpStream::connect(ADDRESS).await.unwrap();
        let mut compat_conn = unsafe { TcpStreamCompat::new(conn) };

        let mut buf = [65u8; 10];
        compat_conn.write_all(&buf).await.unwrap();
        compat_conn.read_exact(&mut buf).await.unwrap();
        assert_eq!(buf[0], 66);
    };
    monoio::join!(client, server);
}

Please read the following note before using this crate.

Important Note

Even with this wrapper, TcpStreamCompat is still not fully compatible with the Tokio IO interface.

The user must ensure that once a slice of data is sent using poll_write and poll_read, it will continue to be used until the call returns Ready. Otherwise, the old data will be send.

We implement a simple checking mechanism to ensure that the data is the same between poll_write. But for the sake of performance, we only check the data length, which is not enough. It may cause unsoundness.

For example, running h2 server based on this wrapper will fail. Inside the h2, it will try to send a data frame with poll_write, and if it get Pending, it will assume the data not be sent yet. If there is another data frame with a higher priority, it will poll_write the new frame instead. But the old data frame will be sent with our wrapper.

The core problem is caused by incompatible between poll-like interface and asynchronous system call.

TcpStreamCompat and TcpStreamCompatUnsafe

TcpStreamCompat: Will copy data into owned buffer first, then construct save the future. If user does not follow the rule, it will panic.

TcpStreamCompatUnsafe: Will only save user-provided buffer pointer and length. It will not copy the data, so it is more efficient than TcpStreamCompat. But if user does not follow the rule, it will cause memory corruption.

Dependencies

~3–15MB
~143K SLoC