Skip to content

Commit

Permalink
poll_fn and Unpin: fix pinning
Browse files Browse the repository at this point in the history
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.

    rust-lang/rust#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.
  • Loading branch information
FrankReh committed Oct 30, 2022
1 parent 507186d commit 665f2ba
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/future.rs
Expand Up @@ -16,7 +16,7 @@ pub(crate) struct PollFn<F> {
f: F,
}

impl<F> Unpin for PollFn<F> {}
impl<F: Unpin> Unpin for PollFn<F> {}

pub(crate) fn poll_fn<T, F>(f: F) -> PollFn<F>
where
Expand All @@ -31,7 +31,8 @@ where
{
type Output = T;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
(self.f)(cx)
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
// SAFETY: We are not moving out of the pinned field.
(unsafe { &mut self.get_unchecked_mut().f })(cx)
}
}

0 comments on commit 665f2ba

Please sign in to comment.