-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
deff252
commit 4601c84
Showing
4 changed files
with
378 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
pub(crate) struct PollFn<F> { | ||
f: F, | ||
} | ||
|
||
pub(crate) fn poll_fn<T, F>(f: F) -> PollFn<F> | ||
where | ||
F: FnMut(&mut Context<'_>) -> Poll<T>, | ||
{ | ||
PollFn { f } | ||
} | ||
|
||
impl<T, F> Future for PollFn<F> | ||
where | ||
F: FnMut(&mut Context<'_>) -> Poll<T>, | ||
{ | ||
type Output = T; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> { | ||
// Safety: We never construct a `Pin<&mut F>` anywhere, so accessing `f` | ||
// mutably in an unpinned way is sound. | ||
// | ||
// This use of unsafe cannot be replaced with the pin-project macro | ||
// because: | ||
// * If we put `#[pin]` on the field, then it gives us a `Pin<&mut F>`, | ||
// which we can't use to call the closure. | ||
// * If we don't put `#[pin]` on the field, then it makes `PollFn` be | ||
// unconditionally `Unpin`, which we also don't want. | ||
let me = unsafe { Pin::into_inner_unchecked(self) }; | ||
(me.f)(cx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.