Skip to content

Commit 5f6e526

Browse files
committed
fix pin compile error
1 parent a2d8e61 commit 5f6e526

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/stream/from_fn.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::task::{Context, Poll};
66
use futures::ready;
77
use futures::stream::Stream;
88

9+
use pin_utils::unsafe_unpinned;
10+
911
/// Creates a new stream where each iteration calls the provided closure.
1012
///
1113
/// This allows creating a custom iterator with any behavior
@@ -52,7 +54,11 @@ pub fn from_fn<T, F>(f: F) -> FromFn<F, T>
5254
/// [`stream::from_fn`]: fn.from_fn.html
5355
pub struct FromFn<F, T> {
5456
closure: F,
55-
fut: Option<Box<dyn Future<Output = Option<T>>>>,
57+
fut: Option<Pin<Box<dyn Future<Output = Option<T>>>>>,
58+
}
59+
60+
impl<F,T> FromFn<F,T> {
61+
unsafe_unpinned!(closure: F);
5662
}
5763

5864
impl<T, F: Unpin> Stream for FromFn<F, T>
@@ -63,10 +69,11 @@ impl<T, F: Unpin> Stream for FromFn<F, T>
6369
#[inline]
6470
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
6571
if let None = self.fut {
66-
self.fut = Some((self.closure)());
72+
let fut = (self.as_mut().closure())();
73+
self.fut = unsafe{Some(Pin::new_unchecked(fut))};
6774
}
6875

69-
let pinned = unsafe { Pin::new_unchecked(&mut self.fut.as_mut().unwrap()) };
76+
let pinned = Pin::new(self.fut.as_mut().unwrap());
7077
let out = ready!(pinned.poll(cx));
7178

7279
self.fut = None;

0 commit comments

Comments
 (0)