private readonly int _index;
private readonly int _length;
- private const int RemoveOwnedFlagBitMask = 0x7FFFFFFF;
+ internal const int RemoveOwnedFlagBitMask = 0x7FFFFFFF;
/// <summary>
/// Creates a new memory over the entirety of the target array.
public readonly ref struct ReadOnlySpan<T>
{
/// <summary>A byref or a native ptr.</summary>
- private readonly ByReference<T> _pointer;
+ internal readonly ByReference<T> _pointer;
/// <summary>The number of elements this ReadOnlySpan contains.</summary>
#if PROJECTN
[Bound]
// 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
/// </remarks>
public static Memory<T> AsMemory<T>(ReadOnlyMemory<T> readOnlyMemory) =>
Unsafe.As<ReadOnlyMemory<T>, Memory<T>>(ref readOnlyMemory);
+
+ public static ref T GetReference<T>(Span<T> span) => ref span._pointer.Value;
+
+ public static ref readonly T GetReference<T>(ReadOnlySpan<T> span) => ref span._pointer.Value;
+
+ public static bool TryGetArray<T>(ReadOnlyMemory<T> readOnlyMemory, out ArraySegment<T> arraySegment)
+ {
+ object obj = readOnlyMemory.GetObjectStartLength(out int index, out int length);
+ if (index < 0)
+ {
+ if (((OwnedMemory<T>)obj).TryGetArray(out var segment))
+ {
+ arraySegment = new ArraySegment<T>(segment.Array, segment.Offset + (index & ReadOnlyMemory<T>.RemoveOwnedFlagBitMask), length);
+ return true;
+ }
+ }
+ else if (obj is T[] arr)
+ {
+ arraySegment = new ArraySegment<T>(arr, index, length);
+ return true;
+ }
+
+ arraySegment = default;
+ return false;
+ }
}
}
public readonly ref struct Span<T>
{
/// <summary>A byref or a native ptr.</summary>
- private readonly ByReference<T> _pointer;
+ internal readonly ByReference<T> _pointer;
/// <summary>The number of elements this Span contains.</summary>
#if PROJECTN
[Bound]