Skip to content

Commit 4f8ff6a

Browse files
committed
Fixed task support
1 parent b9c589c commit 4f8ff6a

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ internal readonly struct CompletedTask<T>
5454
/// </summary>
5555
/// <param name="task">Completed task.</param>
5656
public static implicit operator Task<T>(CompletedTask<T> task)
57-
=> task.result.AsTask();
57+
=> task.result.AsTask().AsTask();
5858

5959
/// <summary>
6060
/// Obtains <see cref="ValueTask{TResult}"/> completed synchronously.
6161
/// </summary>
6262
/// <param name="task">Completed task.</param>
6363
public static implicit operator ValueTask<T>(CompletedTask<T> task)
64-
=> new(task.result.AsTask());
64+
=> task.result.AsTask();
6565
}

src/DotNext.Tests/ResultTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,12 @@ private static TResult FromError<TError, TResult>(TError error)
230230
public static async Task ConvertToTask()
231231
{
232232
Result<int> result = 42;
233-
var task = (Task<int>)result;
234-
Equal(42, await task);
233+
Equal(42, await (ValueTask<int>)result);
235234

236235
result = Result.FromException<int>(new OperationCanceledException(new CancellationToken(canceled: true)));
237-
task = (Task<int>)result;
238-
True(task.IsCanceled);
236+
True(result.AsTask().IsCanceled);
239237

240238
result = Result.FromException<int>(new Exception());
241-
task = (Task<int>)result;
242-
await ThrowsAsync<Exception>(Func.Constant(task));
239+
await ThrowsAsync<Exception>(result.AsTask().AsTask);
243240
}
244241
}

src/DotNext/Result.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,20 @@ public unsafe T OrInvoke(delegate*<Exception, T> defaultFunc)
319319
/// Converts this result to <see cref="Task{TResult}"/>.
320320
/// </summary>
321321
/// <returns>The completed task representing the result.</returns>
322-
public Task<T> AsTask()
322+
public ValueTask<T> AsTask()
323323
=> exception?.SourceException switch
324324
{
325-
null => Task.FromResult(value),
326-
OperationCanceledException canceledEx => Task.FromCanceled<T>(canceledEx.CancellationToken),
327-
{ } error => Task.FromException<T>(error),
325+
null => new(value),
326+
OperationCanceledException canceledEx => ValueTask.FromCanceled<T>(canceledEx.CancellationToken),
327+
{ } error => ValueTask.FromException<T>(error),
328328
};
329329

330330
/// <summary>
331331
/// Converts the result to <see cref="Task{TResult}"/>.
332332
/// </summary>
333333
/// <param name="result">The result to be converted.</param>
334334
/// <returns>The completed task representing the result.</returns>
335-
public static explicit operator Task<T>(in Result<T> result) => result.AsTask();
335+
public static explicit operator ValueTask<T>(in Result<T> result) => result.AsTask();
336336

337337
/// <summary>
338338
/// Gets boxed representation of the result.

0 commit comments

Comments
 (0)