Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`and_then`](super::TryStreamExt::and_then) method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct AndThen<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
#[pin]
|
||||
future: Option<Fut>,
|
||||
f: F,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for AndThen<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("AndThen")
|
||||
.field("stream", &self.stream)
|
||||
.field("future", &self.future)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> AndThen<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: TryFuture<Error = St::Error>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, future: None, f }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Stream for AndThen<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: TryFuture<Error = St::Error>,
|
||||
{
|
||||
type Item = Result<Fut::Ok, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.future.as_mut().as_pin_mut() {
|
||||
let item = ready!(fut.try_poll(cx));
|
||||
this.future.set(None);
|
||||
break Some(item);
|
||||
} else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
this.future.set(Some((this.f)(item)));
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let future_len = usize::from(self.future.is_some());
|
||||
let (lower, upper) = self.stream.size_hint();
|
||||
let lower = lower.saturating_add(future_len);
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(future_len),
|
||||
None => None,
|
||||
};
|
||||
(lower, upper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedStream for AndThen<St, Fut, F>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: TryFuture<Error = St::Error>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.future.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Fut, F, Item> Sink<Item> for AndThen<S, Fut, F>
|
||||
where
|
||||
S: Sink<Item>,
|
||||
{
|
||||
type Error = S::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
use core::pin::Pin;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::cmp;
|
||||
use std::io::{Error, Result};
|
||||
|
||||
pin_project! {
|
||||
/// Reader for the [`into_async_read`](super::TryStreamExt::into_async_read) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "readers do nothing unless polled"]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "io")))]
|
||||
pub struct IntoAsyncRead<St>
|
||||
where
|
||||
St: TryStream<Error = Error>,
|
||||
St::Ok: AsRef<[u8]>,
|
||||
{
|
||||
#[pin]
|
||||
stream: St,
|
||||
state: ReadState<St::Ok>,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ReadState<T: AsRef<[u8]>> {
|
||||
Ready { chunk: T, chunk_start: usize },
|
||||
PendingChunk,
|
||||
Eof,
|
||||
}
|
||||
|
||||
impl<St> IntoAsyncRead<St>
|
||||
where
|
||||
St: TryStream<Error = Error>,
|
||||
St::Ok: AsRef<[u8]>,
|
||||
{
|
||||
pub(super) fn new(stream: St) -> Self {
|
||||
Self { stream, state: ReadState::PendingChunk }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> AsyncRead for IntoAsyncRead<St>
|
||||
where
|
||||
St: TryStream<Error = Error>,
|
||||
St::Ok: AsRef<[u8]>,
|
||||
{
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
let mut this = self.project();
|
||||
|
||||
loop {
|
||||
match this.state {
|
||||
ReadState::Ready { chunk, chunk_start } => {
|
||||
let chunk = chunk.as_ref();
|
||||
let len = cmp::min(buf.len(), chunk.len() - *chunk_start);
|
||||
|
||||
buf[..len].copy_from_slice(&chunk[*chunk_start..*chunk_start + len]);
|
||||
*chunk_start += len;
|
||||
|
||||
if chunk.len() == *chunk_start {
|
||||
*this.state = ReadState::PendingChunk;
|
||||
}
|
||||
|
||||
return Poll::Ready(Ok(len));
|
||||
}
|
||||
ReadState::PendingChunk => match ready!(this.stream.as_mut().try_poll_next(cx)) {
|
||||
Some(Ok(chunk)) => {
|
||||
if !chunk.as_ref().is_empty() {
|
||||
*this.state = ReadState::Ready { chunk, chunk_start: 0 };
|
||||
}
|
||||
}
|
||||
Some(Err(err)) => {
|
||||
*this.state = ReadState::Eof;
|
||||
return Poll::Ready(Err(err));
|
||||
}
|
||||
None => {
|
||||
*this.state = ReadState::Eof;
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
},
|
||||
ReadState::Eof => {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> AsyncWrite for IntoAsyncRead<St>
|
||||
where
|
||||
St: TryStream<Error = Error> + AsyncWrite,
|
||||
St::Ok: AsRef<[u8]>,
|
||||
{
|
||||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
|
||||
let this = self.project();
|
||||
this.stream.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
let this = self.project();
|
||||
this.stream.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
let this = self.project();
|
||||
this.stream.poll_close(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> AsyncBufRead for IntoAsyncRead<St>
|
||||
where
|
||||
St: TryStream<Error = Error>,
|
||||
St::Ok: AsRef<[u8]>,
|
||||
{
|
||||
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
|
||||
let mut this = self.project();
|
||||
|
||||
while let ReadState::PendingChunk = this.state {
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)) {
|
||||
Some(Ok(chunk)) => {
|
||||
if !chunk.as_ref().is_empty() {
|
||||
*this.state = ReadState::Ready { chunk, chunk_start: 0 };
|
||||
}
|
||||
}
|
||||
Some(Err(err)) => {
|
||||
*this.state = ReadState::Eof;
|
||||
return Poll::Ready(Err(err));
|
||||
}
|
||||
None => {
|
||||
*this.state = ReadState::Eof;
|
||||
return Poll::Ready(Ok(&[]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let &mut ReadState::Ready { ref chunk, chunk_start } = this.state {
|
||||
let chunk = chunk.as_ref();
|
||||
return Poll::Ready(Ok(&chunk[chunk_start..]));
|
||||
}
|
||||
|
||||
// To get to this point we must be in ReadState::Eof
|
||||
Poll::Ready(Ok(&[]))
|
||||
}
|
||||
|
||||
fn consume(self: Pin<&mut Self>, amount: usize) {
|
||||
let this = self.project();
|
||||
|
||||
// https://github.com/rust-lang/futures-rs/pull/1556#discussion_r281644295
|
||||
if amount == 0 {
|
||||
return;
|
||||
}
|
||||
if let ReadState::Ready { chunk, chunk_start } = this.state {
|
||||
*chunk_start += amount;
|
||||
debug_assert!(*chunk_start <= chunk.as_ref().len());
|
||||
if *chunk_start >= chunk.as_ref().len() {
|
||||
*this.state = ReadState::PendingChunk;
|
||||
}
|
||||
} else {
|
||||
debug_assert!(false, "Attempted to consume from IntoAsyncRead without chunk");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
use core::pin::Pin;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`into_stream`](super::TryStreamExt::into_stream) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct IntoStream<St> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> IntoStream<St> {
|
||||
#[inline]
|
||||
pub(super) fn new(stream: St) -> Self {
|
||||
Self { stream }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St: TryStream + FusedStream> FusedStream for IntoStream<St> {
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: TryStream> Stream for IntoStream<St> {
|
||||
type Item = Result<St::Ok, St::Error>;
|
||||
|
||||
#[inline]
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
self.project().stream.try_poll_next(cx)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.stream.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S: Sink<Item>, Item> Sink<Item> for IntoStream<S> {
|
||||
type Error = S::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
+1254
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,109 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`or_else`](super::TryStreamExt::or_else) method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct OrElse<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
#[pin]
|
||||
future: Option<Fut>,
|
||||
f: F,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for OrElse<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("OrElse")
|
||||
.field("stream", &self.stream)
|
||||
.field("future", &self.future)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> OrElse<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Error) -> Fut,
|
||||
Fut: TryFuture<Ok = St::Ok>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, future: None, f }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Stream for OrElse<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Error) -> Fut,
|
||||
Fut: TryFuture<Ok = St::Ok>,
|
||||
{
|
||||
type Item = Result<St::Ok, Fut::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.future.as_mut().as_pin_mut() {
|
||||
let item = ready!(fut.try_poll(cx));
|
||||
this.future.set(None);
|
||||
break Some(item);
|
||||
} else {
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)) {
|
||||
Some(Ok(item)) => break Some(Ok(item)),
|
||||
Some(Err(e)) => {
|
||||
this.future.set(Some((this.f)(e)));
|
||||
}
|
||||
None => break None,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let future_len = usize::from(self.future.is_some());
|
||||
let (lower, upper) = self.stream.size_hint();
|
||||
let lower = lower.saturating_add(future_len);
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(future_len),
|
||||
None => None,
|
||||
};
|
||||
(lower, upper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedStream for OrElse<St, Fut, F>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
F: FnMut(St::Error) -> Fut,
|
||||
Fut: TryFuture<Ok = St::Ok>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.future.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Fut, F, Item> Sink<Item> for OrElse<S, Fut, F>
|
||||
where
|
||||
S: Sink<Item>,
|
||||
{
|
||||
type Error = S::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{FusedFuture, Future};
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the [`try_all`](super::TryStreamExt::try_all) method.
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryAll<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
done: bool,
|
||||
#[pin]
|
||||
future: Option<Fut>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryAll<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryAll")
|
||||
.field("stream", &self.stream)
|
||||
.field("done", &self.done)
|
||||
.field("future", &self.future)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryAll<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, done: false, future: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedFuture for TryAll<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.done && self.future.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Future for TryAll<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
type Output = Result<bool, St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<bool, St::Error>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.future.as_mut().as_pin_mut() {
|
||||
// we're currently processing a future to produce a new value
|
||||
let acc = ready!(fut.poll(cx));
|
||||
this.future.set(None);
|
||||
if !acc {
|
||||
*this.done = true;
|
||||
break Ok(false);
|
||||
} // early exit
|
||||
} else if !*this.done {
|
||||
// we're waiting on a new item from the stream
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)) {
|
||||
Some(Ok(item)) => {
|
||||
this.future.set(Some((this.f)(item)));
|
||||
}
|
||||
Some(Err(err)) => {
|
||||
*this.done = true;
|
||||
break Err(err);
|
||||
}
|
||||
None => {
|
||||
*this.done = true;
|
||||
break Ok(true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("TryAll polled after completion")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{FusedFuture, Future};
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the [`try_any`](super::TryStreamExt::try_any) method.
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryAny<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
done: bool,
|
||||
#[pin]
|
||||
future: Option<Fut>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryAny<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryAny")
|
||||
.field("stream", &self.stream)
|
||||
.field("done", &self.done)
|
||||
.field("future", &self.future)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryAny<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, done: false, future: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedFuture for TryAny<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.done && self.future.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Future for TryAny<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
type Output = Result<bool, St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<bool, St::Error>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.future.as_mut().as_pin_mut() {
|
||||
// we're currently processing a future to produce a new value
|
||||
let acc = ready!(fut.poll(cx));
|
||||
this.future.set(None);
|
||||
if acc {
|
||||
*this.done = true;
|
||||
break Ok(true);
|
||||
} // early exit
|
||||
} else if !*this.done {
|
||||
// we're waiting on a new item from the stream
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)) {
|
||||
Some(Ok(item)) => {
|
||||
this.future.set(Some((this.f)(item)));
|
||||
}
|
||||
Some(Err(err)) => {
|
||||
*this.done = true;
|
||||
break Err(err);
|
||||
}
|
||||
None => {
|
||||
*this.done = true;
|
||||
break Ok(false);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("TryAny polled after completion")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
use crate::future::{IntoFuture, TryFutureExt};
|
||||
use crate::stream::{Fuse, FuturesUnordered, IntoStream, StreamExt};
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::stream::{Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the
|
||||
/// [`try_buffer_unordered`](super::TryStreamExt::try_buffer_unordered) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryBufferUnordered<St>
|
||||
where St: TryStream
|
||||
{
|
||||
#[pin]
|
||||
stream: Fuse<IntoStream<St>>,
|
||||
in_progress_queue: FuturesUnordered<IntoFuture<St::Ok>>,
|
||||
max: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> TryBufferUnordered<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryFuture,
|
||||
{
|
||||
pub(super) fn new(stream: St, n: usize) -> Self {
|
||||
Self {
|
||||
stream: IntoStream::new(stream).fuse(),
|
||||
in_progress_queue: FuturesUnordered::new(),
|
||||
max: n,
|
||||
}
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, (. .));
|
||||
}
|
||||
|
||||
impl<St> Stream for TryBufferUnordered<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryFuture<Error = St::Error>,
|
||||
{
|
||||
type Item = Result<<St::Ok as TryFuture>::Ok, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
// First up, try to spawn off as many futures as possible by filling up
|
||||
// our queue of futures. Propagate errors from the stream immediately.
|
||||
while this.in_progress_queue.len() < *this.max {
|
||||
match this.stream.as_mut().poll_next(cx)? {
|
||||
Poll::Ready(Some(fut)) => this.in_progress_queue.push(fut.into_future()),
|
||||
Poll::Ready(None) | Poll::Pending => break,
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to pull the next value from the in_progress_queue
|
||||
match this.in_progress_queue.poll_next_unpin(cx) {
|
||||
x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x,
|
||||
Poll::Ready(None) => {}
|
||||
}
|
||||
|
||||
// If more values are still coming from the stream, we're not done yet
|
||||
if this.stream.is_done() {
|
||||
Poll::Ready(None)
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Item, E> Sink<Item> for TryBufferUnordered<S>
|
||||
where
|
||||
S: TryStream + Sink<Item, Error = E>,
|
||||
S::Ok: TryFuture<Error = E>,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
use crate::future::{IntoFuture, TryFutureExt};
|
||||
use crate::stream::{Fuse, FuturesOrdered, IntoStream, StreamExt};
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::stream::{Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_buffered`](super::TryStreamExt::try_buffered) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryBuffered<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryFuture,
|
||||
{
|
||||
#[pin]
|
||||
stream: Fuse<IntoStream<St>>,
|
||||
in_progress_queue: FuturesOrdered<IntoFuture<St::Ok>>,
|
||||
max: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> TryBuffered<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryFuture,
|
||||
{
|
||||
pub(super) fn new(stream: St, n: usize) -> Self {
|
||||
Self {
|
||||
stream: IntoStream::new(stream).fuse(),
|
||||
in_progress_queue: FuturesOrdered::new(),
|
||||
max: n,
|
||||
}
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, (. .));
|
||||
}
|
||||
|
||||
impl<St> Stream for TryBuffered<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryFuture<Error = St::Error>,
|
||||
{
|
||||
type Item = Result<<St::Ok as TryFuture>::Ok, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
// First up, try to spawn off as many futures as possible by filling up
|
||||
// our queue of futures. Propagate errors from the stream immediately.
|
||||
while this.in_progress_queue.len() < *this.max {
|
||||
match this.stream.as_mut().poll_next(cx)? {
|
||||
Poll::Ready(Some(fut)) => this.in_progress_queue.push_back(fut.into_future()),
|
||||
Poll::Ready(None) | Poll::Pending => break,
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to pull the next value from the in_progress_queue
|
||||
match this.in_progress_queue.poll_next_unpin(cx) {
|
||||
x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x,
|
||||
Poll::Ready(None) => {}
|
||||
}
|
||||
|
||||
// If more values are still coming from the stream, we're not done yet
|
||||
if this.stream.is_done() {
|
||||
Poll::Ready(None)
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Item, E> Sink<Item> for TryBuffered<S>
|
||||
where
|
||||
S: TryStream + Sink<Item, Error = E>,
|
||||
S::Ok: TryFuture<Error = E>,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
use crate::stream::{Fuse, IntoStream, StreamExt};
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use core::pin::Pin;
|
||||
use core::{fmt, mem};
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_chunks`](super::TryStreamExt::try_chunks) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryChunks<St: TryStream> {
|
||||
#[pin]
|
||||
stream: Fuse<IntoStream<St>>,
|
||||
items: Vec<St::Ok>,
|
||||
cap: usize, // https://github.com/rust-lang/futures-rs/issues/1475
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: TryStream> TryChunks<St> {
|
||||
pub(super) fn new(stream: St, capacity: usize) -> Self {
|
||||
assert!(capacity > 0);
|
||||
|
||||
Self {
|
||||
stream: IntoStream::new(stream).fuse(),
|
||||
items: Vec::with_capacity(capacity),
|
||||
cap: capacity,
|
||||
}
|
||||
}
|
||||
|
||||
fn take(self: Pin<&mut Self>) -> Vec<St::Ok> {
|
||||
let cap = self.cap;
|
||||
mem::replace(self.project().items, Vec::with_capacity(cap))
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, (. .));
|
||||
}
|
||||
|
||||
type TryChunksStreamError<St> = TryChunksError<<St as TryStream>::Ok, <St as TryStream>::Error>;
|
||||
|
||||
impl<St: TryStream> Stream for TryChunks<St> {
|
||||
type Item = Result<Vec<St::Ok>, TryChunksStreamError<St>>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.as_mut().project();
|
||||
loop {
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)) {
|
||||
// Push the item into the buffer and check whether it is full.
|
||||
// If so, replace our buffer with a new and empty one and return
|
||||
// the full one.
|
||||
Some(item) => match item {
|
||||
Ok(item) => {
|
||||
this.items.push(item);
|
||||
if this.items.len() >= *this.cap {
|
||||
return Poll::Ready(Some(Ok(self.take())));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Poll::Ready(Some(Err(TryChunksError(self.take(), e))));
|
||||
}
|
||||
},
|
||||
|
||||
// Since the underlying stream ran out of values, return what we
|
||||
// have buffered, if we have anything.
|
||||
None => {
|
||||
let last = if this.items.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let full_buf = mem::take(this.items);
|
||||
Some(full_buf)
|
||||
};
|
||||
|
||||
return Poll::Ready(last.map(Ok));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let chunk_len = usize::from(!self.items.is_empty());
|
||||
let (lower, upper) = self.stream.size_hint();
|
||||
let lower = (lower / self.cap).saturating_add(chunk_len);
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(chunk_len),
|
||||
None => None,
|
||||
};
|
||||
(lower, upper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: TryStream + FusedStream> FusedStream for TryChunks<St> {
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_terminated() && self.items.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Item> Sink<Item> for TryChunks<S>
|
||||
where
|
||||
S: TryStream + Sink<Item>,
|
||||
{
|
||||
type Error = <S as Sink<Item>>::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
|
||||
/// Error indicating, that while chunk was collected inner stream produced an error.
|
||||
///
|
||||
/// Contains all items that were collected before an error occurred, and the stream error itself.
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct TryChunksError<T, E>(pub Vec<T>, pub E);
|
||||
|
||||
impl<T, E: fmt::Debug> fmt::Debug for TryChunksError<T, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.1.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: fmt::Display> fmt::Display for TryChunksError<T, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.1.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T, E: fmt::Debug + fmt::Display> std::error::Error for TryChunksError<T, E> {}
|
||||
@@ -0,0 +1,52 @@
|
||||
use core::mem;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{FusedFuture, Future};
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the [`try_collect`](super::TryStreamExt::try_collect) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryCollect<St, C> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
items: C,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: TryStream, C: Default> TryCollect<St, C> {
|
||||
pub(super) fn new(s: St) -> Self {
|
||||
Self { stream: s, items: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, C> FusedFuture for TryCollect<St, C>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
C: Default + Extend<St::Ok>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, C> Future for TryCollect<St, C>
|
||||
where
|
||||
St: TryStream,
|
||||
C: Default + Extend<St::Ok>,
|
||||
{
|
||||
type Output = Result<C, St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
Poll::Ready(Ok(loop {
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
Some(x) => this.items.extend(Some(x)),
|
||||
None => break mem::take(this.items),
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::Future;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the [`try_concat`](super::TryStreamExt::try_concat) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryConcat<St: TryStream> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
accum: Option<St::Ok>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> TryConcat<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: Extend<<St::Ok as IntoIterator>::Item> + IntoIterator + Default,
|
||||
{
|
||||
pub(super) fn new(stream: St) -> Self {
|
||||
Self { stream, accum: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> Future for TryConcat<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: Extend<<St::Ok as IntoIterator>::Item> + IntoIterator + Default,
|
||||
{
|
||||
type Output = Result<St::Ok, St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(Ok(loop {
|
||||
if let Some(x) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
if let Some(a) = this.accum {
|
||||
a.extend(x)
|
||||
} else {
|
||||
*this.accum = Some(x)
|
||||
}
|
||||
} else {
|
||||
break this.accum.take().unwrap_or_default();
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::Future;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_filter`](super::TryStreamExt::try_filter)
|
||||
/// method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryFilter<St, Fut, F>
|
||||
where St: TryStream
|
||||
{
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
#[pin]
|
||||
pending_fut: Option<Fut>,
|
||||
pending_item: Option<St::Ok>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryFilter<St, Fut, F>
|
||||
where
|
||||
St: TryStream + fmt::Debug,
|
||||
St::Ok: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryFilter")
|
||||
.field("stream", &self.stream)
|
||||
.field("pending_fut", &self.pending_fut)
|
||||
.field("pending_item", &self.pending_item)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryFilter<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, pending_fut: None, pending_item: None }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedStream for TryFilter<St, Fut, F>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: Future<Output = bool>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.pending_fut.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Stream for TryFilter<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
Fut: Future<Output = bool>,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
{
|
||||
type Item = Result<St::Ok, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.pending_fut.as_mut().as_pin_mut() {
|
||||
let res = ready!(fut.poll(cx));
|
||||
this.pending_fut.set(None);
|
||||
if res {
|
||||
break this.pending_item.take().map(Ok);
|
||||
}
|
||||
*this.pending_item = None;
|
||||
} else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
this.pending_fut.set(Some((this.f)(&item)));
|
||||
*this.pending_item = Some(item);
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let pending_len = usize::from(self.pending_fut.is_some());
|
||||
let (_, upper) = self.stream.size_hint();
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(pending_len),
|
||||
None => None,
|
||||
};
|
||||
(0, upper) // can't know a lower bound, due to the predicate
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Fut, F, Item, E> Sink<Item> for TryFilter<S, Fut, F>
|
||||
where
|
||||
S: TryStream + Sink<Item, Error = E>,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_filter_map`](super::TryStreamExt::try_filter_map)
|
||||
/// method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryFilterMap<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
#[pin]
|
||||
pending: Option<Fut>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryFilterMap<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryFilterMap")
|
||||
.field("stream", &self.stream)
|
||||
.field("pending", &self.pending)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryFilterMap<St, Fut, F> {
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, pending: None }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St, Fut, F, T> FusedStream for TryFilterMap<St, Fut, F>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
Fut: TryFuture<Ok = Option<T>, Error = St::Error>,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.pending.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F, T> Stream for TryFilterMap<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
Fut: TryFuture<Ok = Option<T>, Error = St::Error>,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
{
|
||||
type Item = Result<T, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(p) = this.pending.as_mut().as_pin_mut() {
|
||||
// We have an item in progress, poll that until it's done
|
||||
let res = ready!(p.try_poll(cx));
|
||||
this.pending.set(None);
|
||||
let item = res?;
|
||||
if item.is_some() {
|
||||
break item.map(Ok);
|
||||
}
|
||||
} else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
// No item in progress, but the stream is still going
|
||||
this.pending.set(Some((this.f)(item)));
|
||||
} else {
|
||||
// The stream is done
|
||||
break None;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let pending_len = usize::from(self.pending.is_some());
|
||||
let (_, upper) = self.stream.size_hint();
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(pending_len),
|
||||
None => None,
|
||||
};
|
||||
(0, upper) // can't know a lower bound, due to the predicate
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Fut, F, Item> Sink<Item> for TryFilterMap<S, Fut, F>
|
||||
where
|
||||
S: Sink<Item>,
|
||||
{
|
||||
type Error = S::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
use core::pin::Pin;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_flatten`](super::TryStreamExt::try_flatten) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryFlatten<St>
|
||||
where
|
||||
St: TryStream,
|
||||
{
|
||||
#[pin]
|
||||
stream: St,
|
||||
#[pin]
|
||||
next: Option<St::Ok>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> TryFlatten<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
pub(super) fn new(stream: St) -> Self {
|
||||
Self { stream, next: None }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St> FusedStream for TryFlatten<St>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
St::Ok: TryStream,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.next.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> Stream for TryFlatten<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
type Item = Result<<St::Ok as TryStream>::Ok, <St::Ok as TryStream>::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(s) = this.next.as_mut().as_pin_mut() {
|
||||
if let Some(item) = ready!(s.try_poll_next(cx)?) {
|
||||
break Some(Ok(item));
|
||||
} else {
|
||||
this.next.set(None);
|
||||
}
|
||||
} else if let Some(s) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
this.next.set(Some(s));
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Item> Sink<Item> for TryFlatten<S>
|
||||
where
|
||||
S: TryStream + Sink<Item>,
|
||||
{
|
||||
type Error = <S as Sink<Item>>::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
use core::marker::PhantomData;
|
||||
use core::pin::Pin;
|
||||
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::future::Either;
|
||||
use crate::stream::stream::flatten_unordered::{
|
||||
FlattenUnorderedWithFlowController, FlowController, FlowStep,
|
||||
};
|
||||
use crate::stream::IntoStream;
|
||||
use crate::TryStreamExt;
|
||||
|
||||
delegate_all!(
|
||||
/// Stream for the [`try_flatten_unordered`](super::TryStreamExt::try_flatten_unordered) method.
|
||||
TryFlattenUnordered<St>(
|
||||
FlattenUnorderedWithFlowController<NestedTryStreamIntoEitherTryStream<St>, PropagateBaseStreamError<St>>
|
||||
): Debug + Sink + Stream + FusedStream + AccessInner[St, (. .)]
|
||||
+ New[
|
||||
|stream: St, limit: impl Into<Option<usize>>|
|
||||
FlattenUnorderedWithFlowController::new(
|
||||
NestedTryStreamIntoEitherTryStream::new(stream),
|
||||
limit.into()
|
||||
)
|
||||
]
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream,
|
||||
St::Ok: Unpin,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>
|
||||
);
|
||||
|
||||
pin_project! {
|
||||
/// Emits either successful streams or single-item streams containing the underlying errors.
|
||||
/// This's a wrapper for `FlattenUnordered` to reuse its logic over `TryStream`.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct NestedTryStreamIntoEitherTryStream<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream,
|
||||
St::Ok: Unpin,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>
|
||||
{
|
||||
#[pin]
|
||||
stream: St
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> NestedTryStreamIntoEitherTryStream<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream + Unpin,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
fn new(stream: St) -> Self {
|
||||
Self { stream }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
/// Emits a single item immediately, then stream will be terminated.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Single<T>(Option<T>);
|
||||
|
||||
impl<T> Single<T> {
|
||||
/// Constructs new `Single` with the given value.
|
||||
fn new(val: T) -> Self {
|
||||
Self(Some(val))
|
||||
}
|
||||
|
||||
/// Attempts to take inner item immediately. Will always succeed if the stream isn't terminated.
|
||||
fn next_immediate(&mut self) -> Option<T> {
|
||||
self.0.take()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Unpin for Single<T> {}
|
||||
|
||||
impl<T> Stream for Single<T> {
|
||||
type Item = T;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
Poll::Ready(self.0.take())
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.0.as_ref().map_or((0, Some(0)), |_| (1, Some(1)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Immediately propagates errors occurred in the base stream.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct PropagateBaseStreamError<St>(PhantomData<St>);
|
||||
|
||||
type BaseStreamItem<St> = <NestedTryStreamIntoEitherTryStream<St> as Stream>::Item;
|
||||
type InnerStreamItem<St> = <BaseStreamItem<St> as Stream>::Item;
|
||||
|
||||
impl<St> FlowController<BaseStreamItem<St>, InnerStreamItem<St>> for PropagateBaseStreamError<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream + Unpin,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
fn next_step(item: BaseStreamItem<St>) -> FlowStep<BaseStreamItem<St>, InnerStreamItem<St>> {
|
||||
match item {
|
||||
// A new successful inner stream received
|
||||
st @ Either::Left(_) => FlowStep::Continue(st),
|
||||
// An error encountered
|
||||
Either::Right(mut err) => FlowStep::Return(err.next_immediate().unwrap()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type SingleStreamResult<St> = Single<Result<<St as TryStream>::Ok, <St as TryStream>::Error>>;
|
||||
|
||||
impl<St> Stream for NestedTryStreamIntoEitherTryStream<St>
|
||||
where
|
||||
St: TryStream,
|
||||
St::Ok: TryStream + Unpin,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
// Item is either an inner stream or a stream containing a single error.
|
||||
// This will allow using `Either`'s `Stream` implementation as both branches are actually streams of `Result`'s.
|
||||
type Item = Either<IntoStream<St::Ok>, SingleStreamResult<St::Ok>>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let item = ready!(self.project().stream.try_poll_next(cx));
|
||||
|
||||
let out = match item {
|
||||
Some(res) => match res {
|
||||
// Emit successful inner stream as is
|
||||
Ok(stream) => Either::Left(stream.into_stream()),
|
||||
// Wrap an error into a stream containing a single item
|
||||
err @ Err(_) => {
|
||||
let res = err.map(|_: St::Ok| unreachable!()).map_err(Into::into);
|
||||
|
||||
Either::Right(Single::new(res))
|
||||
}
|
||||
},
|
||||
None => return Poll::Ready(None),
|
||||
};
|
||||
|
||||
Poll::Ready(Some(out))
|
||||
}
|
||||
}
|
||||
|
||||
impl<St> FusedStream for NestedTryStreamIntoEitherTryStream<St>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
St::Ok: TryStream + Unpin,
|
||||
<St::Ok as TryStream>::Error: From<St::Error>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<St, Item> Sink<Item> for NestedTryStreamIntoEitherTryStream<St>
|
||||
where
|
||||
St: TryStream + Sink<Item>,
|
||||
St::Ok: TryStream + Unpin,
|
||||
<St::Ok as TryStream>::Error: From<<St as TryStream>::Error>,
|
||||
{
|
||||
type Error = <St as Sink<Item>>::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{FusedFuture, Future, TryFuture};
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the [`try_fold`](super::TryStreamExt::try_fold) method.
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryFold<St, Fut, T, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
accum: Option<T>,
|
||||
#[pin]
|
||||
future: Option<Fut>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, T, F> fmt::Debug for TryFold<St, Fut, T, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
T: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryFold")
|
||||
.field("stream", &self.stream)
|
||||
.field("accum", &self.accum)
|
||||
.field("future", &self.future)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, T, F> TryFold<St, Fut, T, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(T, St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = T, Error = St::Error>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F, t: T) -> Self {
|
||||
Self { stream, f, accum: Some(t), future: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, T, F> FusedFuture for TryFold<St, Fut, T, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(T, St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = T, Error = St::Error>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.accum.is_none() && self.future.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, T, F> Future for TryFold<St, Fut, T, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(T, St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = T, Error = St::Error>,
|
||||
{
|
||||
type Output = Result<T, St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.future.as_mut().as_pin_mut() {
|
||||
// we're currently processing a future to produce a new accum value
|
||||
let res = ready!(fut.try_poll(cx));
|
||||
this.future.set(None);
|
||||
match res {
|
||||
Ok(a) => *this.accum = Some(a),
|
||||
Err(e) => break Err(e),
|
||||
}
|
||||
} else if this.accum.is_some() {
|
||||
// we're waiting on a new item from the stream
|
||||
let res = ready!(this.stream.as_mut().try_poll_next(cx));
|
||||
let a = this.accum.take().unwrap();
|
||||
match res {
|
||||
Some(Ok(item)) => this.future.set(Some((this.f)(a, item))),
|
||||
Some(Err(e)) => break Err(e),
|
||||
None => break Ok(a),
|
||||
}
|
||||
} else {
|
||||
panic!("Fold polled after completion")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{Future, TryFuture};
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the [`try_for_each`](super::TryStreamExt::try_for_each) method.
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryForEach<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
#[pin]
|
||||
future: Option<Fut>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryForEach<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryForEach")
|
||||
.field("stream", &self.stream)
|
||||
.field("future", &self.future)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryForEach<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = (), Error = St::Error>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, future: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Future for TryForEach<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = (), Error = St::Error>,
|
||||
{
|
||||
type Output = Result<(), St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
loop {
|
||||
if let Some(fut) = this.future.as_mut().as_pin_mut() {
|
||||
ready!(fut.try_poll(cx))?;
|
||||
this.future.set(None);
|
||||
} else {
|
||||
match ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
Some(e) => this.future.set(Some((this.f)(e))),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
use crate::stream::{FuturesUnordered, StreamExt};
|
||||
use core::fmt;
|
||||
use core::num::NonZeroUsize;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{FusedFuture, Future};
|
||||
use futures_core::stream::TryStream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future for the
|
||||
/// [`try_for_each_concurrent`](super::TryStreamExt::try_for_each_concurrent)
|
||||
/// method.
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryForEachConcurrent<St, Fut, F> {
|
||||
#[pin]
|
||||
stream: Option<St>,
|
||||
f: F,
|
||||
futures: FuturesUnordered<Fut>,
|
||||
limit: Option<NonZeroUsize>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryForEachConcurrent<St, Fut, F>
|
||||
where
|
||||
St: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryForEachConcurrent")
|
||||
.field("stream", &self.stream)
|
||||
.field("futures", &self.futures)
|
||||
.field("limit", &self.limit)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedFuture for TryForEachConcurrent<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = Result<(), St::Error>>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_none() && self.futures.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryForEachConcurrent<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = Result<(), St::Error>>,
|
||||
{
|
||||
pub(super) fn new(stream: St, limit: Option<usize>, f: F) -> Self {
|
||||
Self {
|
||||
stream: Some(stream),
|
||||
// Note: `limit` = 0 gets ignored.
|
||||
limit: limit.and_then(NonZeroUsize::new),
|
||||
f,
|
||||
futures: FuturesUnordered::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Future for TryForEachConcurrent<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(St::Ok) -> Fut,
|
||||
Fut: Future<Output = Result<(), St::Error>>,
|
||||
{
|
||||
type Output = Result<(), St::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
loop {
|
||||
let mut made_progress_this_iter = false;
|
||||
|
||||
// Check if we've already created a number of futures greater than `limit`
|
||||
if this.limit.map(|limit| limit.get() > this.futures.len()).unwrap_or(true) {
|
||||
let poll_res = match this.stream.as_mut().as_pin_mut() {
|
||||
Some(stream) => stream.try_poll_next(cx),
|
||||
None => Poll::Ready(None),
|
||||
};
|
||||
|
||||
let elem = match poll_res {
|
||||
Poll::Ready(Some(Ok(elem))) => {
|
||||
made_progress_this_iter = true;
|
||||
Some(elem)
|
||||
}
|
||||
Poll::Ready(None) => {
|
||||
this.stream.set(None);
|
||||
None
|
||||
}
|
||||
Poll::Pending => None,
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
// Empty the stream and futures so that we know
|
||||
// the future has completed.
|
||||
this.stream.set(None);
|
||||
drop(core::mem::take(this.futures));
|
||||
return Poll::Ready(Err(e));
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(elem) = elem {
|
||||
this.futures.push((this.f)(elem));
|
||||
}
|
||||
}
|
||||
|
||||
match this.futures.poll_next_unpin(cx) {
|
||||
Poll::Ready(Some(Ok(()))) => made_progress_this_iter = true,
|
||||
Poll::Ready(None) => {
|
||||
if this.stream.is_none() {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
Poll::Pending => {}
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
// Empty the stream and futures so that we know
|
||||
// the future has completed.
|
||||
this.stream.set(None);
|
||||
drop(core::mem::take(this.futures));
|
||||
return Poll::Ready(Err(e));
|
||||
}
|
||||
}
|
||||
|
||||
if !made_progress_this_iter {
|
||||
return Poll::Pending;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
use crate::stream::TryStreamExt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::{FusedFuture, Future};
|
||||
use futures_core::stream::{FusedStream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
|
||||
/// Future for the [`try_next`](super::TryStreamExt::try_next) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct TryNext<'a, St: ?Sized> {
|
||||
stream: &'a mut St,
|
||||
}
|
||||
|
||||
impl<St: ?Sized + Unpin> Unpin for TryNext<'_, St> {}
|
||||
|
||||
impl<'a, St: ?Sized + TryStream + Unpin> TryNext<'a, St> {
|
||||
pub(super) fn new(stream: &'a mut St) -> Self {
|
||||
Self { stream }
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: ?Sized + TryStream + Unpin + FusedStream> FusedFuture for TryNext<'_, St> {
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: ?Sized + TryStream + Unpin> Future for TryNext<'_, St> {
|
||||
type Output = Result<Option<St::Ok>, St::Error>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
self.stream.try_poll_next_unpin(cx)?.map(Ok)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
use crate::stream::{Fuse, IntoStream, StreamExt};
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_ready_chunks`](super::TryStreamExt::try_ready_chunks) method.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryReadyChunks<St: TryStream> {
|
||||
#[pin]
|
||||
stream: Fuse<IntoStream<St>>,
|
||||
cap: usize, // https://github.com/rust-lang/futures-rs/issues/1475
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: TryStream> TryReadyChunks<St> {
|
||||
pub(super) fn new(stream: St, capacity: usize) -> Self {
|
||||
assert!(capacity > 0);
|
||||
|
||||
Self { stream: IntoStream::new(stream).fuse(), cap: capacity }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, (. .));
|
||||
}
|
||||
|
||||
type TryReadyChunksStreamError<St> =
|
||||
TryReadyChunksError<<St as TryStream>::Ok, <St as TryStream>::Error>;
|
||||
|
||||
impl<St: TryStream> Stream for TryReadyChunks<St> {
|
||||
type Item = Result<Vec<St::Ok>, TryReadyChunksStreamError<St>>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.as_mut().project();
|
||||
|
||||
let mut items: Vec<St::Ok> = Vec::new();
|
||||
|
||||
loop {
|
||||
match this.stream.as_mut().poll_next(cx) {
|
||||
// Flush all the collected data if the underlying stream doesn't
|
||||
// contain more ready values
|
||||
Poll::Pending => {
|
||||
return if items.is_empty() {
|
||||
Poll::Pending
|
||||
} else {
|
||||
Poll::Ready(Some(Ok(items)))
|
||||
}
|
||||
}
|
||||
|
||||
// Push the ready item into the buffer and check whether it is full.
|
||||
// If so, return the buffer.
|
||||
Poll::Ready(Some(Ok(item))) => {
|
||||
if items.is_empty() {
|
||||
items.reserve_exact(*this.cap);
|
||||
}
|
||||
items.push(item);
|
||||
if items.len() >= *this.cap {
|
||||
return Poll::Ready(Some(Ok(items)));
|
||||
}
|
||||
}
|
||||
|
||||
// Return the already collected items and the error.
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
return Poll::Ready(Some(Err(TryReadyChunksError(items, e))));
|
||||
}
|
||||
|
||||
// Since the underlying stream ran out of values, return what we
|
||||
// have buffered, if we have anything.
|
||||
Poll::Ready(None) => {
|
||||
let last = if items.is_empty() { None } else { Some(Ok(items)) };
|
||||
return Poll::Ready(last);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let (lower, upper) = self.stream.size_hint();
|
||||
let lower = lower / self.cap;
|
||||
(lower, upper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<St: TryStream + FusedStream> FusedStream for TryReadyChunks<St> {
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Item> Sink<Item> for TryReadyChunks<S>
|
||||
where
|
||||
S: TryStream + Sink<Item>,
|
||||
{
|
||||
type Error = <S as Sink<Item>>::Error;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
|
||||
/// Error indicating, that while chunk was collected inner stream produced an error.
|
||||
///
|
||||
/// Contains all items that were collected before an error occurred, and the stream error itself.
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct TryReadyChunksError<T, E>(pub Vec<T>, pub E);
|
||||
|
||||
impl<T, E: fmt::Debug> fmt::Debug for TryReadyChunksError<T, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.1.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: fmt::Display> fmt::Display for TryReadyChunksError<T, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.1.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T, E: fmt::Debug + fmt::Display> std::error::Error for TryReadyChunksError<T, E> {}
|
||||
@@ -0,0 +1,120 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_skip_while`](super::TryStreamExt::try_skip_while)
|
||||
/// method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TrySkipWhile<St, Fut, F> where St: TryStream {
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
#[pin]
|
||||
pending_fut: Option<Fut>,
|
||||
pending_item: Option<St::Ok>,
|
||||
done_skipping: bool,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TrySkipWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream + fmt::Debug,
|
||||
St::Ok: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TrySkipWhile")
|
||||
.field("stream", &self.stream)
|
||||
.field("pending_fut", &self.pending_fut)
|
||||
.field("pending_item", &self.pending_item)
|
||||
.field("done_skipping", &self.done_skipping)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TrySkipWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = bool, Error = St::Error>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, pending_fut: None, pending_item: None, done_skipping: false }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Stream for TrySkipWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = bool, Error = St::Error>,
|
||||
{
|
||||
type Item = Result<St::Ok, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
if *this.done_skipping {
|
||||
return this.stream.try_poll_next(cx);
|
||||
}
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.pending_fut.as_mut().as_pin_mut() {
|
||||
let res = ready!(fut.try_poll(cx));
|
||||
this.pending_fut.set(None);
|
||||
let skipped = res?;
|
||||
let item = this.pending_item.take();
|
||||
if !skipped {
|
||||
*this.done_skipping = true;
|
||||
break item.map(Ok);
|
||||
}
|
||||
} else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
this.pending_fut.set(Some((this.f)(&item)));
|
||||
*this.pending_item = Some(item);
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let pending_len = usize::from(self.pending_item.is_some());
|
||||
let (_, upper) = self.stream.size_hint();
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(pending_len),
|
||||
None => None,
|
||||
};
|
||||
(0, upper) // can't know a lower bound, due to the predicate
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedStream for TrySkipWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = bool, Error = St::Error>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.pending_item.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Fut, F, Item, E> Sink<Item> for TrySkipWhile<S, Fut, F>
|
||||
where
|
||||
S: TryStream + Sink<Item, Error = E>,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::{FusedStream, Stream, TryStream};
|
||||
use futures_core::task::{Context, Poll};
|
||||
#[cfg(feature = "sink")]
|
||||
use futures_sink::Sink;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_take_while`](super::TryStreamExt::try_take_while)
|
||||
/// method.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryTakeWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
{
|
||||
#[pin]
|
||||
stream: St,
|
||||
f: F,
|
||||
#[pin]
|
||||
pending_fut: Option<Fut>,
|
||||
pending_item: Option<St::Ok>,
|
||||
done_taking: bool,
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> fmt::Debug for TryTakeWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream + fmt::Debug,
|
||||
St::Ok: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryTakeWhile")
|
||||
.field("stream", &self.stream)
|
||||
.field("pending_fut", &self.pending_fut)
|
||||
.field("pending_item", &self.pending_item)
|
||||
.field("done_taking", &self.done_taking)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> TryTakeWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = bool, Error = St::Error>,
|
||||
{
|
||||
pub(super) fn new(stream: St, f: F) -> Self {
|
||||
Self { stream, f, pending_fut: None, pending_item: None, done_taking: false }
|
||||
}
|
||||
|
||||
delegate_access_inner!(stream, St, ());
|
||||
}
|
||||
|
||||
impl<St, Fut, F> Stream for TryTakeWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = bool, Error = St::Error>,
|
||||
{
|
||||
type Item = Result<St::Ok, St::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
if *this.done_taking {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
|
||||
Poll::Ready(loop {
|
||||
if let Some(fut) = this.pending_fut.as_mut().as_pin_mut() {
|
||||
let res = ready!(fut.try_poll(cx));
|
||||
this.pending_fut.set(None);
|
||||
let take = res?;
|
||||
let item = this.pending_item.take();
|
||||
if take {
|
||||
break item.map(Ok);
|
||||
} else {
|
||||
*this.done_taking = true;
|
||||
break None;
|
||||
}
|
||||
} else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) {
|
||||
this.pending_fut.set(Some((this.f)(&item)));
|
||||
*this.pending_item = Some(item);
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
if self.done_taking {
|
||||
return (0, Some(0));
|
||||
}
|
||||
|
||||
let pending_len = usize::from(self.pending_item.is_some());
|
||||
let (_, upper) = self.stream.size_hint();
|
||||
let upper = match upper {
|
||||
Some(x) => x.checked_add(pending_len),
|
||||
None => None,
|
||||
};
|
||||
(0, upper) // can't know a lower bound, due to the predicate
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, Fut, F> FusedStream for TryTakeWhile<St, Fut, F>
|
||||
where
|
||||
St: TryStream + FusedStream,
|
||||
F: FnMut(&St::Ok) -> Fut,
|
||||
Fut: TryFuture<Ok = bool, Error = St::Error>,
|
||||
{
|
||||
fn is_terminated(&self) -> bool {
|
||||
self.done_taking || self.pending_item.is_none() && self.stream.is_terminated()
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impl of Sink from the underlying stream
|
||||
#[cfg(feature = "sink")]
|
||||
impl<S, Fut, F, Item, E> Sink<Item> for TryTakeWhile<S, Fut, F>
|
||||
where
|
||||
S: TryStream + Sink<Item, Error = E>,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
delegate_sink!(stream, Item);
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
use super::assert_stream;
|
||||
use core::fmt;
|
||||
use core::pin::Pin;
|
||||
use futures_core::future::TryFuture;
|
||||
use futures_core::ready;
|
||||
use futures_core::stream::Stream;
|
||||
use futures_core::task::{Context, Poll};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
/// Creates a `TryStream` from a seed and a closure returning a `TryFuture`.
|
||||
///
|
||||
/// This function is the dual for the `TryStream::try_fold()` adapter: while
|
||||
/// `TryStream::try_fold()` reduces a `TryStream` to one single value,
|
||||
/// `try_unfold()` creates a `TryStream` from a seed value.
|
||||
///
|
||||
/// `try_unfold()` will call the provided closure with the provided seed, then
|
||||
/// wait for the returned `TryFuture` to complete with `(a, b)`. It will then
|
||||
/// yield the value `a`, and use `b` as the next internal state.
|
||||
///
|
||||
/// If the closure returns `None` instead of `Some(TryFuture)`, then the
|
||||
/// `try_unfold()` will stop producing items and return `Poll::Ready(None)` in
|
||||
/// future calls to `poll()`.
|
||||
///
|
||||
/// In case of error generated by the returned `TryFuture`, the error will be
|
||||
/// returned by the `TryStream`. The `TryStream` will then yield
|
||||
/// `Poll::Ready(None)` in future calls to `poll()`.
|
||||
///
|
||||
/// This function can typically be used when wanting to go from the "world of
|
||||
/// futures" to the "world of streams": the provided closure can build a
|
||||
/// `TryFuture` using other library functions working on futures, and
|
||||
/// `try_unfold()` will turn it into a `TryStream` by repeating the operation.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # #[derive(Debug, PartialEq)]
|
||||
/// # struct SomeError;
|
||||
/// # futures::executor::block_on(async {
|
||||
/// use futures::stream::{self, TryStreamExt};
|
||||
///
|
||||
/// let stream = stream::try_unfold(0, |state| async move {
|
||||
/// if state < 0 {
|
||||
/// return Err(SomeError);
|
||||
/// }
|
||||
///
|
||||
/// if state <= 2 {
|
||||
/// let next_state = state + 1;
|
||||
/// let yielded = state * 2;
|
||||
/// Ok(Some((yielded, next_state)))
|
||||
/// } else {
|
||||
/// Ok(None)
|
||||
/// }
|
||||
/// });
|
||||
///
|
||||
/// let result: Result<Vec<i32>, _> = stream.try_collect().await;
|
||||
/// assert_eq!(result, Ok(vec![0, 2, 4]));
|
||||
/// # });
|
||||
/// ```
|
||||
pub fn try_unfold<T, F, Fut, Item>(init: T, f: F) -> TryUnfold<T, F, Fut>
|
||||
where
|
||||
F: FnMut(T) -> Fut,
|
||||
Fut: TryFuture<Ok = Option<(Item, T)>>,
|
||||
{
|
||||
assert_stream::<Result<Item, Fut::Error>, _>(TryUnfold { f, state: Some(init), fut: None })
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
/// Stream for the [`try_unfold`] function.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
pub struct TryUnfold<T, F, Fut> {
|
||||
f: F,
|
||||
state: Option<T>,
|
||||
#[pin]
|
||||
fut: Option<Fut>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, Fut> fmt::Debug for TryUnfold<T, F, Fut>
|
||||
where
|
||||
T: fmt::Debug,
|
||||
Fut: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TryUnfold").field("state", &self.state).field("fut", &self.fut).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, Fut, Item> Stream for TryUnfold<T, F, Fut>
|
||||
where
|
||||
F: FnMut(T) -> Fut,
|
||||
Fut: TryFuture<Ok = Option<(Item, T)>>,
|
||||
{
|
||||
type Item = Result<Item, Fut::Error>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let mut this = self.project();
|
||||
|
||||
if let Some(state) = this.state.take() {
|
||||
this.fut.set(Some((this.f)(state)));
|
||||
}
|
||||
|
||||
match this.fut.as_mut().as_pin_mut() {
|
||||
None => {
|
||||
// The future previously errored
|
||||
Poll::Ready(None)
|
||||
}
|
||||
Some(future) => {
|
||||
let step = ready!(future.try_poll(cx));
|
||||
this.fut.set(None);
|
||||
|
||||
match step {
|
||||
Ok(Some((item, next_state))) => {
|
||||
*this.state = Some(next_state);
|
||||
Poll::Ready(Some(Ok(item)))
|
||||
}
|
||||
Ok(None) => Poll::Ready(None),
|
||||
Err(e) => Poll::Ready(Some(Err(e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user