Skip to content

Provide a way to get a PipeReader from a ReadOnlySequence<byte> #47990

Closed

Description

@Alxandr

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 contiguous Span<byte>), and you need to feed that into an API that expects a PipeReader. This is basically the same usecase as a MemoryStream over a byte[], except it's in the new APIs of Pipes and ReadOnlySequence<byte>s. The exact signature for this API isn't too terribly important (wether it's an extension method on ReadOnlySequence<byte>, an overload of PipeReader.Create or new MemoryPipeReader(sequence)), since this template requires a proposed API, I will be writing the static Create, as that is closest to what exist for Stream.

Proposed API

namespace System.IO.Pipelines
{
    public abstract class PipeReader
    {
+       public static PipeReader Create(ReadOnlySequence<byte> sequence);

Usage Examples

var data = GenerateDataAsSequence();
var obj = await DeserializeAsync(PipeReader.Create(data));

Alternative Designs

Using the package Nerdbank.Streams, it's possible to achieve a similar result going through a Stream;

var data = GenerateDataAsSequence();
var obj = await DeserializeAsync(PipeReader.Create(data.AsStream()));

Alternatively, the fake "generate" method here needs to be rewritten to take a writer:

var pipe = new Pipe();
var task = GenerateDataIntoPipe(pipe.Writer);
var obj = await DeserializeAsync(pipe.Reader);
await task;

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.

Activity

  1. added
    api-suggestion
    on Feb 8, 2021
  2. ghost added
    untriaged
    on Feb 8, 2021
  3. added this to the Future milestone on Feb 8, 2021
  4. removed
    untriaged
    on Feb 8, 2021
  5. BrennanConroy commented on Feb 8, 2021

    @BrennanConroy
    Member

    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.

  6. Alxandr commented on Feb 9, 2021

    @Alxandr
    ContributorAuthor

    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 PipeReaders to ensure they are agnostic with regards to where any buffer boundaries ends up being, so I run tests with a few different strategies ranging from "give all data on the first read" to "give a single byte more for every call to read". That's very useful for testing, this however I imagine would be useful whenever you have a data in memory already (for instance if you've downloaded a pipe already, and want to reuse the data somewhere that expects a pipe).

  7. davidfowl commented on Feb 11, 2021

    @davidfowl
    Member

    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.

  8. modified the milestones: Future, 6.0.0 on Feb 11, 2021
  9. added
    api-ready-for-review
    and removed
    api-suggestion
    on Feb 11, 2021
  10. davidfowl commented on Feb 11, 2021

    @davidfowl
    Member

    @Alxandr once the API is approved would you be OK doing the work?

  11. Alxandr commented on Feb 11, 2021

    @Alxandr
    ContributorAuthor

    I could probably do that, sure :).

  12. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 16, 2021
  13. terrajobst commented on Feb 16, 2021

    @terrajobst
    Contributor

    Video

    • We could also overloads to create a reader straight from a byte[] or ReadOnlyMemory<byte> but those can be constructed by creating a sequence, so we can add them later if we think that's common enough.
    • We don't believe this method needs any options
    namespace System.IO.Pipelines
    {
        public abstract class PipeReader
        {
            public static PipeReader Create(ReadOnlySequence<byte> sequence);
        }
    }
  14. davidfowl commented on Feb 16, 2021

    @davidfowl
    Member

    @Alxandr the API's been approved! Have at it. I'll gladly review the implementation.

  15. added a commit that references this issue on Feb 16, 2021
    927e2d0
  16. ghost added
    in-pr
    on Feb 16, 2021
  17. ghost removed
    in-pr
    on Mar 18, 2021
  18. self-assigned this
    on Mar 18, 2021
  19. ghost locked as resolved and limited conversation to collaborators on Apr 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.IO.Pipelines

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions