-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add CancellationToken overloads for TextWriter.WriteAsync and WriteLineAsync #122127
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
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-io |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements the approved API proposal to add CancellationToken overloads to TextWriter for WriteAsync and WriteLineAsync methods. The changes provide a more convenient way to pass cancellation tokens when writing strings asynchronously, avoiding the need to call .AsMemory() on strings.
- Added three non-virtual methods to
TextWriter:WriteAsync(string?, CancellationToken),WriteLineAsync(CancellationToken), andWriteLineAsync(string?, CancellationToken) - Updated the public API surface in System.Runtime.cs
- Added comprehensive test coverage for all three new methods, including happy path, null handling, and cancellation scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs | Implements three new non-virtual methods that delegate to existing ReadOnlyMemory<char> overloads with proper cancellation token handling |
| src/libraries/System.Runtime/ref/System.Runtime.cs | Updates the public API surface to expose the three new method signatures |
| src/libraries/System.Runtime/tests/System.IO.Tests/TextWriter/TextWriterTests.cs | Adds comprehensive test coverage with 6 test methods covering normal operation, null handling, and cancellation for all three new methods |
|
@copilot, please address the feedback, specifically about adding XML comments to the new APIs. These will be used to seed the docs and should mirror the existing documentation (from learn.microsoft.com) for the existing overloads. |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Added XML documentation to all three new methods in commit 4ff346c. The documentation follows the pattern from the existing overloads and includes proper param descriptions, return value documentation, and uses <see langword> for null references. |
src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
| /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||
| /// <returns>A task that represents the asynchronous write operation.</returns> | ||
| public Task WriteAsync(string? value, CancellationToken cancellationToken) => | ||
| WriteAsync(value.AsMemory(), cancellationToken); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bartonjs, was there any discussion in API review about instead adding an implicit operator from string to ReadOnlyMemory<char> (just as it has one to span)? Two of the three overloads being added here would then seem unnecessary.
adamsitnik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you @stephentoub and @copilot ;)
Implementation Plan: TextWriter WriteAsync/WriteLineAsync CancellationToken Overloads
Task WriteAsync(string? value, CancellationToken token)Task WriteLineAsync(CancellationToken token)Task WriteLineAsync(string? value, CancellationToken token)Summary
Successfully implemented the approved API proposal for adding CancellationToken overloads to TextWriter. The implementation:
Original prompt
TextWriter.WriteLineAsyncwithCancellationTokenoverloads #106136💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.