Repository navigation
Closed
Labels
api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Buffers
Description
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
Reacted by Kuinox
Metadata
Metadata
Assignees
Labels
api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Buffers
Activity
JeremyKuhne commentedon Aug 13, 2019
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.
ahsonkhan commentedon Aug 15, 2019
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
SequenceReadercontained a ulong, none of the existing overloads would work.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).
khellang commentedon Aug 15, 2019
Nothing that I couldn't work around, but as you already mentioned:
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
ulongoverlong.MaxValue; I don't really see how you'd do that using these methods (as theTryRead<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:
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.
JeremyKuhne commentedon Aug 15, 2019
I don't understand what you're trying to say. This is just byte blitting and hard casts. This isn't parsing.
khellang commentedon Aug 15, 2019
Are you saying you could read
ulong.MaxValueinto alongusingpublic static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out long value);and then cast it toulong? 🤔JeremyKuhne commentedon Aug 15, 2019
Yes, casts are unchecked by default at runtime. Both of these are
0xFFFFFFFF: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.
ahsonkhan commentedon Aug 16, 2019
That's a good point. This works as expected.
[-]Unsigned overloads for SequenceReader[/-][+]API Proposal: Unsigned overloads for SequenceReader[/+]MithrilMan commentedon Dec 22, 2019
@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 reverseto handle endianness)khellang commentedon Dec 22, 2019
I think that proposal is covered by https://github.com/dotnet/corefx/issues/40870? 🤔
dotnet-policy-service commentedon Jan 28, 2025
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.
dotnet-policy-service commentedon Feb 11, 2025
This issue will now be closed since it had been marked
no-recent-activitybut 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.