-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Provide a way to get a PipeReader from a ReadOnlySequence<byte>
#47990
Comments
Triage: This would definitely make some of the tests in AspNetCore nicer. You should be able to write an extension method for this and party on that. |
I have written one of these for our tests actually, however it contains some extra features for testing purposes, like being able to simulate "network delays" (not actual delays, but how the data is split up for the reads). I use this to test APIs that consume |
I think the API as proposed is good, I don't think it should be an extension method as the pattern we have so far introduced is a factory method. |
@Alxandr once the API is approved would you be OK doing the work? |
I could probably do that, sure :). |
namespace System.IO.Pipelines
{
public abstract class PipeReader
{
public static PipeReader Create(ReadOnlySequence<byte> sequence);
}
} |
@Alxandr the API's been approved! Have at it. I'll gladly review the implementation. |
Add new factory method to PipeReader to create a PipeReader from a ReadOnlySequence<byte>. Fix dotnet#47990
Background and Motivation
Sometimes you have data available in memory (either already downloaded or read from file or generated by your process), represented as a
ReadOnlySequence<byte>
(which may or may not be a single contiguousSpan<byte>
), and you need to feed that into an API that expects aPipeReader
. This is basically the same usecase as aMemoryStream
over abyte[]
, except it's in the new APIs ofPipe
s andReadOnlySequence<byte>
s. The exact signature for this API isn't too terribly important (wether it's an extension method onReadOnlySequence<byte>
, an overload ofPipeReader.Create
ornew MemoryPipeReader(sequence)
), since this template requires a proposed API, I will be writing the staticCreate
, as that is closest to what exist forStream
.Proposed API
namespace System.IO.Pipelines { public abstract class PipeReader { + public static PipeReader Create(ReadOnlySequence<byte> sequence);
Usage Examples
Alternative Designs
Using the package Nerdbank.Streams, it's possible to achieve a similar result going through a Stream;
Alternatively, the fake "generate" method here needs to be rewritten to take a writer:
While this is probably the better solution in many cases, you don't always have access to rewrite the APIs that generate the data.
Risks
The only risk I can see is an expanded API surface area to teach/maintain.
The text was updated successfully, but these errors were encountered: