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

Add {RO}Span GetReference and ROMemory TryGetArray to MemoryMarshal#25789

Merged
ahsonkhan merged 1 commit into
dotnet:masterfrom
ahsonkhan:AddToMemoryMarshal
Dec 15, 2017
Merged

Add {RO}Span GetReference and ROMemory TryGetArray to MemoryMarshal#25789
ahsonkhan merged 1 commit into
dotnet:masterfrom
ahsonkhan:AddToMemoryMarshal

Conversation

@ahsonkhan
Copy link
Copy Markdown

@ahsonkhan
Copy link
Copy Markdown
Author

@dotnet-bot test this please

@ahsonkhan
Copy link
Copy Markdown
Author

Is this PR good to merge?

{
if (((OwnedMemory<T>)obj).TryGetArray(out var segment))
{
arraySegment = new ArraySegment<T>(segment.Array, segment.Offset + (index & ReadOnlyMemory<T>.RemoveOwnedFlagBitMask), length);
Copy link
Copy Markdown
Member

@KrzysztofCwalina KrzysztofCwalina Dec 14, 2017

Choose a reason for hiding this comment

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

Do we need to check that the length <= segment.Length - index?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If length is greater than segment.Count (if, let's say someone implements OwnedMemory.TryGetArray incorrectly and returns a subset of the backing memory), then the ArraySegment constructor will already throw ArgumentException.

System.ArgumentException : Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

Regarding index, that only changes on a Slice operation, and we decrease the length accordingly whenever that happens. Therefore, I don't think an explicit check is necessary here.

Copy link
Copy Markdown
Member

@KrzysztofCwalina KrzysztofCwalina left a comment

Choose a reason for hiding this comment

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

Just one question. Otherwise looks good.

@ahsonkhan ahsonkhan merged commit 5b07e3b into dotnet:master Dec 15, 2017
@ahsonkhan ahsonkhan deleted the AddToMemoryMarshal branch December 15, 2017 00:44
@karelz karelz added this to the 2.1.0 milestone Dec 28, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants