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.
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
Update stream rfc #15
Update stream rfc #15
Changes from 14 commits
7ede7b7
3ccfcb0
b5f7031
f6cd705
a6a69a0
81a2d51
7c95855
9f6aa05
1750343
0050a3e
9d48ced
8d19261
06205c4
d646a7e
c0bbd43
8cf7b37
df39f12
a13dd48
e398679
d41c12b
ddec9e0
4bcc194
6d06d3f
35efa81
642935a
b9c8bff
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
One very open-ended question I haven't thought too hard about yet: Would it ever be possible to automatically implement this for all T where
T: FromIterator
? It seems like, in most cases, the implementation should be logically equivalent, with the (rather large) wrinkle that now callingnext()
suspends you :).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.
I definitely think that's possible. To clarify, are you thinking of implementing this same trait for both Streams and Iterators, or are you thinking of having separate traits with very similar functionality?
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.
I'm saying that
FromStream
(as defined here) and the existingFromIterator
trait are separate, but with very similar functionality.Thinking more about it,
FromStream
is actually more general, because an await point is allowed to suspend execution of the current function but doesn't actually have to. So many, but not all, existing impls ofFromIterator
would work forFromStream
as well. If not for coherence with existing impls, we could doimpl<T: FromStream<Item = I>> FromIterator<I> for T
, and switch a bunch ofFromIterator
impls to beFromStream
impls without having to repeat ourselves.This is a somewhat similar problem to the
LendingStream
/Stream
relationship below, but the relationship is reversed (the trait impls we're starting with are allowed to assume too much – that there are no suspend points – rather than too little).This might be another possible bullet point for the lang team discussion, @nikomatsakis? (cc rust-lang/lang-team#34)
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.
The way we solved this in
async-std
is by manually copying all implementations ofFromIterator
from std (ref). When we choose to introducestd::stream::FromStream
we could pretty much copy-paste all of the impls fromasync-std
. I forget whether there were coherence issues (likely), but the main reason why we took this approach is because doing general conversions between sync and async code is hard to get right in practice:sync -> async
means operations may quietly block in the background which can lead to perf issues in runtimesasync -> sync
means using methods such astask::block_on
which is hard to tune, or risk deadlocks when nested (both futures-rs and async-std have had issues with this)To help with the conversion from
Iterator
->Stream
we introducedasync_std::stream::from_iter
. To my knowledge all of this together has covered people's needs quite well.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.
I don't think the way currently used in
async-std
is the ideal way. That way means that all third-party crates that provide theFromIterator
implementation must be patched in order to fully support.As far as I know, this is why
futures
implementscollect
using existing traits (Default
,Extend
) rather than its own. The way used infutures
cannot currently optimize based on the size-hint of the stream, but this will be possible once rust-lang/rust#72631 stabilizes.If we want the collection to extend asynchronously, you can use
Sink
or use a loop.This seems copy of futures::stream::iter?
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.
I don't think that's a compelling outcome; this means that patterns from sync Rust wouldn't be transferable to async Rust. Where possible I think it's incredibly valuable to aim for parity between the two. Though this kind of gets into "design philosophy", and at least in the context of this PR we don't need to find consensus.
That's accurate, and I think that's the right outcome. In some cases
FromIterator
implementations perform blocking IO which I think is I think enough reason to not introduce a blanket conversion. For example if a function were to useimpl FromStream<Result<fs::Metadata>>
as an input, then both an iterator constructed fromstd::fs::ReadDir
and a stream constructed fromasync_std::fs::ReadDir
would be valid*. However the synchronous variant would likely interfere with the scheduler.*Admittedly it's a bit of a contrived example, but hopefully it still serves to illustrate the 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.
Agreed, it actually requires real async trait method to add this to std, and afaik all of the existing approaches have some problems.
Well, this problem also exists in
futures::stream::iter
/async_std::stream::from_iter
.Aside from it's preferable or not, even a crate that is obviously unrelated to async can't be used as a return type for
collect
without implementing it, which is necessary information when explaining/considering-adding it (I mainly think about collections).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.
One solution I was thinking about for this is, leave
futures
's currentcollect
method (ascollect2
or another name), even after theFromStream
basedcollect
is implemented.(This is not an ideal solution, but it means third-party crates can provide mitigation for this, even if
std
can't solve this problem.)