mirror of
https://github.com/async-rs/async-std.git
synced 2025-03-27 03:30:25 +00:00
First attempt at successor
This commit is contained in:
parent
4e1d79adb1
commit
fe3c9ef626
2 changed files with 60 additions and 0 deletions
|
@ -57,6 +57,7 @@ mod partial_cmp;
|
||||||
mod position;
|
mod position;
|
||||||
mod scan;
|
mod scan;
|
||||||
mod skip;
|
mod skip;
|
||||||
|
mod successor;
|
||||||
mod skip_while;
|
mod skip_while;
|
||||||
mod step_by;
|
mod step_by;
|
||||||
mod take;
|
mod take;
|
||||||
|
|
59
src/stream/stream/successor.rs
Normal file
59
src/stream/stream/successor.rs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use crate::future::Future;
|
||||||
|
use crate::stream::Stream;
|
||||||
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Successor<F, Fut, T>
|
||||||
|
where Fut: Future<Output=T>
|
||||||
|
{
|
||||||
|
successor: F,
|
||||||
|
next: T,
|
||||||
|
_marker: PhantomData<Fut>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn successor<F, Fut, T>(func: F, start: T) -> Successor<F, Fut, T>
|
||||||
|
where
|
||||||
|
F: FnMut(T) -> Fut,
|
||||||
|
Fut: Future<Output = T>,
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
Successor {
|
||||||
|
successor: func,
|
||||||
|
next: start,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <F, Fut, T> Successor<F, Fut, T>
|
||||||
|
where
|
||||||
|
F: FnMut(T) -> Fut,
|
||||||
|
Fut: Future<Output = T>,
|
||||||
|
T: Copy,
|
||||||
|
|
||||||
|
{
|
||||||
|
pin_utils::unsafe_unpinned!(successor: F);
|
||||||
|
pin_utils::unsafe_unpinned!(next: T);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <F, Fut, T> Stream for Successor<F, Fut, T>
|
||||||
|
where
|
||||||
|
Fut: Future<Output = T>,
|
||||||
|
F: FnMut(T) -> Fut,
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
type Item = T;
|
||||||
|
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
|
||||||
|
match self.as_mut().successor()(self.next).poll(cx) {
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
|
Poll::Ready(val) => {
|
||||||
|
self.next = val;
|
||||||
|
Poll::Ready(Some(val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue