-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add zip and enumerate #9
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use core::pin::Pin; | ||
use core::task::{Context, Poll}; | ||
|
||
use async_std::task::ready; | ||
use pin_project_lite::pin_project; | ||
|
||
use crate::ParallelStream; | ||
|
||
pin_project! { | ||
/// A stream that yields the current count and element. | ||
/// | ||
/// This `struct` is created by the [`enumerate`] method on [`ParallelStream`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`enumerate`]: trait.ParallelStream.html#method.enumerate | ||
/// [`ParallelStream`]: trait.ParallelStream.html | ||
#[derive(Clone, Debug)] | ||
pub struct Enumerate<S> { | ||
#[pin] | ||
stream: S, | ||
count: usize, | ||
limit: Option<usize>, | ||
} | ||
} | ||
|
||
impl<S: ParallelStream> Enumerate<S> { | ||
pub(super) fn new(stream: S) -> Self { | ||
Self { | ||
limit: stream.get_limit(), | ||
count: 0, | ||
stream, | ||
} | ||
} | ||
} | ||
|
||
impl<S: ParallelStream> ParallelStream for Enumerate<S> { | ||
type Item = (usize, S::Item); | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let this = self.project(); | ||
let next = ready!(this.stream.poll_next(cx)); | ||
*this.count += 1; | ||
let count = *this.count; | ||
Poll::Ready(next.map(|val| (count, val))) | ||
} | ||
|
||
fn limit(mut self, limit: impl Into<Option<usize>>) -> Self { | ||
self.limit = limit.into(); | ||
self | ||
} | ||
|
||
fn get_limit(&self) -> Option<usize> { | ||
self.limit | ||
} | ||
} | ||
|
||
#[async_std::test] | ||
async fn smoke() { | ||
use async_std::prelude::*; | ||
let s = async_std::stream::repeat(5usize).enumerate().take(3); | ||
let mut output = vec![]; | ||
let mut stream = crate::from_stream(s); | ||
while let Some(n) = stream.next().await { | ||
output.push(n); | ||
} | ||
assert_eq!(output, vec![(0, 5), (1, 5), (2, 5)]); | ||
} |
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,73 @@ | ||
use core::cmp; | ||
use core::pin::Pin; | ||
use core::task::{Context, Poll}; | ||
|
||
use async_std::task::ready; | ||
use pin_project_lite::pin_project; | ||
|
||
use crate::ParallelStream; | ||
|
||
pin_project! { | ||
/// A stream that yields two streams simultaneously. | ||
/// | ||
/// This `struct` is created by the [`zip`] method on [`ParallelStream`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`zip`]: trait.ParallelStream.html#method.zip | ||
/// [`ParallelStream`]: trait.ParallelStream.html | ||
#[derive(Clone, Debug)] | ||
pub struct Zip<A, B> { | ||
#[pin] | ||
stream: A, | ||
#[pin] | ||
other: B, | ||
limit: Option<usize>, | ||
} | ||
} | ||
|
||
impl<A: ParallelStream, B: ParallelStream> Zip<A, B> { | ||
pub(super) fn new(stream: A, other: B) -> Self { | ||
Self { | ||
limit: cmp::min(stream.get_limit(), other.get_limit()), | ||
other, | ||
stream, | ||
} | ||
} | ||
} | ||
|
||
impl<A: ParallelStream, B: ParallelStream> ParallelStream for Zip<A, B> { | ||
type Item = (A::Item, B::Item); | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let this = self.project(); | ||
let next = ready!(this.stream.poll_next(cx)); | ||
let other_next = ready!(this.other.poll_next(cx)); | ||
match (next, other_next) { | ||
(Some(a), Some(b)) => Poll::Ready(Some((a, b))), | ||
_ => Poll::Ready(None), | ||
} | ||
} | ||
|
||
fn limit(mut self, limit: impl Into<Option<usize>>) -> Self { | ||
self.limit = limit.into(); | ||
self | ||
} | ||
|
||
fn get_limit(&self) -> Option<usize> { | ||
self.limit | ||
} | ||
} | ||
|
||
#[async_std::test] | ||
async fn smoke() { | ||
use async_std::prelude::*; | ||
let s = async_std::stream::repeat(5usize) | ||
.zip(async_std::stream::repeat(10usize)) | ||
.take(3); | ||
let mut output = vec![]; | ||
let mut stream = crate::from_stream(s); | ||
while let Some(n) = stream.next().await { | ||
output.push(n); | ||
} | ||
assert_eq!(output, vec![(5, 10), (5, 10), (5, 10)]); | ||
} |
Empty file.
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.
This will drop items when
this.stream
is ready butthis.other
is not. In this case, I suggest you store the result ofthis.stream.poll_next(cx)
and use that stored value during the next poll (see here).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.
Ah right, good point.
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.
A straightforward port of the above into
ParallelStream
requires theParallelStream::Item
s to beSync
. I'm not quite sure why, I think because they aren'tFused
?