From 665f2babd8b9a8d78e8239b4808d832100f83454 Mon Sep 17 00:00:00 2001 From: Frank Rehwinkel Date: Sun, 30 Oct 2022 12:49:02 -0400 Subject: [PATCH] poll_fn and Unpin: fix pinning Keeping this crate's poll_fn in sync with that of rust's library/core/src/future/poll_fn.rs. Their merge provides all the details. https://github.com/rust-lang/rust/pull/102737 None of this crate's uses of poll_fn are affected. It protects against misuse in the future which could have led to UB as the above mentioned PR points out with an example of a double free becoming possible. --- src/future.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/future.rs b/src/future.rs index f27df0eb..76fba131 100644 --- a/src/future.rs +++ b/src/future.rs @@ -16,7 +16,7 @@ pub(crate) struct PollFn { f: F, } -impl Unpin for PollFn {} +impl Unpin for PollFn {} pub(crate) fn poll_fn(f: F) -> PollFn where @@ -31,7 +31,8 @@ where { type Output = T; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - (self.f)(cx) + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + // SAFETY: We are not moving out of the pinned field. + (unsafe { &mut self.get_unchecked_mut().f })(cx) } }