Search code examples
rustrust-tokio

How to access the read buffer of tokio::io::BufStream?


I recognized that tokio::io::BufReader has a buffer() method for accessing its internal read buffer. However I cannot find such an interface for tokio::io::BufStream, nor can I access the internal BufReader of a BufStream by using the public API.

I wonder if this is by design, or if there is some other way to do it?

BTW, the following is my use case:

I want to implement a traffic dispatcher for a socket with the trait AsyncRead + AsyncWrite. The dispatcher will try to peek the first read of the underlying socket to determine where the traffic will be routed, and then return a BufStream that also supports AsyncRead + AsyncWrite, as if the original socket is untouched (no data consumed). My plan is to first trigger a fill_buf() call to the BufStream, then to look at the internal read buffer (which I don't how to do).


Solution

  • I don't think this is an inherent limitation: BufStream is just BufReader<BufWriter<RW>>. You can do it yourself if you need and then you can access the buffers, but there is no need to: fill_buf() returns the filled buffer, so you can just use its returned value.