|
|
@ -1,25 +1,31 @@
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::future::Future;
|
|
|
|
use crate::future::Future;
|
|
|
|
use crate::stream::Stream;
|
|
|
|
use crate::stream::Stream;
|
|
|
|
use crate::task::{Context, Poll};
|
|
|
|
use crate::task::{Context, Poll, ready};
|
|
|
|
|
|
|
|
|
|
|
|
/// A stream that yields elements by calling an async closure with the previous value as an
|
|
|
|
|
|
|
|
/// argument
|
|
|
|
|
|
|
|
///
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
/// This stream is constructed by [`successor`] function
|
|
|
|
/// A stream that yields elements by calling an async closure with the previous value as an
|
|
|
|
///
|
|
|
|
/// argument
|
|
|
|
/// [`successor`]: fn.successor.html
|
|
|
|
///
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// This stream is constructed by [`successor`] function
|
|
|
|
pub struct Successors<F, Fut, T>
|
|
|
|
///
|
|
|
|
where
|
|
|
|
/// [`successor`]: fn.successor.html
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
|
#[derive(Debug)]
|
|
|
|
{
|
|
|
|
pub struct Successors<F, Fut, T>
|
|
|
|
successor: F,
|
|
|
|
where
|
|
|
|
future: Option<Fut>,
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
|
next: Option<T>,
|
|
|
|
{
|
|
|
|
_marker: PhantomData<Fut>,
|
|
|
|
successor: F,
|
|
|
|
|
|
|
|
#[pin]
|
|
|
|
|
|
|
|
future: Option<Fut>,
|
|
|
|
|
|
|
|
slot: Option<T>,
|
|
|
|
|
|
|
|
_marker: PhantomData<Fut>,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a new stream where to produce each new element a closure is called with the previous
|
|
|
|
/// Creates a new stream where to produce each new element a closure is called with the previous
|
|
|
@ -40,6 +46,7 @@ where
|
|
|
|
/// });
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// pin_utils::pin_mut!(s);
|
|
|
|
/// pin_utils::pin_mut!(s);
|
|
|
|
|
|
|
|
/// assert_eq!(s.next().await, Some(22));
|
|
|
|
/// assert_eq!(s.next().await, Some(23));
|
|
|
|
/// assert_eq!(s.next().await, Some(23));
|
|
|
|
/// assert_eq!(s.next().await, Some(24));
|
|
|
|
/// assert_eq!(s.next().await, Some(24));
|
|
|
|
/// assert_eq!(s.next().await, Some(25));
|
|
|
|
/// assert_eq!(s.next().await, Some(25));
|
|
|
@ -58,31 +65,20 @@ where
|
|
|
|
/// # }) }
|
|
|
|
/// # }) }
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub fn successors<F, Fut, T>(start: Option<T>, func: F) -> Successors<F, Fut, T>
|
|
|
|
pub fn successors<F, Fut, T>(first: Option<T>, succ: F) -> Successors<F, Fut, T>
|
|
|
|
where
|
|
|
|
where
|
|
|
|
F: FnMut(T) -> Fut,
|
|
|
|
F: FnMut(T) -> Fut,
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
|
T: Copy,
|
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Successors {
|
|
|
|
Successors {
|
|
|
|
successor: func,
|
|
|
|
successor: succ,
|
|
|
|
future: None,
|
|
|
|
future: None,
|
|
|
|
next: start,
|
|
|
|
slot: first,
|
|
|
|
_marker: PhantomData,
|
|
|
|
_marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<F, Fut, T> Successors<F, Fut, T>
|
|
|
|
|
|
|
|
where
|
|
|
|
|
|
|
|
F: FnMut(T) -> Fut,
|
|
|
|
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
|
|
|
|
|
T: Copy,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
pin_utils::unsafe_unpinned!(successor: F);
|
|
|
|
|
|
|
|
pin_utils::unsafe_unpinned!(next: Option<T>);
|
|
|
|
|
|
|
|
pin_utils::unsafe_pinned!(future: Option<Fut>);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<F, Fut, T> Stream for Successors<F, Fut, T>
|
|
|
|
impl<F, Fut, T> Stream for Successors<F, Fut, T>
|
|
|
|
where
|
|
|
|
where
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
|
Fut: Future<Output = Option<T>>,
|
|
|
@ -91,23 +87,23 @@ where
|
|
|
|
{
|
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
if self.next.is_none() {
|
|
|
|
let mut this = self.project();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if this.slot.is_none() {
|
|
|
|
return Poll::Ready(None);
|
|
|
|
return Poll::Ready(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
match &self.future {
|
|
|
|
if this.future.is_none() {
|
|
|
|
None => {
|
|
|
|
let x = this.slot.unwrap();
|
|
|
|
let x = self.next.unwrap();
|
|
|
|
let fut = (this.successor)(x);
|
|
|
|
let fut = (self.as_mut().successor())(x);
|
|
|
|
this.future.set(Some(fut));
|
|
|
|
self.as_mut().future().set(Some(fut));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let next = futures_core::ready!(self.as_mut().future().as_pin_mut().unwrap().poll(cx));
|
|
|
|
let mut next = ready!(this.future.as_mut().as_pin_mut().unwrap().poll(cx));
|
|
|
|
*self.as_mut().next() = next;
|
|
|
|
|
|
|
|
self.as_mut().future().set(None);
|
|
|
|
this.future.set(None);
|
|
|
|
|
|
|
|
mem::swap(this.slot, &mut next);
|
|
|
|
Poll::Ready(next)
|
|
|
|
Poll::Ready(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|