Skip to content

Commit 7efcab4

Browse files
authored
Do not require Unpin for some trait impls (#7204)
1 parent e4a39d2 commit 7efcab4

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

tokio/src/io/async_buf_read.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ impl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for &mut T {
8484

8585
impl<P> AsyncBufRead for Pin<P>
8686
where
87-
P: DerefMut + Unpin,
87+
P: DerefMut,
8888
P::Target: AsyncBufRead,
8989
{
9090
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
91-
self.get_mut().as_mut().poll_fill_buf(cx)
91+
crate::util::pin_as_deref_mut(self).poll_fill_buf(cx)
9292
}
9393

9494
fn consume(self: Pin<&mut Self>, amt: usize) {
95-
self.get_mut().as_mut().consume(amt);
95+
crate::util::pin_as_deref_mut(self).consume(amt);
9696
}
9797
}
9898

tokio/src/io/async_read.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ impl<T: ?Sized + AsyncRead + Unpin> AsyncRead for &mut T {
8080

8181
impl<P> AsyncRead for Pin<P>
8282
where
83-
P: DerefMut + Unpin,
83+
P: DerefMut,
8484
P::Target: AsyncRead,
8585
{
8686
fn poll_read(
8787
self: Pin<&mut Self>,
8888
cx: &mut Context<'_>,
8989
buf: &mut ReadBuf<'_>,
9090
) -> Poll<io::Result<()>> {
91-
self.get_mut().as_mut().poll_read(cx, buf)
91+
crate::util::pin_as_deref_mut(self).poll_read(cx, buf)
9292
}
9393
}
9494

tokio/src/io/async_seek.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ impl<T: ?Sized + AsyncSeek + Unpin> AsyncSeek for &mut T {
6868

6969
impl<P> AsyncSeek for Pin<P>
7070
where
71-
P: DerefMut + Unpin,
71+
P: DerefMut,
7272
P::Target: AsyncSeek,
7373
{
7474
fn start_seek(self: Pin<&mut Self>, pos: SeekFrom) -> io::Result<()> {
75-
self.get_mut().as_mut().start_seek(pos)
75+
crate::util::pin_as_deref_mut(self).start_seek(pos)
7676
}
7777

7878
fn poll_complete(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
79-
self.get_mut().as_mut().poll_complete(cx)
79+
crate::util::pin_as_deref_mut(self).poll_complete(cx)
8080
}
8181
}
8282

tokio/src/io/async_write.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -224,35 +224,35 @@ impl<T: ?Sized + AsyncWrite + Unpin> AsyncWrite for &mut T {
224224

225225
impl<P> AsyncWrite for Pin<P>
226226
where
227-
P: DerefMut + Unpin,
227+
P: DerefMut,
228228
P::Target: AsyncWrite,
229229
{
230230
fn poll_write(
231231
self: Pin<&mut Self>,
232232
cx: &mut Context<'_>,
233233
buf: &[u8],
234234
) -> Poll<io::Result<usize>> {
235-
self.get_mut().as_mut().poll_write(cx, buf)
235+
crate::util::pin_as_deref_mut(self).poll_write(cx, buf)
236236
}
237237

238238
fn poll_write_vectored(
239239
self: Pin<&mut Self>,
240240
cx: &mut Context<'_>,
241241
bufs: &[IoSlice<'_>],
242242
) -> Poll<io::Result<usize>> {
243-
self.get_mut().as_mut().poll_write_vectored(cx, bufs)
243+
crate::util::pin_as_deref_mut(self).poll_write_vectored(cx, bufs)
244244
}
245245

246246
fn is_write_vectored(&self) -> bool {
247247
(**self).is_write_vectored()
248248
}
249249

250250
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
251-
self.get_mut().as_mut().poll_flush(cx)
251+
crate::util::pin_as_deref_mut(self).poll_flush(cx)
252252
}
253253

254254
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
255-
self.get_mut().as_mut().poll_shutdown(cx)
255+
crate::util::pin_as_deref_mut(self).poll_shutdown(cx)
256256
}
257257
}
258258

tokio/src/util/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,11 @@ pub(crate) mod cacheline;
9696
cfg_io_driver_impl! {
9797
pub(crate) mod ptr_expose;
9898
}
99+
100+
use std::{ops::DerefMut, pin::Pin};
101+
102+
/// Copy of [`std::pin::Pin::as_deref_mut`].
103+
// TODO: Remove this once we bump the MSRV to 1.84.
104+
pub(crate) fn pin_as_deref_mut<P: DerefMut>(ptr: Pin<&mut Pin<P>>) -> Pin<&mut P::Target> {
105+
unsafe { ptr.get_unchecked_mut() }.as_mut()
106+
}

0 commit comments

Comments
 (0)