-
Notifications
You must be signed in to change notification settings - Fork 341
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 #541 from yjhmelody/stream-partition
add stream-partition
- Loading branch information
Showing
2 changed files
with
102 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
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,60 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::default::Default; | ||
use pin_project_lite::pin_project; | ||
|
||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
pin_project! { | ||
#[derive(Debug)] | ||
#[allow(missing_debug_implementations)] | ||
#[cfg(all(feature = "default", feature = "unstable"))] | ||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] | ||
pub struct PartitionFuture<S, F, B> { | ||
#[pin] | ||
stream: S, | ||
f: F, | ||
res: Option<(B, B)>, | ||
} | ||
} | ||
|
||
impl<S, F, B: Default> PartitionFuture<S, F, B> { | ||
pub(super) fn new(stream: S, f: F) -> Self { | ||
Self { | ||
stream, | ||
f, | ||
res: Some((B::default(), B::default())), | ||
} | ||
} | ||
} | ||
|
||
impl<S, F, B> Future for PartitionFuture<S, F, B> | ||
where | ||
S: Stream + Sized, | ||
F: FnMut(&S::Item) -> bool, | ||
B: Default + Extend<S::Item>, | ||
{ | ||
type Output = (B, B); | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let mut this = self.project(); | ||
|
||
loop { | ||
let next = futures_core::ready!(this.stream.as_mut().poll_next(cx)); | ||
|
||
match next { | ||
Some(v) => { | ||
let mut res = this.res.take().unwrap(); | ||
match (this.f)(&v) { | ||
true => res.0.extend(Some(v)), | ||
false => res.1.extend(Some(v)), | ||
}; | ||
|
||
*this.res = Some(res); | ||
} | ||
None => return Poll::Ready(this.res.take().unwrap()), | ||
} | ||
} | ||
} | ||
} |