I have a function whose signature is this:
pub fn handle_decode(buf: &mut bytes::BytesMut) -> anyhow::Result<DataType> {}
Which is to be shared between routines which handle different network transfer protocols. The reason I'm using the bytes package is because it nicely handles things like endianness and shallow copies. It's very nice for picking off bytes in the right order and "cloning" them cheaply.
With that being said, in the TCP routine I'm looking to use a BufReader<TcpStream> to combat the overhead of repeated syscalls to read() when picking data off the stream.
Is there any way to use a bytes::BufMut in conjunction with a buffered reader to get the best of both worlds?
Minimum example:
use anyhow::Context;
use std::io::BufReader;
use std::net::TcpListener;
fn bind_accept() -> anyhow::Result<()> {
fn handle_decode(buf: &mut bytes::BytesMut) -> anyhow::Result<()> {
unimplemented!()
}
let mut l = TcpListener::bind("1234")?;
let (sock, _) = l.accept().context("should bind to provided port")?;
let bufreader = BufReader::new(sock);
let mut buf = bytes::BytesMut::with_capacity(1024);
// This line will not work in this current state
handle_decode(&mut bufreader)
}
BytesMutalready buffer its content, there is no need for another buffer. You either useBytesMutorBufReadernot both.Using the trait
BufMutyou can usewriterthis will return you a wrapper that implement Write from std. Then you need to implement yourself what doesBufReaderof std, I didn't find anything that already do it for you. Or you could prefer use theBufMuttrait instead of std trait directly.