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
Consider adding Half support to the BinaryPrimitives class #38456
Comments
Tagging subscribers to this area: @tannergooding |
Saw that you also mentioned this in #38288. Definitely should do both at the same time. :) |
Can we add these to namespace System.IO
{
public class BinaryReader
{
// NEW virtual method on existing type
// We'll give it a viable default implementation, just like the existing virtual Read* methods
public virtual Half ReadHalf();
}
public class BinaryWriter
{
// NEW virtual method on existing type
// We'll give it a viable default implementation, just like the existing virtual Write overloads
public virtual void Write(Half value);
}
} |
namespace System.Buffers.Binary
{
public static class BinaryPrimitives
{
public static Half ReadHalfLittleEndian(ReadOnlySpan<byte> source);
public static Half ReadHalfBigEndian(ReadOnlySpan<byte> source);
public static bool TryReadHalfLittleEndian(ReadOnlySpan<byte> source, out Half);
public static bool TryReadHalfBigEndian(ReadOnlySpan<byte> source, out Half);
public static void WriteHalfLittleEndian(Span<byte> destination, Half value);
public static void WriteHalfBigEndian(Span<byte> destination, Half value);
public static bool TryWriteHalfLittleEndian(Span<byte> destination, Half value);
public static bool TryWriteHalfBigEndian(Span<byte> destination, Half value);
}
}
namespace System.IO
{
public partial class BinaryReader
{
public virtual Half ReadHalf();
}
public partial class BinaryWriter
{
public virtual Half Write(Half value);
}
} |
Curious why we need separate *LittleEndian and *BigEndian methods? Is it because I might want to read something in big-endian encoding even though I'm on a little-endian machine so using |
Right, many file formats (for example) explicitly call out themselves as being big or little endian. These methods allow you to do the right thing regardless of what machine you are on. |
Moved to 6.0.0 per discussion on #40882. Thanks, @huoyaoyuan. |
Would you reconsider putting this into 5.0 release? Putting this into 6.0 release makes this new type harder to use imo. I wanted to switch to using native half type, but the missing methods on BinaryReader make this a bit harder. |
.NET 5 API surface is done. We are not able to add this to 5.0 release. If you need the method on BinaryReader for your project, you can add it as internal extension method for now. It should be like ~20 lines. |
Background and Motivation
This came up while working to add
Half
support to the CBOR components. While not blocking to my work, these methods would be nice to have for the sake of completeness.Proposed API
The text was updated successfully, but these errors were encountered: