-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
API proposal: Add File class method overloads for ReadOnlyMemory and ReadOnlySpan #35054
Comments
Tagging subscribers to this area: @carlossanlop, @jozkee |
Thanks for the contribution, @Tan90909090. I noticed that in the source code for |
@carlossanlop Should we mark this as ready for review? Seems like it's actionable by the API reviewers. |
Oh! I forget namespace System.IO
{
public static class File
{
+ public static ValueTask AppendAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, CancellationToken cancellationToken = default);
+ public static ValueTask AppendAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, Encoding encoding, CancellationToken cancellationToken = default);
+ public static ValueTask AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default);
+ public static ValueTask AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default);
+ public static ValueTask WriteAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, CancellationToken cancellationToken = default);
+ public static ValueTask WriteAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, Encoding encoding, CancellationToken cancellationToken = default);
+ public static ValueTask WriteAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default);
+ public static ValueTask WriteAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default);
}
} Question: Should I merge a description and change the issue title, or open another issue about APIs for |
Good catch, @Tan90909090. I updated your main issue description with all the APIs from your last comment. |
It seemed that a |
I also forgot about synchronous APIs... I updated this issue description for consistency. |
There is not much benefit in returning |
@carlossanlop Do you suppose any of our existing internal call sites would be able to use the new proposed overloads? I [very briefly] looked through source.dot.net and didn't immediately see any good candidates. |
namespace System.IO
{
public static class File
{
public static ValueTask AppendAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, CancellationToken cancellationToken = default);
public static ValueTask AppendAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, Encoding encoding, CancellationToken cancellationToken = default);
public static void AppendAllText(string path, ReadOnlySpan<char> contents);
public static void AppendAllText(string path, ReadOnlySpan<char> contents, Encoding encoding);
public static ValueTask AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default);
public static ValueTask AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default);
public static ValueTask WriteAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, CancellationToken cancellationToken = default);
public static ValueTask WriteAllLinesAsync(string path, IEnumerable<ReadOnlyMemory<char>> contents, Encoding encoding, CancellationToken cancellationToken = default);
public static void WriteAllText(string path, ReadOnlySpan<char> contents);
public static void WriteAllText(string path, ReadOnlySpan<char> contents, Encoding encoding);
public static ValueTask WriteAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default);
public static ValueTask WriteAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default);
public static void WriteAllBytes(string path, ReadOnlySpan<byte> bytes);
public static ValueTask WriteAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default);
}
} |
Related to #30305.
Motivation
Currently,
File
APIs for writing a file only acceptbyte[]
andstring
.For instance,
File.WriteAllBytesAsync
only acceptsbyte[]
. When I want to write aReadOnlyMemory<byte>
to file, I have two choises.ReadOnlyMemory<byte>.ToArray()
then callFile.WriteAllBytesAsync(string, byte[], CancellationToken)
. But this allocates extra memory.FileStream
instance with an appropriateFileOptions
and callFileStream.WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
. But this choise is verbose.It is convenient to be able to pass
ReadOnlyMemory<byte>
toFile.WriteAllBytesAsync()
directly.Proposal
The text was updated successfully, but these errors were encountered: