Skip to content

Commit

Permalink
add Stream DisposeAsync (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 14, 2024
1 parent e42ecf1 commit 8907b2a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 339**
**API count: 340**
1 change: 1 addition & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
#### Stream

* `Task CopyToAsync(Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copytoasync#system-io-stream-copytoasync(system-io-stream-system-threading-cancellationtoken))
* `ValueTask DisposeAsync()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.disposeasync)
* `ValueTask<Int32> ReadAsync(Memory<Byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync#system-io-stream-readasync(system-memory((system-byte))-system-threading-cancellationtoken))
* `ValueTask WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.writeasync#system-io-stream-writeasync(system-readonlymemory((system-byte))-system-threading-cancellationtoken))

Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
* `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0`


**API count: 339**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 340**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -737,6 +737,7 @@ The class `Polyfill` includes the following extension methods:
#### Stream

* `Task CopyToAsync(Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copytoasync#system-io-stream-copytoasync(system-io-stream-system-threading-cancellationtoken))
* `ValueTask DisposeAsync()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.disposeasync)
* `ValueTask<Int32> ReadAsync(Memory<Byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync#system-io-stream-readasync(system-memory((system-byte))-system-threading-cancellationtoken))
* `ValueTask WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.writeasync#system-io-stream-writeasync(system-readonlymemory((system-byte))-system-threading-cancellationtoken))

Expand Down
3 changes: 3 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ async Task Stream_Methods()
var read = await stream.ReadAsync(memory);
#endif
await stream.CopyToAsync(stream);
#if FeatureValueTask
await stream.DisposeAsync();
#endif
}

async Task StreamReader_Methods()
Expand Down
33 changes: 33 additions & 0 deletions src/Polyfill/Polyfill_Stream_DisposeAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <auto-generated />
#pragma warning disable

#if (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2X) && FeatureValueTask

namespace Polyfills;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Link = System.ComponentModel.DescriptionAttribute;

static partial class Polyfill
{
//https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L174
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.disposeasync")]
public static ValueTask DisposeAsync(this Stream target)
{
try
{
target.Dispose();
return default;
}
catch (Exception exception)
{
return new ValueTask(Task.FromException(exception));
}
}
}
#endif

0 comments on commit 8907b2a

Please sign in to comment.