Skip to content

Is there any struct/trait in quiche that implement trait std::io::Read/Write? #984

Done

Description

@tubzby

I have a TCP server running in production that I'm now trying to migrate to QUIC.

The client/server example is some kind of complicated, I think it will be much helpful if quiche::Connection(or anything else) implement trait std::io::Read/Write, so anyone having some knowledge of TCPStream can easily adopt quiche in their own program.

I know I can wrap it myself, but it will be much convenient if it is right there.

Thanks!

Activity

  1. added
    question
    on Jul 20, 2021
  2. ljluestc commented on Sep 10, 2024

    @ljluestc
    use quiche::Connection;
    use std::io::{self, Read, Write};
    
    // A wrapper to adapt `quiche::Connection` to `std::io::Read`.
    pub struct QuicheRead<'a> {
        conn: &'a mut Connection,
    }
    
    impl<'a> QuicheRead<'a> {
        pub fn new(conn: &'a mut Connection) -> Self {
            QuicheRead { conn }
        }
    }
    
    impl<'a> Read for QuicheRead<'a> {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            // Adapt the quiche connection to read data
            // This is a simplified example, in practice, you might need to handle more cases.
            match self.conn.recv(buf) {
                Ok(len) => Ok(len),
                Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Read error")),
            }
        }
    }
    
    // A wrapper to adapt `quiche::Connection` to `std::io::Write`.
    pub struct QuicheWrite<'a> {
        conn: &'a mut Connection,
    }
    
    impl<'a> QuicheWrite<'a> {
        pub fn new(conn: &'a mut Connection) -> Self {
            QuicheWrite { conn }
        }
    }
    
    impl<'a> Write for QuicheWrite<'a> {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            // Adapt the quiche connection to write data
            // This is a simplified example, in practice, you might need to handle more cases.
            match self.conn.send(buf) {
                Ok(_) => Ok(buf.len()),
                Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Write error")),
            }
        }
    
        fn flush(&mut self) -> io::Result<()> {
            // QUIC doesn't have a direct flush method, but you might want to handle it
            Ok(())
        }
    }
    
    
  3. ghedo commented on Nov 25, 2026

    @ghedo
    Member

    Closing as answered. quiche is a sans-I/O library, so it exposes stream_recv() and stream_send() rather than std::io::Read and Write, which would require quiche to own the socket and timers. tokio-quiche provides an async integration that handles the I/O.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions