Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add GetReference and TryGetArray to MemoryMarshal #15417

Merged
merged 4 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mscorlib/shared/System/ReadOnlyMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public readonly struct ReadOnlyMemory<T>
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.
Expand Down
2 changes: 1 addition & 1 deletion src/mscorlib/shared/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace System
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,5 +24,30 @@ public static class MemoryMarshal
/// </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))
Copy link
Member

Choose a reason for hiding this comment

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

Can this be just return (((OwnedMemory<T>)obj).TryGetArray(out var arraySegment) ?

Copy link
Member

Choose a reason for hiding this comment

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

I see - you need to add the local index.

{
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;
}
}
}
2 changes: 1 addition & 1 deletion src/mscorlib/shared/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace System
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]
Expand Down