Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Add TryGetOwnedMemory (dotnet/coreclr#16455)
Browse files Browse the repository at this point in the history
* Add TryGetOwnedMemory

* Feedback

* spelling

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
benaadams authored and jkotas committed Feb 21, 2018
1 parent ca0fefa commit 94b4f26
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ public static bool TryGetArray<T>(ReadOnlyMemory<T> readOnlyMemory, out ArraySeg
return false;
}

/// <summary>
/// Get a <see cref="OwnedMemory{T}"/> from the underlying memory.
/// If unable to get the <typeparamref name="TOwner"/>, return false with a default <typeparamref name="TOwner"/>.
/// </summary>
public static bool TryGetOwnedMemory<T, TOwner>(ReadOnlyMemory<T> readOnlyMemory, out TOwner ownedMemory)
where TOwner : OwnedMemory<T>
{
TOwner owner; // Use register for null comparison rather than byref
ownedMemory = owner = readOnlyMemory.GetObjectStartLength(out int index, out int length) as TOwner;
return !ReferenceEquals(owner, null);
}

/// <summary>
/// Get a <see cref="OwnedMemory{T}"/> and <param name="index">, <param name="length"> on the <see cref="OwnedMemory{T}"/> from the underlying memory.
/// If unable to get the <typeparamref name="TOwner"/>, return false with a default <typeparamref name="TOwner"/>.
/// </summary>
public static bool TryGetOwnedMemory<T, TOwner>(ReadOnlyMemory<T> readOnlyMemory, out TOwner ownedMemory, out int index, out int length)
where TOwner : OwnedMemory<T>
{
TOwner owner; // Use register for null comparison rather than byref
ownedMemory = owner = readOnlyMemory.GetObjectStartLength(out index, out length) as TOwner;
return !ReferenceEquals(owner, null);
}

/// <summary>
/// Creates an <see cref="IEnumerable{T}"/> view of the given <paramref name="memory" /> to allow
/// the <paramref name="memory" /> to be used in existing APIs that take an <see cref="IEnumerable{T}"/>.
Expand Down

0 comments on commit 94b4f26

Please sign in to comment.