Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Fix truncated ArraySegment<byte> if elementSize != 1 #6462

Merged
merged 3 commits into from
Feb 23, 2021

Conversation

bjornharrtell
Copy link
Collaborator

@bjornharrtell bjornharrtell commented Feb 14, 2021

Intends to fix #6463.

@github-actions github-actions bot added the c# label Feb 14, 2021
@bjornharrtell bjornharrtell changed the title WIP: Fix returned truncated ArraySegment<byte> if elementSize is not … WIP: Fix returned truncated ArraySegment<byte> if elementSize != 1 Feb 14, 2021
@bjornharrtell bjornharrtell changed the title WIP: Fix returned truncated ArraySegment<byte> if elementSize != 1 [C#] Fix returned truncated ArraySegment<byte> if elementSize != 1 Feb 14, 2021
@github-actions github-actions bot added c++ codegen Involving generating code from schema labels Feb 14, 2021
@bjornharrtell bjornharrtell changed the title [C#] Fix returned truncated ArraySegment<byte> if elementSize != 1 [C#] Fix truncated ArraySegment<byte> if elementSize != 1 Feb 14, 2021
@bjornharrtell bjornharrtell marked this pull request as ready for review February 14, 2021 11:21
@bjornharrtell
Copy link
Collaborator Author

ping @dbaileychess

@@ -112,7 +112,7 @@ public int __vector(int offset)
// Get the data of a vector whoses offset is stored at "offset" in this object as an
// ArraySegment&lt;byte&gt;. If the vector is not present in the ByteBuffer,
// then a null value will be returned.
public ArraySegment<byte>? __vector_as_arraysegment(int offset)
public ArraySegment<byte>? __vector_as_arraysegment(int offset, int elementSize)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we're returning ArraySegment<byte> instead of ArraySegment<T> because of endianness? Though possibly this API would be more useful if we returned ArraySegment<T> and asserted we're on little endian? Or is it not possible in C# to make a ArraySegment<T> out of bytes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Possibly by some unsafe code tricks but normally you are not allowed to cast no.

The relatively new MemoryMarshal class allows to cast Span but curiously not Memory, as of yet I fail to understand why. I also have not fully understood if use of MemoryMarshal is as unsafe as unsafe code but dotnet/runtime#41418 indicates it is equivalent to unsafe in many cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, then this can be merged if @dbaileychess doesn't have any comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c# c++ codegen Involving generating code from schema
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C#] Generated code returns truncated ArraySegment<byte> if elementSize is not byte
3 participants