From 14bd5df498718559d7f5e91265e0abd0e05d0520 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 1 Nov 2021 18:43:24 +0000 Subject: [PATCH] Added AsyncWrite + AsyncRead support to Stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for transfer's current commit: Small blessing − 小吉 --- src/sock.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/sock.rs b/src/sock.rs index 22204a4..e9a70ee 100644 --- a/src/sock.rs +++ b/src/sock.rs @@ -6,6 +6,13 @@ use std::path::{ Path, PathBuf }; use std::{fmt, error}; +use std::{ + task::{Context, Poll}, + pin::Pin, +}; +use tokio::io::{ + AsyncWrite, AsyncRead, +}; #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct SocketAddrUnix @@ -186,11 +193,80 @@ impl Listener } } - // TODO: Add `poll_accept` for unpinned `&self` polling + /// Poll to accept a new incoming connection to this listener + pub fn poll_accept(&self, cx: &mut Context<'_>) -> Poll> + { + match self.0.as_ref() + { + ListenerInner::Unix(un) => un.poll_accept(cx).map_ok(Into::into), + ListenerInner::Tcp(tcp) => tcp.poll_accept(cx).map_ok(Into::into) + } + } } //TODO: impl Stream impl Stream { + #[inline(always)] pub fn reader_ref(&self) -> &(dyn AsyncRead + Unpin + Send + Sync + '_) + { + match self.0.as_ref() + { + StreamInner::Unix(un) => un, + StreamInner::Tcp(tc) => tc, + } + } + #[inline(always)] pub fn writer_ref(&self) -> &(dyn AsyncWrite + Unpin + Send + Sync + '_) + { + match self.0.as_ref() + { + StreamInner::Unix(un) => un, + StreamInner::Tcp(tc) => tc, + } + } + #[inline(always)] pub fn reader_mut(&mut self) -> &mut (dyn AsyncRead + Unpin + Send + Sync + '_) + { + match self.0.as_mut() + { + StreamInner::Unix(un) => un, + StreamInner::Tcp(tc) => tc, + } + } + #[inline(always)] pub fn writer_mut(&mut self) -> &mut (dyn AsyncWrite + Unpin + Send + Sync + '_) + { + match self.0.as_mut() + { + StreamInner::Unix(un) => un, + StreamInner::Tcp(tc) => tc, + } + } + + #[inline(always)] fn reader_pinned_mut(self: Pin<&mut Self>) -> Pin<&mut (dyn AsyncRead + Unpin + Send + Sync + '_)> + { + Pin::new(self.get_mut().reader_mut()) + } + #[inline(always)] fn writer_pinned_mut(self: Pin<&mut Self>) -> Pin<&mut (dyn AsyncWrite + Unpin + Send + Sync + '_)> + { + Pin::new(self.get_mut().writer_mut()) + } +} + +impl AsyncWrite for Stream +{ + #[inline] fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll> { + self.writer_pinned_mut().poll_write(cx, buf) + } + #[inline] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.writer_pinned_mut().poll_flush(cx) + } + #[inline] fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.writer_pinned_mut().poll_shutdown(cx) + } +} + +impl AsyncRead for Stream +{ + #[inline] fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut tokio::io::ReadBuf<'_>) -> Poll> { + self.reader_pinned_mut().poll_read(cx, buf) + } }