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

Implement BinaryReader.ReadExactly #106238

Merged
merged 5 commits into from
Aug 20, 2024
Merged

Implement BinaryReader.ReadExactly #106238

merged 5 commits into from
Aug 20, 2024

Conversation

Log234
Copy link
Contributor

@Log234 Log234 commented Aug 11, 2024

This PR implements the BinaryReader.ReadExactly(Span<byte> buffer) method, as proposed in #101614, as well as some new tests to cover the new method.

The BinaryReader source files appear almost not have any tripple slash comments documenting the methods. I would of course be happy to add documentation, but I am unsure if the documentation of these methods is supposed to go somewhere else or if I am supposed to break with the existing "convention" of not documenting these methods.

Fixes #101614

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 11, 2024
@Log234
Copy link
Contributor Author

Log234 commented Aug 11, 2024

@dotnet-policy-bot agree

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

The PR is almost ready, PTAL at my comments.

@Log234 thank you for your contribution!

{
using (var stream = CreateStream())
{
var source = new byte[sourceSize].AsSpan();
Copy link
Member

Choose a reason for hiding this comment

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

nit: AsSpan is needed only for test projects that target older monikers like Full .NET Framework (because Span<T> is not part of mscorlib.dll in Full Framework, so we could not have added an implicit cast operator from array to span for Full Framework).

Suggested change
var source = new byte[sourceSize].AsSpan();
byte[] source = new byte[sourceSize];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, thanks for the explanation. I have updated the tests to remove the .AsSpan() calls.

@@ -453,6 +453,12 @@ public virtual byte[] ReadBytes(int count)
return result;
}

public virtual void ReadExactly(Span<byte> buffer)
Copy link
Member

Choose a reason for hiding this comment

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

The BinaryReader source files appear almost not have any tripple slash comments documenting the methods. I would of course be happy to add documentation, but I am unsure if the documentation of these methods is supposed to go somewhere else or if I am supposed to break with the existing "convention" of not documenting these methods.

Please add the tripple slash comments here. We have an internal porting process where we sporadically run a command line app that feeds the new comments to api docs. Sample outcome: dotnet/dotnet-api-docs#10210

You can use the wording from Stream.ReadExactly as an inspiration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification, I have updated the method with a triple slash comment, based on the Stream.ReadExactly method as you suggested.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @Log234 !

BTW dotnet/runtime is currently in a temporary mode where we merge to main only PRs that are a must have for the .NET 9 release. As soon as our main branch becomes .NET 10, I am going to merge your PR and it will be released in .NET 10 Preview 1.

@skyoxZ
Copy link
Contributor

skyoxZ commented Aug 14, 2024

It seems that there's a similar method:

private ReadOnlySpan<byte> InternalRead(Span<byte> buffer)
{
Debug.Assert(buffer.Length != 1, "length of 1 should use ReadByte.");
if (_isMemoryStream)
{
// read directly from MemoryStream buffer
Debug.Assert(_stream is MemoryStream);
return Unsafe.As<MemoryStream>(_stream).InternalReadSpan(buffer.Length);
}
else
{
ThrowIfDisposed();
_stream.ReadExactly(buffer);
return buffer;
}
}

@adamsitnik adamsitnik merged commit 9876ed1 into dotnet:main Aug 20, 2024
145 of 147 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: BinaryReader.ReadExactly
4 participants