Skip to content

Commit 665f2ba

Browse files
committedOct 30, 2022
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. 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.
1 parent 507186d commit 665f2ba

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed
 

‎src/future.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) struct PollFn<F> {
1616
f: F,
1717
}
1818

19-
impl<F> Unpin for PollFn<F> {}
19+
impl<F: Unpin> Unpin for PollFn<F> {}
2020

2121
pub(crate) fn poll_fn<T, F>(f: F) -> PollFn<F>
2222
where
@@ -31,7 +31,8 @@ where
3131
{
3232
type Output = T;
3333

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

0 commit comments

Comments
 (0)
Please sign in to comment.