Added AsyncWrite + AsyncRead support to Stream

Fortune for transfer's current commit: Small blessing − 小吉
master
Avril 2 years ago
parent 596788f2ad
commit 14bd5df498
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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<io::Result<Stream>>
{
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<Result<usize, io::Error>> {
self.writer_pinned_mut().poll_write(cx, buf)
}
#[inline] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.writer_pinned_mut().poll_flush(cx)
}
#[inline] fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
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<io::Result<()>> {
self.reader_pinned_mut().poll_read(cx, buf)
}
}

Loading…
Cancel
Save