Skip to content

API Proposal: Unsigned overloads for SequenceReader #30580

Closed

Description

@khellang

What's the reason unsigned types were left out of SequenceReader<T>? CLS compliance? I couldn't find any details on it.

It would be really nice if it mirrored the API surface of BinaryPrimitives (or Utf8Parser for that matter).

I propose the following:

namespace System.Buffers
{
    public static partial class SequenceReaderExtensions
    {
        public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out short value);
        public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out short value);

        public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out int value);
        public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out int value);

        public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out long value);
        public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out long value);

+       [CLSCompliant(false)]
+       public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out ushort value);
+       [CLSCompliant(false)]
+       public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out ushort value);
+
+       [CLSCompliant(false)]
+       public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out uint value);
+       [CLSCompliant(false)]
+       public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out uint value);
+
+       [CLSCompliant(false)]
+       public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out ulong value);
+       [CLSCompliant(false)]
+       public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out ulong value);
    }
}

It would also be nice if it had non-Try* alternatives, but that could be left for a separate proposal.

// @JeremyKuhne

Activity

  1. JeremyKuhne commented on Aug 13, 2019

    @JeremyKuhne
    Member

    I didn't add them to reduce bloat. I figured you can just cast as there is no validation here other than you have the right amount of bytes to fill the type.

  2. ahsonkhan commented on Aug 15, 2019

    @ahsonkhan
    Contributor

    I didn't add them to reduce bloat. I figured you can just cast as there is no validation here other than you have the right amount of bytes to fill the type.

    Except trying to read a uint wouldn't fit into an int, so the caller would have to up-cast to long. Similarly, if the SequenceReader contained a ulong, none of the existing overloads would work.

    It would be really nice if it mirrored the API surface of BinaryPrimitives (or Utf8Parser for that matter).

    I think its a good idea to add them, but just for context, @khellang do you have a scenario or real code snippet where you ran into this limitation being a concern that you had to workaround? That would help motivate adding the APIs (more than just the fact that we don't have parity).

  3. khellang commented on Aug 15, 2019

    @khellang
    MemberAuthor

    do you have a scenario or real code snippet where you ran into this limitation being a concern that you had to workaround?

    Nothing that I couldn't work around, but as you already mentioned:

    trying to read a uint wouldn't fit into an int, so the caller would have to up-cast to long

    The Try* pattern also adds to the clunkyness, where you have to declare (and name) a variable of the "wrong" type first, only to cast it later. You can't just cast "on the way out", like you could've done with a non-Try* API.

    It's even more troublesome if you want to read a ulong over long.MaxValue; I don't really see how you'd do that using these methods (as the TryRead<T> method isn't exposed)?

    When using these APIs, you typically code against some specification of data structures. It would be nice if the code would reflect the specification as close as possible, without having to do so much casting back and forth:

    var reader = new SequenceReader<byte>(buffer);
    
    var structure = new MyStructure
    {
        Data1 = reader.ReadUInt32(),
        Data2 = reader.ReadUInt16(),
        Data3 = reader.ReadUInt16(),
        Data4 = reader.ReadUInt64()
    }

    I can, of course, add all of these as extension methods myself, but I think the framework could benefit from having them around in-box.

  4. JeremyKuhne commented on Aug 15, 2019

    @JeremyKuhne
    Member

    Except trying to read a uint wouldn't fit into an int

    I don't understand what you're trying to say. This is just byte blitting and hard casts. This isn't parsing.

  5. khellang commented on Aug 15, 2019

    @khellang
    MemberAuthor

    Are you saying you could read ulong.MaxValue into a long using public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out long value); and then cast it to ulong? 🤔

  6. JeremyKuhne commented on Aug 15, 2019

    @JeremyKuhne
    Member

    Are you saying you could read ulong.MaxValue into a long using public static bool TryReadLittleEndian(ref this SequenceReader reader, out long value); and then cast it to ulong? 🤔

    Yes, casts are unchecked by default at runtime. Both of these are 0xFFFFFFFF:

                int integer = -1;
                uint unsignedInteger = (uint)integer;

    I'm not saying we can't add new overloads, I just want to make sure we're clear on justifications before we consider it. Data loss shouldn't be a problem here.

  7. ahsonkhan commented on Aug 16, 2019

    @ahsonkhan
    Contributor

    Yes, casts are unchecked by default at runtime. Both of these are 0xFFFFFFFF:

    Data loss shouldn't be a problem here.

    That's a good point. This works as expected.

    [Fact]
    public void ReadUInt64OverInt64()
    {
        ulong value = (ulong)long.MaxValue + long.MaxValue;
        byte[] input = BitConverter.GetBytes(value);
    
        ReadOnlySequence<byte> bytes = new ReadOnlySequence<byte>(input);
    
        var reader = new SequenceReader<byte>(bytes);
    
        Assert.True(reader.TryReadLittleEndian(out long actual));
        Assert.Equal(value, (ulong)actual); // 18446744073709551614
    }
  8. changed the title [-]Unsigned overloads for SequenceReader[/-] [+]API Proposal: Unsigned overloads for SequenceReader[/+] on Sep 11, 2019
  9. MithrilMan commented on Dec 22, 2019

    @MithrilMan

    @JeremyKuhne
    Let me chime in just to say that for clarity unsigned overloads would be very welcome; maybe they would bloat a bit the library code but will help having lot less of that bloating in our codebase with unsolecited casts.
    (probably at networking level would have had more sense to have just unsigned if a decision had to be made to what trim out
    Last consideration: as you said, most of the time int/long etc... are just a way to speicfy how many bytes to fetch in a single call; giving the fact that SequenceReader are generic (even if for network purpose most of the time will be byte), why then not having a way to specify how many instances of T get in a single call, and return them in an array T[]?

    So basically what about having a SequenceReader<T>.TryRead(int count, out T[] values) ?
    maybe with an argument like bool reverse to handle endianness)

  10. khellang commented on Dec 22, 2019

    @khellang
    MemberAuthor
  11. transferred this issue fromdotnet/corefxon Feb 1, 2020
  12. added this to the 5.0 milestone on Feb 1, 2020
  13. dotnet-policy-service commented on Jan 28, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  14. added
    backlog-cleanup-candidate
    on Jan 28, 2025
  15. dotnet-policy-service commented on Feb 11, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  16. removed this from the Future milestone on Feb 11, 2025
  17. locked and limited conversation to collaborators on Mar 13, 2025
  18. removed
    backlog-cleanup-candidate
    on Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Buffers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions