Skip to content

Commit 3d46957

Browse files
committed
Added conversion to Task<T>
1 parent 9c10fde commit 3d46957

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/DotNext.Metaprogramming/Threading/Tasks/CompletedTask.cs

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
using System.Runtime.CompilerServices;
3-
using System.Runtime.InteropServices;
1+
using System.Runtime.InteropServices;
42

53
namespace DotNext.Threading.Tasks;
64

@@ -37,41 +35,31 @@ public static implicit operator ValueTask(CompletedTask task)
3735
[StructLayout(LayoutKind.Auto)]
3836
internal readonly struct CompletedTask<T>
3937
{
40-
private readonly Exception? failure;
41-
42-
[AllowNull]
43-
private readonly T result;
38+
private readonly Result<T> result;
4439

4540
/// <summary>
4641
/// Creates task that has completed with a specified exception.
4742
/// </summary>
4843
/// <param name="failure">The exception with which to complete the task.</param>
49-
public CompletedTask(Exception failure) => this.failure = failure;
44+
public CompletedTask(Exception failure) => result = new(failure);
5045

5146
/// <summary>
5247
/// Creates task that has completed successfully with a specified result.
5348
/// </summary>
5449
/// <param name="result">The task result.</param>
55-
public CompletedTask(T result) => this.result = result;
50+
public CompletedTask(T result) => this.result = new(result);
5651

5752
/// <summary>
5853
/// Obtains <see cref="Task{TResult}"/> completed synchronously.
5954
/// </summary>
6055
/// <param name="task">Completed task.</param>
6156
public static implicit operator Task<T>(CompletedTask<T> task)
62-
=> task.failure is null ? Task.FromResult(task.result) : Task.FromException<T>(task.failure);
57+
=> task.result.AsTask();
6358

6459
/// <summary>
6560
/// Obtains <see cref="ValueTask{TResult}"/> completed synchronously.
6661
/// </summary>
6762
/// <param name="task">Completed task.</param>
6863
public static implicit operator ValueTask<T>(CompletedTask<T> task)
69-
{
70-
var builder = AsyncValueTaskMethodBuilder<T>.Create();
71-
if (task.failure is null)
72-
builder.SetResult(task.result);
73-
else
74-
builder.SetException(task.failure);
75-
return builder.Task;
76-
}
64+
=> new(task.result.AsTask());
7765
}

src/DotNext/Result.cs

+19
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ public unsafe T OrInvoke(delegate*<Exception, T> defaultFunc)
315315
public static Result<T> operator |(in Result<T> x, in Result<T> y)
316316
=> x.IsSuccessful ? x : y;
317317

318+
/// <summary>
319+
/// Converts this result to <see cref="Task{TResult}"/>.
320+
/// </summary>
321+
/// <returns>The completed task representing the result.</returns>
322+
public Task<T> AsTask()
323+
=> exception?.SourceException switch
324+
{
325+
null => Task.FromResult(value),
326+
OperationCanceledException canceledEx => Task.FromCanceled<T>(canceledEx.CancellationToken),
327+
{ } error => Task.FromException<T>(error),
328+
};
329+
330+
/// <summary>
331+
/// Converts the result to <see cref="Task{TResult}"/>.
332+
/// </summary>
333+
/// <param name="result">The result to be converted.</param>
334+
/// <returns>The completed task representing the result.</returns>
335+
public static explicit operator Task<T>(in Result<T> result) => result.AsTask();
336+
318337
/// <summary>
319338
/// Gets boxed representation of the result.
320339
/// </summary>

0 commit comments

Comments
 (0)