From 7efcab43c95feef0e020a8bac8aa31e70e3aafe2 Mon Sep 17 00:00:00 2001 From: Alphyr <47725341+a1phyr@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:14:22 +0100 Subject: [PATCH] Do not require `Unpin` for some trait impls (#7204) --- tokio/src/io/async_buf_read.rs | 6 +++--- tokio/src/io/async_read.rs | 4 ++-- tokio/src/io/async_seek.rs | 6 +++--- tokio/src/io/async_write.rs | 10 +++++----- tokio/src/util/mod.rs | 8 ++++++++ 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tokio/src/io/async_buf_read.rs b/tokio/src/io/async_buf_read.rs index f235b8081..f5ff88248 100644 --- a/tokio/src/io/async_buf_read.rs +++ b/tokio/src/io/async_buf_read.rs @@ -84,15 +84,15 @@ impl AsyncBufRead for &mut T { impl

AsyncBufRead for Pin

where - P: DerefMut + Unpin, + P: DerefMut, P::Target: AsyncBufRead, { fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - self.get_mut().as_mut().poll_fill_buf(cx) + crate::util::pin_as_deref_mut(self).poll_fill_buf(cx) } fn consume(self: Pin<&mut Self>, amt: usize) { - self.get_mut().as_mut().consume(amt); + crate::util::pin_as_deref_mut(self).consume(amt); } } diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index ca3a435f2..76b93bcb0 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -80,7 +80,7 @@ impl AsyncRead for &mut T { impl

AsyncRead for Pin

where - P: DerefMut + Unpin, + P: DerefMut, P::Target: AsyncRead, { fn poll_read( @@ -88,7 +88,7 @@ where cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll> { - self.get_mut().as_mut().poll_read(cx, buf) + crate::util::pin_as_deref_mut(self).poll_read(cx, buf) } } diff --git a/tokio/src/io/async_seek.rs b/tokio/src/io/async_seek.rs index bd7a992e4..1b57b7890 100644 --- a/tokio/src/io/async_seek.rs +++ b/tokio/src/io/async_seek.rs @@ -68,15 +68,15 @@ impl AsyncSeek for &mut T { impl

AsyncSeek for Pin

where - P: DerefMut + Unpin, + P: DerefMut, P::Target: AsyncSeek, { fn start_seek(self: Pin<&mut Self>, pos: SeekFrom) -> io::Result<()> { - self.get_mut().as_mut().start_seek(pos) + crate::util::pin_as_deref_mut(self).start_seek(pos) } fn poll_complete(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - self.get_mut().as_mut().poll_complete(cx) + crate::util::pin_as_deref_mut(self).poll_complete(cx) } } diff --git a/tokio/src/io/async_write.rs b/tokio/src/io/async_write.rs index 7ec1a302e..df1b0cfe2 100644 --- a/tokio/src/io/async_write.rs +++ b/tokio/src/io/async_write.rs @@ -224,7 +224,7 @@ impl AsyncWrite for &mut T { impl

AsyncWrite for Pin

where - P: DerefMut + Unpin, + P: DerefMut, P::Target: AsyncWrite, { fn poll_write( @@ -232,7 +232,7 @@ where cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { - self.get_mut().as_mut().poll_write(cx, buf) + crate::util::pin_as_deref_mut(self).poll_write(cx, buf) } fn poll_write_vectored( @@ -240,7 +240,7 @@ where cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll> { - self.get_mut().as_mut().poll_write_vectored(cx, bufs) + crate::util::pin_as_deref_mut(self).poll_write_vectored(cx, bufs) } fn is_write_vectored(&self) -> bool { @@ -248,11 +248,11 @@ where } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - self.get_mut().as_mut().poll_flush(cx) + crate::util::pin_as_deref_mut(self).poll_flush(cx) } fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - self.get_mut().as_mut().poll_shutdown(cx) + crate::util::pin_as_deref_mut(self).poll_shutdown(cx) } } diff --git a/tokio/src/util/mod.rs b/tokio/src/util/mod.rs index e585c665c..f41062440 100644 --- a/tokio/src/util/mod.rs +++ b/tokio/src/util/mod.rs @@ -96,3 +96,11 @@ pub(crate) mod cacheline; cfg_io_driver_impl! { pub(crate) mod ptr_expose; } + +use std::{ops::DerefMut, pin::Pin}; + +/// Copy of [`std::pin::Pin::as_deref_mut`]. +// TODO: Remove this once we bump the MSRV to 1.84. +pub(crate) fn pin_as_deref_mut(ptr: Pin<&mut Pin

>) -> Pin<&mut P::Target> { + unsafe { ptr.get_unchecked_mut() }.as_mut() +}