From 64216b8e6bf24ccb95a296650e08cc56cc59ad74 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 14 Nov 2019 21:49:24 +0100 Subject: [PATCH] Take a normal closure, not an async one --- src/stream/successors.rs | 51 ++++++++++------------------------------ 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/src/stream/successors.rs b/src/stream/successors.rs index e86512b..d5840ee 100644 --- a/src/stream/successors.rs +++ b/src/stream/successors.rs @@ -1,9 +1,8 @@ use std::pin::Pin; use std::mem; -use crate::future::Future; use crate::stream::Stream; -use crate::task::{Context, Poll, ready}; +use crate::task::{Context, Poll}; use pin_project_lite::pin_project; @@ -18,11 +17,7 @@ use pin_project_lite::pin_project; /// use async_std::prelude::*; /// use async_std::stream; /// -/// let s = stream::successors(Some(22), |&val| { -/// async move { -/// Some(val + 1) -/// } -/// }); +/// let s = stream::successors(Some(22), |&val| Some(val + 1) ); /// /// pin_utils::pin_mut!(s); /// assert_eq!(s.next().await, Some(22)); @@ -30,30 +25,18 @@ use pin_project_lite::pin_project; /// assert_eq!(s.next().await, Some(24)); /// assert_eq!(s.next().await, Some(25)); /// -/// -///let never = stream::successors(None, |_| { -/// async move { -/// Some(1) -/// } -/// }); -/// -/// pin_utils::pin_mut!(never); -/// assert_eq!(never.next().await, None); -/// assert_eq!(never.next().await, None); /// # /// # }) } /// /// ``` #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] -pub fn successors(first: Option, succ: F) -> Successors +pub fn successors(first: Option, succ: F) -> Successors where - F: FnMut(&T) -> Fut, - Fut: Future>, + F: FnMut(&T) -> Option, { Successors { - succ: succ, - future: None, + succ, slot: first, } } @@ -68,39 +51,29 @@ pin_project! { #[cfg(feature = "unstable")] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[derive(Debug)] - pub struct Successors + pub struct Successors where - Fut: Future>, + F: FnMut(&T) -> Option { succ: F, - #[pin] - future: Option, slot: Option, } } -impl Stream for Successors +impl Stream for Successors where - Fut: Future>, - F: FnMut(&T) -> Fut, + F: FnMut(&T) -> Option, { type Item = T; - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let mut this = self.project(); + fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + let this = self.project(); if this.slot.is_none() { return Poll::Ready(None); } - if this.future.is_none() { - let fut = (this.succ)(this.slot.as_ref().unwrap()); - this.future.set(Some(fut)); - } - - let mut next = ready!(this.future.as_mut().as_pin_mut().unwrap().poll(cx)); - - this.future.set(None); + let mut next = (this.succ)(&this.slot.as_ref().unwrap()); // 'swapping' here means 'slot' will hold the next value and next will be th one from the previous iteration mem::swap(this.slot, &mut next);