Reading just parts of buffers in a fifo (VecDeque)

Suppose I have a

packets: VecDeque<Packet>

which is a fifo for tcp packets. That is, a client reads from this deque everytime, gets the oldest buffer, and fills a buffer, like this:


fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        buf.put_slice(self.packets.front().unwrap().internal_slice)
}

there's a problem here. If buf's capacity is smaller than internal_slice, it panics. So we cannot simply rely on getting the front() everytime. What would be the best way to consume part of the VecDeque's oldest buffer?

I thought of getting the front, and if the buffer is too big, only copy part of it and then put it back and update something internally on Packet, but is there a better and less error prone?

This is what you need to do.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.