Expand description
Buffered Read+BufRead and Write for Rust that does not own the underlying Read/Write
§Example usage
use std::io;
use std::io::ErrorKind;
use std::net::TcpStream;
use std::sync::Mutex;
use unowned_buf::{UnownedReadBuffer, UnownedWriteBuffer};
#[derive(Debug)]
struct DuplexBufferedTcpStream {
stream: TcpStream,
read_buf: Mutex<UnownedReadBuffer<0x4000>>,
write_buf: Mutex<UnownedWriteBuffer<0x4000>>,
}
impl DuplexBufferedTcpStream {
fn new(stream: TcpStream) -> DuplexBufferedTcpStream {
Self {
stream,
read_buf: Mutex::new(UnownedReadBuffer::new()),
write_buf: Mutex::new(UnownedWriteBuffer::new()),
}
}
fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
self.read_buf.try_lock()
.map_err(|_| io::Error::from(ErrorKind::WouldBlock))?
.read(&mut &self.stream, buf)
}
fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.write_buf.try_lock()
.map_err(|_| io::Error::from(ErrorKind::WouldBlock))?
.write(&mut &self.stream, buf)
}
fn flush(&self) -> io::Result<()> {
self.write_buf.try_lock()
.map_err(|_| io::Error::from(ErrorKind::WouldBlock))?
.flush(&mut &self.stream)
}
//Add other fn delegates from BufRead, Read or Write as needed or implement the traits for these directly.
//Or add set/get timeout fns that delete to the TcpStream.
}
Structs§
- Borrowed
Read Buffer - Borrowed dyn Read/ReadBuf of a
UnownedReadBuffer
. This borrowed version is directly associated with aRead
impl, but is subject to lifetimes. - Borrowed
Write Buffer - Borrowed dyn Write of a
UnownedWriteBuffer
. This borrowed version is directly associated with a Write impl, but is subject to lifetimes. - Unowned
Read Buffer - Unowned Read buffer.
- Unowned
Write Buffer - Unowned Write buffer.