diff --git a/src/libraries/System.Private.CoreLib/src/System/Memory.cs b/src/libraries/System.Private.CoreLib/src/System/Memory.cs index 5ec18c4cb842ec..a0da08ba9a8038 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Memory.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Memory.cs @@ -20,9 +20,6 @@ namespace System [DebuggerDisplay("{ToString(),raw}")] public readonly struct Memory : IEquatable> { - // NOTE: With the current implementation, Memory and ReadOnlyMemory must have the same layout, - // as code uses Unsafe.As to cast between them. - // The highest order bit of _index is used to discern whether _object is a pre-pinned array. // (_index < 0) => _object is a pre-pinned array, so Pin() will not allocate a new GCHandle // (else) => Pin() needs to allocate a new GCHandle to pin the object. @@ -187,7 +184,7 @@ internal Memory(object? obj, int start, int length) /// Defines an implicit conversion of a to a /// public static implicit operator ReadOnlyMemory(Memory memory) => - Unsafe.As, ReadOnlyMemory>(ref memory); + new ReadOnlyMemory(memory._object, memory._index, memory._length); /// /// Returns an empty diff --git a/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs b/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs index ee2059ef0170aa..61bd9314bd26ca 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs @@ -20,15 +20,12 @@ namespace System [DebuggerDisplay("{ToString(),raw}")] public readonly struct ReadOnlyMemory : IEquatable> { - // NOTE: With the current implementation, Memory and ReadOnlyMemory must have the same layout, - // as code uses Unsafe.As to cast between them. - // The highest order bit of _index is used to discern whether _object is a pre-pinned array. // (_index < 0) => _object is a pre-pinned array, so Pin() will not allocate a new GCHandle // (else) => Pin() needs to allocate a new GCHandle to pin the object. - private readonly object? _object; - private readonly int _index; - private readonly int _length; + internal readonly object? _object; + internal readonly int _index; + internal readonly int _length; internal const int RemoveFlagsBitMask = 0x7FFFFFFF; diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index d849a0d58c75ed..0fc0642c52978b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -71,7 +71,7 @@ ref Unsafe.As(ref GetReference(span)), /// as but only used for reading to store a . /// public static Memory AsMemory(ReadOnlyMemory memory) => - Unsafe.As, Memory>(ref memory); + new Memory(memory._object, memory._index, memory._length); /// /// 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