Add CancellationToken overloads for TextWriter.WriteAsync and WriteLineAsync#122127
Add CancellationToken overloads for TextWriter.WriteAsync and WriteLineAsync#122127stephentoub merged 4 commits intomainfrom
Conversation
Co-authored-by: stephentoub <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-system-io |
There was a problem hiding this comment.
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 <[email protected]>
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 <[email protected]>
| /// <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.
@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.
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.