This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
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.
Added AsStream to PipeReader and PipeWriter #35399
Added AsStream to PipeReader and PipeWriter #35399
Changes from 1 commit
93eb298
d3f1b23
d27a7c2
ad20d31
c05f3ec
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.
It is C#8 already 😎
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 is relying on Memory's argument validation. That will end up allowing a null buffer if offset and count are 0. Might want to add a null / throw check here, but up to 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 think it's fine relying on the memory ctor.
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.
Why cast to int? Just keep it as a long as you compare to buffer length afterwards.
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 return value is an int so it has to be an int eventually.
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 trying to understand the purpose of the try/finally. I'm not sure what here could cause an exception, but if something did and we haven't yet gotten to
consumed = slice.End
, we'll end up advancing past everything that was read, but if such an exception happened afterconsumed = slice.End
, then we'll end up advancing only past the length up toactual
, and in any exceptional case, the consumer didn't actually read anything so it seems we shouldn't advance at all?I guess I'm wondering why this try/finally is needed at all, and why instead there isn't just a
_pipeReader.AdvanceTo(slice.End)
call just beforereturn actual;
? Does it have to do with the PipeReader contract around pairings of ReadAsync and AdvanceTo?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.
Right it’s about not leaving the reader in that “I’m still reading” state. Some of that has since been relaxed where AdvanceTo can be skipped if the next call is Complete.
But it doesn’t look like anything should really throw here. Even if it did, it’s possible you don’t want to make it consume everything unless it returned from the branch where data is returned.
Tl;DR this code is old and was tweaked a bunch of times and maybe there’s some paranoia in here that may no longer be strictly required