Skip to content

Commit

Permalink
add AsValueTask extension method to cast from ValueTask<T> to ValueTask
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Arvidsson committed Jan 21, 2022
1 parent 8697fef commit 10df6ef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Kafka.TestFramework/NetworkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public override void Write(byte[] buffer, int offset, int count)
throw new System.NotImplementedException();
}

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = new CancellationToken())
{
return new ValueTask(_networkClient.SendAsync(buffer, cancellationToken).AsTask());
}
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = new CancellationToken()) =>
_networkClient.SendAsync(buffer, cancellationToken).AsValueTask();

public override bool CanRead => false;
public override bool CanSeek => false;
Expand Down
3 changes: 3 additions & 0 deletions src/Kafka.TestFramework/ValueTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ internal static Task WhenAllAsync(
valueTask
=> !valueTask.IsCompletedSuccessfully)
.Select(valueTask => valueTask.AsTask()));

internal static ValueTask AsValueTask<T>(this ValueTask<T> valueTask) =>
valueTask.IsCompletedSuccessfully ? default : new ValueTask(valueTask.AsTask());
}
}

0 comments on commit 10df6ef

Please sign in to comment.