1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 16:19:12 -05:00

Remove unnecessary use of Pin, use ready! (#3576)

This commit is contained in:
Gurwinder Singh 2020-01-02 23:41:33 +05:30 committed by Ry Dahl
parent 5cf2eb7d18
commit b0761138e5

View file

@ -7,14 +7,12 @@ use crate::state::ThreadSafeState;
use deno::ErrBox; use deno::ErrBox;
use deno::Resource; use deno::Resource;
use deno::*; use deno::*;
use futures;
use futures::future::FutureExt; use futures::future::FutureExt;
use std; use futures::ready;
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::task::Context; use std::task::Context;
use std::task::Poll; use std::task::Poll;
use tokio;
use tokio::io::{AsyncRead, AsyncWrite}; use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio_rustls::client::TlsStream as ClientTlsStream; use tokio_rustls::client::TlsStream as ClientTlsStream;
@ -94,7 +92,7 @@ impl Resource for StreamResource {}
/// but uses an `ErrBox` error instead of `std::io:Error` /// but uses an `ErrBox` error instead of `std::io:Error`
pub trait DenoAsyncRead { pub trait DenoAsyncRead {
fn poll_read( fn poll_read(
self: Pin<&mut Self>, &mut self,
cx: &mut Context, cx: &mut Context,
buf: &mut [u8], buf: &mut [u8],
) -> Poll<Result<usize, ErrBox>>; ) -> Poll<Result<usize, ErrBox>>;
@ -102,32 +100,25 @@ pub trait DenoAsyncRead {
impl DenoAsyncRead for StreamResource { impl DenoAsyncRead for StreamResource {
fn poll_read( fn poll_read(
self: Pin<&mut Self>, &mut self,
cx: &mut Context, cx: &mut Context,
buf: &mut [u8], buf: &mut [u8],
) -> Poll<Result<usize, ErrBox>> { ) -> Poll<Result<usize, ErrBox>> {
let inner = self.get_mut(); use StreamResource::*;
let mut f: Box<dyn AsyncRead + Unpin> = match inner { let mut f: Pin<Box<dyn AsyncRead>> = match self {
StreamResource::FsFile(f) => Box::new(f), FsFile(f) => Box::pin(f),
StreamResource::Stdin(f) => Box::new(f), Stdin(f) => Box::pin(f),
StreamResource::TcpStream(f) => Box::new(f), TcpStream(f) => Box::pin(f),
StreamResource::ClientTlsStream(f) => Box::new(f), ClientTlsStream(f) => Box::pin(f),
StreamResource::ServerTlsStream(f) => Box::new(f), ServerTlsStream(f) => Box::pin(f),
StreamResource::ChildStdout(f) => Box::new(f), ChildStdout(f) => Box::pin(f),
StreamResource::ChildStderr(f) => Box::new(f), ChildStderr(f) => Box::pin(f),
StreamResource::HttpBody(f) => Box::new(f), HttpBody(f) => Box::pin(f),
_ => { _ => return Err(bad_resource()).into(),
return Poll::Ready(Err(bad_resource()));
}
}; };
let r = AsyncRead::poll_read(Pin::new(&mut f), cx, buf); let v = ready!(f.as_mut().poll_read(cx, buf))?;
Ok(v).into()
match r {
Poll::Ready(Err(e)) => Poll::Ready(Err(ErrBox::from(e))),
Poll::Ready(Ok(v)) => Poll::Ready(Ok(v)),
Poll::Pending => Poll::Pending,
}
} }
} }
@ -182,15 +173,7 @@ where
let resource = table let resource = table
.get_mut::<StreamResource>(inner.rid) .get_mut::<StreamResource>(inner.rid)
.ok_or_else(bad_resource)?; .ok_or_else(bad_resource)?;
let nread = match DenoAsyncRead::poll_read( let nread = ready!(resource.poll_read(cx, &mut inner.buf.as_mut()[..]))?;
Pin::new(resource),
cx,
&mut inner.buf.as_mut()[..],
) {
Poll::Ready(Ok(v)) => v,
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Pending => return Poll::Pending,
};
inner.io_state = IoState::Done; inner.io_state = IoState::Done;
Poll::Ready(Ok(nread as i32)) Poll::Ready(Ok(nread as i32))
} }
@ -218,82 +201,56 @@ pub fn op_read(
/// but uses an `ErrBox` error instead of `std::io:Error` /// but uses an `ErrBox` error instead of `std::io:Error`
pub trait DenoAsyncWrite { pub trait DenoAsyncWrite {
fn poll_write( fn poll_write(
self: Pin<&mut Self>, &mut self,
cx: &mut Context, cx: &mut Context,
buf: &[u8], buf: &[u8],
) -> Poll<Result<usize, ErrBox>>; ) -> Poll<Result<usize, ErrBox>>;
fn poll_close( fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>>;
self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Result<(), ErrBox>>;
fn poll_flush( fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>>;
self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Result<(), ErrBox>>;
} }
impl DenoAsyncWrite for StreamResource { impl DenoAsyncWrite for StreamResource {
fn poll_write( fn poll_write(
self: Pin<&mut Self>, &mut self,
cx: &mut Context, cx: &mut Context,
buf: &[u8], buf: &[u8],
) -> Poll<Result<usize, ErrBox>> { ) -> Poll<Result<usize, ErrBox>> {
let inner = self.get_mut(); use StreamResource::*;
let mut f: Box<dyn AsyncWrite + Unpin> = match inner { let mut f: Pin<Box<dyn AsyncWrite>> = match self {
StreamResource::FsFile(f) => Box::new(f), FsFile(f) => Box::pin(f),
StreamResource::Stdout(f) => Box::new(f), Stdout(f) => Box::pin(f),
StreamResource::Stderr(f) => Box::new(f), Stderr(f) => Box::pin(f),
StreamResource::TcpStream(f) => Box::new(f), TcpStream(f) => Box::pin(f),
StreamResource::ClientTlsStream(f) => Box::new(f), ClientTlsStream(f) => Box::pin(f),
StreamResource::ServerTlsStream(f) => Box::new(f), ServerTlsStream(f) => Box::pin(f),
StreamResource::ChildStdin(f) => Box::new(f), ChildStdin(f) => Box::pin(f),
_ => { _ => return Err(bad_resource()).into(),
return Poll::Ready(Err(bad_resource()));
}
}; };
let r = AsyncWrite::poll_write(Pin::new(&mut f), cx, buf); let v = ready!(f.as_mut().poll_write(cx, buf))?;
Ok(v).into()
match r {
Poll::Ready(Err(e)) => Poll::Ready(Err(ErrBox::from(e))),
Poll::Ready(Ok(v)) => Poll::Ready(Ok(v)),
Poll::Pending => Poll::Pending,
}
} }
fn poll_flush( fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>> {
self: Pin<&mut Self>, use StreamResource::*;
cx: &mut Context, let mut f: Pin<Box<dyn AsyncWrite>> = match self {
) -> Poll<Result<(), ErrBox>> { FsFile(f) => Box::pin(f),
let inner = self.get_mut(); Stdout(f) => Box::pin(f),
let mut f: Box<dyn AsyncWrite + Unpin> = match inner { Stderr(f) => Box::pin(f),
StreamResource::FsFile(f) => Box::new(f), TcpStream(f) => Box::pin(f),
StreamResource::Stdout(f) => Box::new(f), ClientTlsStream(f) => Box::pin(f),
StreamResource::Stderr(f) => Box::new(f), ServerTlsStream(f) => Box::pin(f),
StreamResource::TcpStream(f) => Box::new(f), ChildStdin(f) => Box::pin(f),
StreamResource::ClientTlsStream(f) => Box::new(f), _ => return Err(bad_resource()).into(),
StreamResource::ServerTlsStream(f) => Box::new(f),
StreamResource::ChildStdin(f) => Box::new(f),
_ => {
return Poll::Ready(Err(bad_resource()));
}
}; };
let r = AsyncWrite::poll_flush(Pin::new(&mut f), cx); ready!(f.as_mut().poll_flush(cx))?;
Ok(()).into()
match r {
Poll::Ready(Err(e)) => Poll::Ready(Err(ErrBox::from(e))),
Poll::Ready(Ok(_)) => Poll::Ready(Ok(())),
Poll::Pending => Poll::Pending,
}
} }
fn poll_close( fn poll_close(&mut self, _cx: &mut Context) -> Poll<Result<(), ErrBox>> {
self: Pin<&mut Self>,
_cx: &mut Context,
) -> Poll<Result<(), ErrBox>> {
unimplemented!() unimplemented!()
} }
} }
@ -345,15 +302,7 @@ where
.get_mut::<StreamResource>(inner.rid) .get_mut::<StreamResource>(inner.rid)
.ok_or_else(bad_resource)?; .ok_or_else(bad_resource)?;
let nwritten = match DenoAsyncWrite::poll_write( let nwritten = ready!(resource.poll_write(cx, inner.buf.as_ref()))?;
Pin::new(resource),
cx,
inner.buf.as_ref(),
) {
Poll::Ready(Ok(v)) => v,
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Pending => return Poll::Pending,
};
inner.io_state = IoState::Flush; inner.io_state = IoState::Flush;
inner.nwritten = nwritten as i32; inner.nwritten = nwritten as i32;
} }
@ -367,13 +316,8 @@ where
let resource = table let resource = table
.get_mut::<StreamResource>(inner.rid) .get_mut::<StreamResource>(inner.rid)
.ok_or_else(bad_resource)?; .ok_or_else(bad_resource)?;
match DenoAsyncWrite::poll_flush(Pin::new(resource), cx) { ready!(resource.poll_flush(cx))?;
Poll::Ready(Ok(_)) => { inner.io_state = IoState::Done;
inner.io_state = IoState::Done;
}
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Pending => return Poll::Pending,
};
} }
Poll::Ready(Ok(inner.nwritten)) Poll::Ready(Ok(inner.nwritten))