Skip to content

Add BinaryReader/BinaryWriter span polyfills (NetCore21) - #131

Merged
Tyrrrz merged 3 commits into
primefrom
copilot/add-polyfills-for-apis
Jul 11, 2026
Merged

Add BinaryReader/BinaryWriter span polyfills (NetCore21)#131
Tyrrrz merged 3 commits into
primefrom
copilot/add-polyfills-for-apis

Conversation

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Adds polyfills for the four span-based BinaryReader/BinaryWriter APIs introduced in .NET Core 2.1, enabling their use on netstandard2.0, netframework, and older netcoreapp targets.

New polyfills

  • BinaryReader.Read(Span<byte>) — delegates to Read(byte[], int, int), copies only bytes actually read
  • BinaryReader.Read(Span<char>) — delegates to Read(char[], int, int), copies only chars actually read
  • BinaryWriter.Write(ReadOnlySpan<byte>) — delegates to Write(byte[], int, int)
  • BinaryWriter.Write(ReadOnlySpan<char>) — delegates to Write(char[], int, int)
using var stream = new MemoryStream([1, 2, 3, 4, 5]);
using var reader = new BinaryReader(stream);
Span<byte> buffer = stackalloc byte[5];
var bytesRead = reader.Read(buffer); // polyfilled on pre-2.1 targets

using var output = new MemoryStream();
using var writer = new BinaryWriter(output);
writer.Write((ReadOnlySpan<byte>)buffer); // polyfilled on pre-2.1 targets

@Tyrrrz Tyrrrz added the enhancement New feature or request label Jul 11, 2026
@Tyrrrz
Tyrrrz marked this pull request as ready for review July 11, 2026 19:19
Copilot AI review requested due to automatic review settings July 11, 2026 19:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds .NET Core 2.1 span-based BinaryReader/BinaryWriter API polyfills so older targets (e.g., netstandard2.0, netframework, pre-2.1 netcoreapp) can use the newer span overloads while delegating to the existing array-based methods.

Changes:

  • Add BinaryReader.Read(Span<byte>) and BinaryReader.Read(Span<char>) polyfills (delegate to array overloads and copy back only what was read).
  • Add BinaryWriter.Write(ReadOnlySpan<byte>) and BinaryWriter.Write(ReadOnlySpan<char>) polyfills (delegate to array overloads).
  • Add unit tests for the new polyfills and update Signatures.md to list the new APIs.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
Signatures.md Adds the BinaryReader/BinaryWriter span-based members to the signature index and updates totals.
PolyShim/NetCore21/BinaryWriter.cs Introduces BinaryWriter.Write(ReadOnlySpan<byte/char>) member polyfills for pre-.NET Core 2.1 targets.
PolyShim/NetCore21/BinaryReader.cs Introduces BinaryReader.Read(Span<byte/char>) member polyfills for pre-.NET Core 2.1 targets.
PolyShim.Tests/NetCore21/BinaryWriterTests.cs Adds tests for the new BinaryWriter span overload polyfills.
PolyShim.Tests/NetCore21/BinaryReaderTests.cs Adds tests for the new BinaryReader span overload polyfills.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Signatures.md
Comment thread PolyShim.Tests/NetCore21/BinaryReaderTests.cs
Comment thread PolyShim.Tests/NetCore21/BinaryReaderTests.cs
Comment thread PolyShim.Tests/NetCore21/BinaryWriterTests.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.16%. Comparing base (1f7fe37) to head (8d1ada6).

Additional details and impacted files
@@            Coverage Diff             @@
##            prime     #131      +/-   ##
==========================================
+ Coverage   99.15%   99.16%   +0.01%     
==========================================
  Files         145      147       +2     
  Lines        3798     3846      +48     
  Branches       87       87              
==========================================
+ Hits         3766     3814      +48     
  Misses         13       13              
  Partials       19       19              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants