Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from montekki/fs-stream-for-each
Adds for_each stream combinator
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::marker::PhantomData; | ||
use std::pin::Pin; | ||
|
||
use crate::future::Future; | ||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct ForEachFuture<S, F, T> { | ||
stream: S, | ||
f: F, | ||
__t: PhantomData<T>, | ||
} | ||
|
||
impl<S, F, T> ForEachFuture<S, F, T> { | ||
pin_utils::unsafe_pinned!(stream: S); | ||
pin_utils::unsafe_unpinned!(f: F); | ||
|
||
pub(super) fn new(stream: S, f: F) -> Self { | ||
ForEachFuture { | ||
stream, | ||
f, | ||
__t: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<S, F> Future for ForEachFuture<S, F, S::Item> | ||
where | ||
S: Stream + Sized, | ||
F: FnMut(S::Item), | ||
{ | ||
type Output = (); | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
loop { | ||
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx)); | ||
|
||
match next { | ||
Some(v) => (self.as_mut().f())(v), | ||
None => return Poll::Ready(()), | ||
} | ||
} | ||
} | ||
} |
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