mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-24 01:06:46 +00:00
wip: Add stream unzip
This commit is contained in:
parent
3c6d41ccb4
commit
31cf932d80
2 changed files with 66 additions and 0 deletions
|
@ -130,6 +130,7 @@ cfg_unstable! {
|
||||||
pub use flat_map::FlatMap;
|
pub use flat_map::FlatMap;
|
||||||
pub use timeout::{TimeoutError, Timeout};
|
pub use timeout::{TimeoutError, Timeout};
|
||||||
pub use throttle::Throttle;
|
pub use throttle::Throttle;
|
||||||
|
pub use unzip::UnzipFuture;
|
||||||
|
|
||||||
mod count;
|
mod count;
|
||||||
mod merge;
|
mod merge;
|
||||||
|
@ -138,6 +139,7 @@ cfg_unstable! {
|
||||||
mod partition;
|
mod partition;
|
||||||
mod timeout;
|
mod timeout;
|
||||||
mod throttle;
|
mod throttle;
|
||||||
|
mod unzip;
|
||||||
}
|
}
|
||||||
|
|
||||||
extension_trait! {
|
extension_trait! {
|
||||||
|
@ -1717,6 +1719,17 @@ extension_trait! {
|
||||||
Zip::new(self, other)
|
Zip::new(self, other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "unstable")]
|
||||||
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
|
fn unzip<A, B, FromA, FromB>(self) -> impl Future<Output = (FromA, FromB)> [UnzipFuture<Self, FromA, FromB>]
|
||||||
|
where
|
||||||
|
FromA: Default + Extend<A>,
|
||||||
|
FromB: Default + Extend<B>,
|
||||||
|
Self: Stream<Item = (A, B)> + Sized,
|
||||||
|
{
|
||||||
|
UnzipFuture::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
#[doc = r#"
|
#[doc = r#"
|
||||||
Transforms a stream into a collection.
|
Transforms a stream into a collection.
|
||||||
|
|
||||||
|
|
53
src/stream/stream/unzip.rs
Normal file
53
src/stream/stream/unzip.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
|
use crate::stream::Stream;
|
||||||
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
|
#[cfg(all(feature = "default", feature = "unstable"))]
|
||||||
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
|
pub struct UnzipFuture<S: Stream, FromA, FromB> {
|
||||||
|
#[pin]
|
||||||
|
stream: S,
|
||||||
|
res: (FromA, FromB),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Stream, FromA, FromB> UnzipFuture<S, FromA, FromB>
|
||||||
|
where
|
||||||
|
FromA: Default,
|
||||||
|
FromB: Default,
|
||||||
|
{
|
||||||
|
pub(super) fn new(stream: S) -> Self {
|
||||||
|
UnzipFuture {
|
||||||
|
stream,
|
||||||
|
res: (FromA::default(), FromB::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, A, B, FromA, FromB> Future for UnzipFuture<S, FromA, FromB>
|
||||||
|
where
|
||||||
|
S: Stream<Item = (A, B)>,
|
||||||
|
FromA: Default + Extend<A> + Copy,
|
||||||
|
FromB: Default + Extend<B> + Copy,
|
||||||
|
{
|
||||||
|
type Output = (FromA, FromB);
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let mut this = self.project();
|
||||||
|
let next = futures_core::ready!(this.stream.as_mut().poll_next(cx));
|
||||||
|
|
||||||
|
match next {
|
||||||
|
Some((a, b)) => {
|
||||||
|
this.res.0.extend(Some(a));
|
||||||
|
this.res.1.extend(Some(b));
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
None => Poll::Ready(*this.res),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue