io: fix unsound pin projection in read_buf and write_buf (#2612)

This commit is contained in:
Taiki Endo 2020-06-12 14:28:23 +09:00 committed by GitHub
parent 1636910f0a
commit 1769f65d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 18 deletions

View File

@ -221,7 +221,7 @@ cfg_io_util! {
/// ```
fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
where
Self: Sized,
Self: Sized + Unpin,
B: BufMut,
{
read_buf(self, buf)

View File

@ -180,7 +180,7 @@ cfg_io_util! {
/// ```
fn write_buf<'a, B>(&'a mut self, src: &'a mut B) -> WriteBuf<'a, Self, B>
where
Self: Sized,
Self: Sized + Unpin,
B: Buf,
{
write_buf(self, src)

View File

@ -8,7 +8,7 @@ use std::task::{Context, Poll};
pub(crate) fn read_buf<'a, R, B>(reader: &'a mut R, buf: &'a mut B) -> ReadBuf<'a, R, B>
where
R: AsyncRead,
R: AsyncRead + Unpin,
B: BufMut,
{
ReadBuf { reader, buf }
@ -26,16 +26,13 @@ cfg_io_util! {
impl<R, B> Future for ReadBuf<'_, R, B>
where
R: AsyncRead,
R: AsyncRead + Unpin,
B: BufMut,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
// safety: no data is moved from self
unsafe {
let me = self.get_unchecked_mut();
Pin::new_unchecked(&mut *me.reader).poll_read_buf(cx, &mut me.buf)
}
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
let me = &mut *self;
Pin::new(&mut *me.reader).poll_read_buf(cx, me.buf)
}
}

View File

@ -20,7 +20,7 @@ cfg_io_util! {
/// asynchronous manner, returning a future.
pub(crate) fn write_buf<'a, W, B>(writer: &'a mut W, buf: &'a mut B) -> WriteBuf<'a, W, B>
where
W: AsyncWrite,
W: AsyncWrite + Unpin,
B: Buf,
{
WriteBuf { writer, buf }
@ -28,16 +28,13 @@ where
impl<W, B> Future for WriteBuf<'_, W, B>
where
W: AsyncWrite,
W: AsyncWrite + Unpin,
B: Buf,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
// safety: no data is moved from self
unsafe {
let me = self.get_unchecked_mut();
Pin::new_unchecked(&mut *me.writer).poll_write_buf(cx, &mut me.buf)
}
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
let me = &mut *self;
Pin::new(&mut *me.writer).poll_write_buf(cx, me.buf)
}
}