From ba071edf2451c4ab14615121a1cf613f3108a04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vavrus=CC=8Ca?= Date: Thu, 1 Aug 2019 23:25:48 -0700 Subject: [PATCH] add support for Read + Write for TcpStream and UnixStream There is no way to convert stream objects to their underlying type, but in some cases it's useful to be able to perform blocking I/O, such as when wrapped in stream that handles WouldBlock error in some way. --- src/tcp/stream.rs | 15 +++++++++++++++ src/uds/stream.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/tcp/stream.rs b/src/tcp/stream.rs index d9a4e1e7..a40384e5 100644 --- a/src/tcp/stream.rs +++ b/src/tcp/stream.rs @@ -525,6 +525,21 @@ impl AsyncWriteReady for TcpStream { } } +impl io::Read for TcpStream { + fn read(&mut self, buf: &mut [u8]) -> Result { + self.io.get_mut().read(buf) + } +} + +impl io::Write for TcpStream { + fn write(&mut self, buf: &[u8]) -> Result { + self.io.get_mut().write(buf) + } + fn flush(&mut self) -> Result<(), io::Error> { + self.io.get_mut().flush() + } +} + impl fmt::Debug for TcpStream { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.get_ref().fmt(f) diff --git a/src/uds/stream.rs b/src/uds/stream.rs index 0da94e37..14c166a8 100644 --- a/src/uds/stream.rs +++ b/src/uds/stream.rs @@ -248,6 +248,21 @@ impl TakeError for UnixStream { } } +impl io::Read for UnixStream { + fn read(&mut self, buf: &mut [u8]) -> Result { + self.io.get_mut().read(buf) + } +} + +impl io::Write for UnixStream { + fn write(&mut self, buf: &[u8]) -> Result { + self.io.get_mut().write(buf) + } + fn flush(&mut self) -> Result<(), io::Error> { + self.io.get_mut().flush() + } +} + impl fmt::Debug for UnixStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.io.get_ref().fmt(f)