-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
decimal forgotten by Span<T> APIs #28904
Comments
What APIs are you referring to? decimal has Parse/TryParse overloads that take |
For binary serialization. There are no methods in |
I see. @tannergooding, @pentp, what (if anything) would make sense to do here? |
I think providing: public readonly partial struct Decimal
{
public Decimal(ReadOnlySpan<int> bits);
public static bool TryGetBits(decimal d, Span<int> bits);
} Makes sense. |
I found this a couple of weeks ago as well. I think it wasn't implemented through lack of clear demand for it having discussed it on corefx gitter. There could be a |
Decimal has an internal method |
That is actually tracked by https://github.com/dotnet/corefx/issues/35791. We should consider including |
|
That looks like the right place! Should add decimal also to that issue and fix them together. |
So, is the plan to only provide new |
It does seem wrong that there is no My use case, for informational purposes, is to safely write message Decimal {
uint32 lo = 1;
uint32 mid = 2;
uint32 hi = 3;
bool isNegative = 4;
uint32 scale = 5;
} In code the generated type can be extended with implicit operators to convert to and from |
In the meantime, if you want something that works now: private readonly struct DecimalData
{
private const int SignMask = unchecked((int)0x80000000);
private const int ScaleMask = 0x00FF0000;
private const int ScaleShift = 16;
public readonly int Flags;
public readonly int Hi;
public readonly int Lo;
public readonly int Mid;
public DecimalData(int flags, int hi, int lo, int mid)
{
if (!IsValid(flags))
throw new InvalidDataException("Invalid decimal flag data.");
Flags = flags;
Hi = hi;
Lo = lo;
Mid = mid;
}
private static bool IsValid(int flags) => (flags & ~(SignMask | ScaleMask)) == 0 && ((uint)(flags & ScaleMask) <= (28 << ScaleShift));
}
public unsafe decimal ReadDecimal()
{
var data = new DecimalData(ReadInt32(), ReadInt32(), ReadInt32(), ReadInt32());
return *(decimal*)&data;
}
public unsafe void WriteDecimal(decimal value)
{
DecimalData data = *(DecimalData*)&value;
WriteInt32(data.Flags);
WriteInt32(data.Hi);
WriteInt32(data.Lo);
WriteInt32(data.Mid);
} Just change |
Is @tannergooding's API proposal at https://github.com/dotnet/corefx/issues/35877#issuecomment-471004904 the thing we want to bring through review? |
Is there anything more needed than the two methods @tannergooding proposed before this can be api reviewed? |
Sorry for the delay, I had missed the last reply to this thread. Ideally the OP (@gbieging) would update the top post with the API intended to be reviewed. The API review process is here: https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md and we generally prefer the proposal to be in a format similar to the good example that is given (with a rationale and proposed API section; if nothing else). It just helps speed along the review process and make everything clear. Otherwise, someone (generally the area owner) has to tell the meeting driver (generally @terrajobst) to jump down to this other comment that actually contains the proposed API, summarize the thinking behind it, and call out any notable "open questions" or other details called out by the overall thread. |
I updated the top comment since Tanner already did the heavy lifting of making the API proposal. ;) |
Is there a need for the public static bool TryGetBits(decimal d, Span<int> bits);
public static void GetBits(decimal d, Span<int> bits);
// Existing:
public static int[] GetBits(Decimal d); |
I think the idea is to provide some symmetry with the |
namespace System
{
public readonly partial struct Decimal
{
public Decimal(ReadOnlySpan<int> bits);
public static bool TryGetBits(decimal d, Span<int> destination, out int valuesWritten);
public static int GetBits(decimal d, Span<int> destination);
}
} |
@stephentoub how to use new Span APIs with Net Standard 2.1 library? will it work with only .net 5? |
They're new in .NET 5, and thus aren't available via .NET Standard 2.1. |
Edit by @GrabYourPitchforks on 29 Jan 20: API proposal is in a comment at https://github.com/dotnet/corefx/issues/35877#issuecomment-471004904
I was recently toying with making a serialization library and found out that there are no
Span<T>
APIs for serializing and deserializing decimals.I searched for the issues related to the
Span<T>
APIs and found #18969 and #18970 for decimal, but when they where closed for tracking in #19444 decimal was forgotten.I know 3.0 is near but it would be nice to have it in 2.x too.
The text was updated successfully, but these errors were encountered: