From 29b55bd6d008a70488fc9efec3866f39a4116cd9 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 23 Oct 2024 02:25:04 +0100 Subject: [PATCH 1/2] Use `Unsafe.BitCast` for generic span casting --- .../src/System/Globalization/DateTimeFormat.cs | 2 +- .../src/System/Globalization/HebrewNumber.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs index a7ca7828e6e493..2b5a9c80a11104 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs @@ -754,7 +754,7 @@ internal static void AppendChar(ref ValueListBuilder result, char { Debug.Assert(typeof(TChar) == typeof(byte)); var r = new Rune(ch); - r.EncodeToUtf8(MemoryMarshal.AsBytes(result.AppendSpan(r.Utf8SequenceLength))); + r.EncodeToUtf8(Unsafe.BitCast, Span>(result.AppendSpan(r.Utf8SequenceLength))); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/HebrewNumber.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/HebrewNumber.cs index e4c511e5b7c5a6..ac51013deff5c4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/HebrewNumber.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/HebrewNumber.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -211,7 +212,7 @@ internal static void Append(ref ValueListBuilder outputBuffer, int else { Debug.Assert(typeof(TChar) == typeof(byte)); - Rune.DecodeLastFromUtf8(MemoryMarshal.AsBytes(outputBuffer.AsSpan()), out Rune value, out int bytesConsumed); + Rune.DecodeLastFromUtf8(Unsafe.BitCast, ReadOnlySpan>(outputBuffer.AsSpan()), out Rune value, out int bytesConsumed); outputBuffer.Length -= bytesConsumed; outputBuffer.Append(TChar.CastFrom('"')); DateTimeFormat.AppendChar(ref outputBuffer, (char)value.Value); From f488388d6cd03a77ba8cd53745d2e5b4680fc5da Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:44:31 +0100 Subject: [PATCH 2/2] Update `Enum.EnumInfo` --- .../System.Private.CoreLib/src/System/Enum.EnumInfo.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.EnumInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.EnumInfo.cs index 07a205ce3016bd..59c788f56e7162 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.EnumInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.EnumInfo.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System @@ -36,7 +37,7 @@ public EnumInfo(bool hasFlagsAttribute, TStorage[] values, string[] names) public unsafe TResult[] CloneValues() where TResult : struct { Debug.Assert(sizeof(TStorage) == sizeof(TResult)); - return MemoryMarshal.Cast(Values).ToArray(); + return Unsafe.BitCast, ReadOnlySpan>(Values).ToArray(); } } }