-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit futures
dependency to Stream
via feature flag
#1774
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cd0e973
remove `futures` dependency from `tokio`
carllerche 8cef1fa
fmt
carllerche 19006c3
fix test
carllerche fb7f606
try to fix windows
carllerche 4ae6d4b
bring back futures_core::Stream dep as feature flag
carllerche b419acc
fix tests
carllerche 9671b4c
fmt
carllerche b6791cd
try to fix ci again
carllerche 97371c6
try to fix ci again
carllerche 39ea191
try to fix windows again
carllerche 0d80e87
try again
carllerche 53278d5
and again
carllerche ab2bbcd
fix windows (hopefully for real, i ran it locally this time)
carllerche ac047a2
dox
carllerche 5b962cc
fmt
carllerche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 @@ | ||
mod read_dir; |
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,19 @@ | ||
use crate::stream::Stream; | ||
use tokio::fs::{DirEntry, ReadDir}; | ||
|
||
use futures_core::ready; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
impl Stream for ReadDir { | ||
type Item = io::Result<DirEntry>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
Poll::Ready(match ready!(self.poll_next_entry(cx)) { | ||
Ok(Some(entry)) => Some(Ok(entry)), | ||
Ok(None) => None, | ||
Err(err) => Some(Err(err)), | ||
}) | ||
} | ||
} |
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,22 @@ | ||
use crate::stream::Stream; | ||
use tokio::io::{AsyncBufRead, Lines}; | ||
|
||
use futures_core::ready; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
impl<R> Stream for Lines<R> | ||
where | ||
R: AsyncBufRead + Unpin, | ||
{ | ||
type Item = io::Result<String>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
match ready!(self.poll_next_line(cx))? { | ||
Some(line) => Poll::Ready(Some(Ok(line))), | ||
None => Poll::Ready(None), | ||
} | ||
} | ||
} | ||
|
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 @@ | ||
mod split; |
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,21 @@ | ||
use crate::stream::Stream; | ||
use tokio::io::{AsyncBufRead, Split}; | ||
|
||
use futures_core::ready; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
impl<R> Stream for Split<R> | ||
where | ||
R: AsyncBufRead + Unpin, | ||
{ | ||
type Item = io::Result<Vec<u8>>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
match ready!(self.poll_next_segment(cx))? { | ||
Some(segment) => Poll::Ready(Some(Ok(segment))), | ||
None => Poll::Ready(None), | ||
} | ||
} | ||
} |
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
Empty file.
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,19 @@ | ||
use crate::stream::Stream; | ||
|
||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
/// Stream for the [`into_std`](super::Stream::into_std) method. | ||
#[derive(Debug)] | ||
pub struct IntoStd<T> { | ||
pub(super) stream: T, | ||
} | ||
|
||
impl<T: Stream> futures_core::Stream for IntoStd<T> { | ||
type Item = T::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let stream = unsafe { self.map_unchecked_mut(|me| &mut me.stream) }; | ||
stream.poll_next(cx) | ||
} | ||
} | ||
carllerche marked this conversation as resolved.
Show resolved
Hide resolved
|
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,117 @@ | ||
//! Asynchronous value iteration. | ||
|
||
mod into_std; | ||
pub use into_std::IntoStd; | ||
|
||
use std::ops::DerefMut; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
/// A stream of values produced asynchronously. | ||
/// | ||
/// This trait is used to convert Tokio's "stream-like" types into | ||
/// `futures::Stream`. This trait is not intended to be implemented by third | ||
/// parties (use `futures::Stream` instead) and only exists to satisfy Rust's | ||
carllerche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// coherence requirements. When `Stream` is stabilized in `std`, this trait | ||
/// will be removed and `tokio` will directly implement the `std` trait. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub trait Stream { | ||
/// Values yielded by the stream. | ||
type Item; | ||
|
||
/// Attempt to pull out the next value of this stream, registering the | ||
/// current task for wakeup if the value is not yet available, and returning | ||
/// `None` if the stream is exhausted. | ||
/// | ||
/// # Return value | ||
/// | ||
/// There are several possible return values, each indicating a distinct | ||
/// stream state: | ||
/// | ||
/// - `Poll::Pending` means that this stream's next value is not ready | ||
/// yet. Implementations will ensure that the current task will be notified | ||
/// when the next value may be ready. | ||
/// | ||
/// - `Poll::Ready(Some(val))` means that the stream has successfully | ||
/// produced a value, `val`, and may produce further values on subsequent | ||
/// `poll_next` calls. | ||
/// | ||
/// - `Poll::Ready(None)` means that the stream has terminated, and | ||
/// `poll_next` should not be invoked again. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Once a stream is finished, i.e. `Ready(None)` has been returned, further | ||
/// calls to `poll_next` may result in a panic or other "bad behavior". If | ||
/// this is difficult to guard against then the `fuse` adapter can be used | ||
/// to ensure that `poll_next` always returns `Ready(None)` in subsequent | ||
/// calls. | ||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>; | ||
|
||
/// Returns the bounds on the remaining length of the stream. | ||
/// | ||
/// Specifically, `size_hint()` returns a tuple where the first element | ||
/// is the lower bound, and the second element is the upper bound. | ||
/// | ||
/// The second half of the tuple that is returned is an [`Option`]`<`[`usize`]`>`. | ||
/// A [`None`] here means that either there is no known upper bound, or the | ||
/// upper bound is larger than [`usize`]. | ||
/// | ||
/// # Implementation notes | ||
/// | ||
/// It is not enforced that a stream implementation yields the declared | ||
/// number of elements. A buggy stream may yield less than the lower bound | ||
/// or more than the upper bound of elements. | ||
/// | ||
/// `size_hint()` is primarily intended to be used for optimizations such as | ||
/// reserving space for the elements of the stream, but must not be | ||
/// trusted to e.g., omit bounds checks in unsafe code. An incorrect | ||
/// implementation of `size_hint()` should not lead to memory safety | ||
/// violations. | ||
/// | ||
/// That said, the implementation should provide a correct estimation, | ||
/// because otherwise it would be a violation of the trait's protocol. | ||
/// | ||
/// The default implementation returns `(0, `[`None`]`)` which is correct for any | ||
/// stream. | ||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(0, None) | ||
} | ||
|
||
/// Convert the stream into a `futures::Stream` type. | ||
fn into_std(self) -> IntoStd<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
IntoStd { stream: self } | ||
} | ||
} | ||
|
||
impl<S: ?Sized + Stream + Unpin> Stream for &mut S { | ||
type Item = S::Item; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
S::poll_next(Pin::new(&mut **self), cx) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(**self).size_hint() | ||
} | ||
} | ||
|
||
impl<P> Stream for Pin<P> | ||
where | ||
P: DerefMut + Unpin, | ||
P::Target: Stream, | ||
{ | ||
type Item = <P::Target as Stream>::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
self.get_mut().as_mut().poll_next(cx) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(**self).size_hint() | ||
} | ||
} |
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,2 @@ | ||
mod mpsc; | ||
mod watch; |
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,21 @@ | ||
use crate::stream::Stream; | ||
use tokio::sync::mpsc::{Receiver, UnboundedReceiver}; | ||
|
||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
impl<T> Stream for Receiver<T> { | ||
type Item = T; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
self.get_mut().poll_recv(cx) | ||
} | ||
} | ||
|
||
impl<T> Stream for UnboundedReceiver<T> { | ||
type Item = T; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
self.get_mut().poll_recv(cx) | ||
} | ||
} |
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,17 @@ | ||
use crate::stream::Stream; | ||
use tokio::sync::watch::Receiver; | ||
|
||
use futures_core::ready; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
impl<T: Clone> Stream for Receiver<T> { | ||
type Item = T; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { | ||
let ret = ready!(self.poll_recv_ref(cx)); | ||
|
||
#[allow(clippy::map_clone)] | ||
Poll::Ready(ret.map(|v_ref| v_ref.clone())) | ||
} | ||
} |
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,15 @@ | ||
use crate::stream::Stream; | ||
use tokio::time::{Instant, Interval}; | ||
|
||
use futures_core::ready; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
impl Stream for Interval { | ||
type Item = Instant; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Instant>> { | ||
let instant = ready!(self.get_mut().poll_tick(cx)); | ||
Poll::Ready(Some(instant)) | ||
} | ||
} |
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 @@ | ||
mod interval; |
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,44 @@ | ||
#![warn(rust_2018_idioms)] | ||
|
||
use tokio::sync::watch; | ||
use tokio_test::task::spawn; | ||
use tokio_test::{assert_pending, assert_ready}; | ||
use tokio_util::stream::Stream; | ||
|
||
use futures::prelude::*; | ||
|
||
#[test] | ||
fn stream_impl() { | ||
let (tx, rx) = watch::channel("one"); | ||
let mut rx = rx.into_std(); | ||
|
||
{ | ||
let mut t = spawn(rx.next()); | ||
let v = assert_ready!(t.poll()).unwrap(); | ||
assert_eq!(v, "one"); | ||
} | ||
|
||
{ | ||
let mut t = spawn(rx.next()); | ||
|
||
assert_pending!(t.poll()); | ||
|
||
tx.broadcast("two").unwrap(); | ||
|
||
assert!(t.is_woken()); | ||
|
||
let v = assert_ready!(t.poll()).unwrap(); | ||
assert_eq!(v, "two"); | ||
} | ||
|
||
{ | ||
let mut t = spawn(rx.next()); | ||
|
||
assert_pending!(t.poll()); | ||
|
||
drop(tx); | ||
|
||
let res = assert_ready!(t.poll()); | ||
assert!(res.is_none()); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delay
was renameddelay_until
to further disambiguate fromdelay_for
.