Skip to content

Commit

Permalink
Use Unsafe.BitCast for generic span casting
Browse files Browse the repository at this point in the history
  • Loading branch information
xtqqczze committed Oct 23, 2024
1 parent 24dd2bc commit 29b55bd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ internal static void AppendChar<TChar>(ref ValueListBuilder<TChar> 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<TChar>, Span<byte>>(result.AppendSpan(r.Utf8SequenceLength)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -211,7 +212,7 @@ internal static void Append<TChar>(ref ValueListBuilder<TChar> 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<TChar>, ReadOnlySpan<byte>>(outputBuffer.AsSpan()), out Rune value, out int bytesConsumed);
outputBuffer.Length -= bytesConsumed;
outputBuffer.Append(TChar.CastFrom('"'));
DateTimeFormat.AppendChar(ref outputBuffer, (char)value.Value);
Expand Down

1 comment on commit 29b55bd

@xtqqczze
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.