I am looking for a struct that can be shared between two threads. The one thread will use std::io::Write to write data to the buffer while the other thread will use std::io::Read to read the data written by the first thread.
I've been searching for a few hours now, but can't find a crate which currently does this. Am I overlooking any crates?
A lot of other languages use the term "pipe" for this. For example, Go's standard library has the io.Pipe() function which returns a reader and a writer.
The pipe crate seems to provide something similar. Skimming the API docs, it looks like it's implemented by transferring Vec<u8>'s from the sender to the receiver via a channel.
The difference between pipe and ringbuffer seems to be that ringbuffer very deliberately makes the capacity fixed, whereas pipe seems to give you less control over how much memory is used and should enable backpressure (i.e. imagine your reader can't read data as fast as it's being written, so the writer goes to sleep until it's able to write again).
pipe ended up doing it for me. In my case it works better than ringbuf for two reasons:
ringbuf panics when it is full so I have to oversize it
ringbuf's read() sometimes errors with WouldBlock which I would have to work around by manually sleeping to wait for enough data to be written. This seems to be related to how the library that uses the Reader is written.
My goto would have been channels. Unfortunately, I have to connect two libraries (cargo and cargo_metadata) which expect a Write and Read respectively.