diff --git a/src/Common/src/CoreLib/System/ReadOnlyMemory.cs b/src/Common/src/CoreLib/System/ReadOnlyMemory.cs index d68f622cce7a..bdf92ff65dd1 100644 --- a/src/Common/src/CoreLib/System/ReadOnlyMemory.cs +++ b/src/Common/src/CoreLib/System/ReadOnlyMemory.cs @@ -29,7 +29,7 @@ public readonly struct ReadOnlyMemory private readonly int _index; private readonly int _length; - private const int RemoveOwnedFlagBitMask = 0x7FFFFFFF; + internal const int RemoveOwnedFlagBitMask = 0x7FFFFFFF; /// /// Creates a new memory over the entirety of the target array. diff --git a/src/Common/src/CoreLib/System/ReadOnlySpan.cs b/src/Common/src/CoreLib/System/ReadOnlySpan.cs index 7a8243d669a0..a7f6f6e64eb1 100644 --- a/src/Common/src/CoreLib/System/ReadOnlySpan.cs +++ b/src/Common/src/CoreLib/System/ReadOnlySpan.cs @@ -21,7 +21,7 @@ namespace System public readonly ref struct ReadOnlySpan { /// A byref or a native ptr. - private readonly ByReference _pointer; + internal readonly ByReference _pointer; /// The number of elements this ReadOnlySpan contains. #if PROJECTN [Bound] diff --git a/src/Common/src/CoreLib/System/Runtime/InteropServices/MemoryMarshal.cs b/src/Common/src/CoreLib/System/Runtime/InteropServices/MemoryMarshal.cs index 5301f8771384..5ee643f2d2e3 100644 --- a/src/Common/src/CoreLib/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/Common/src/CoreLib/System/Runtime/InteropServices/MemoryMarshal.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Buffers; using System.Runtime.CompilerServices; namespace System.Runtime.InteropServices @@ -23,5 +24,30 @@ public static class MemoryMarshal /// public static Memory AsMemory(ReadOnlyMemory readOnlyMemory) => Unsafe.As, Memory>(ref readOnlyMemory); + + public static ref T GetReference(Span span) => ref span._pointer.Value; + + public static ref readonly T GetReference(ReadOnlySpan span) => ref span._pointer.Value; + + public static bool TryGetArray(ReadOnlyMemory readOnlyMemory, out ArraySegment arraySegment) + { + object obj = readOnlyMemory.GetObjectStartLength(out int index, out int length); + if (index < 0) + { + if (((OwnedMemory)obj).TryGetArray(out var segment)) + { + arraySegment = new ArraySegment(segment.Array, segment.Offset + (index & ReadOnlyMemory.RemoveOwnedFlagBitMask), length); + return true; + } + } + else if (obj is T[] arr) + { + arraySegment = new ArraySegment(arr, index, length); + return true; + } + + arraySegment = default; + return false; + } } } diff --git a/src/Common/src/CoreLib/System/Span.cs b/src/Common/src/CoreLib/System/Span.cs index ca146aef6597..45d630d18595 100644 --- a/src/Common/src/CoreLib/System/Span.cs +++ b/src/Common/src/CoreLib/System/Span.cs @@ -27,7 +27,7 @@ namespace System public readonly ref struct Span { /// A byref or a native ptr. - private readonly ByReference _pointer; + internal readonly ByReference _pointer; /// The number of elements this Span contains. #if PROJECTN [Bound]