Skip to content

Commit e09c892

Browse files
committed
Use Unsafe.BitCastto cast Memory<T> and ReadOnlyMemory<T>
1 parent 511d266 commit e09c892

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

src/libraries/System.Private.CoreLib/src/System/Memory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace System
2121
public readonly struct Memory<T> : IEquatable<Memory<T>>
2222
{
2323
// NOTE: With the current implementation, Memory<T> and ReadOnlyMemory<T> must have the same layout,
24-
// as code uses Unsafe.As to cast between them.
24+
// as code uses Unsafe.BitCast to cast between them.
2525

2626
// The highest order bit of _index is used to discern whether _object is a pre-pinned array.
2727
// (_index < 0) => _object is a pre-pinned array, so Pin() will not allocate a new GCHandle
@@ -187,7 +187,7 @@ internal Memory(object? obj, int start, int length)
187187
/// Defines an implicit conversion of a <see cref="Memory{T}"/> to a <see cref="ReadOnlyMemory{T}"/>
188188
/// </summary>
189189
public static implicit operator ReadOnlyMemory<T>(Memory<T> memory) =>
190-
Unsafe.As<Memory<T>, ReadOnlyMemory<T>>(ref memory);
190+
Unsafe.BitCast<Memory<T>, ReadOnlyMemory<T>>(memory);
191191

192192
/// <summary>
193193
/// Returns an empty <see cref="Memory{T}"/>

src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace System
2121
public readonly struct ReadOnlyMemory<T> : IEquatable<ReadOnlyMemory<T>>
2222
{
2323
// NOTE: With the current implementation, Memory<T> and ReadOnlyMemory<T> must have the same layout,
24-
// as code uses Unsafe.As to cast between them.
24+
// as code uses Unsafe.BitCast to cast between them.
2525

2626
// The highest order bit of _index is used to discern whether _object is a pre-pinned array.
2727
// (_index < 0) => _object is a pre-pinned array, so Pin() will not allocate a new GCHandle

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ref Unsafe.As<T, byte>(ref GetReference(span)),
7171
/// as <see cref="Memory{T}"/> but only used for reading to store a <see cref="ReadOnlyMemory{T}"/>.
7272
/// </remarks>
7373
public static Memory<T> AsMemory<T>(ReadOnlyMemory<T> memory) =>
74-
Unsafe.As<ReadOnlyMemory<T>, Memory<T>>(ref memory);
74+
Unsafe.BitCast<ReadOnlyMemory<T>, Memory<T>>(memory);
7575

7676
/// <summary>
7777
/// Returns a reference to the 0th element of the Span. If the Span is empty, returns a reference to the location where the 0th element

0 commit comments

Comments
 (0)