1
- using System . Diagnostics . CodeAnalysis ;
2
- using System . Runtime . CompilerServices ;
3
- using System . Runtime . InteropServices ;
1
+ using System . Runtime . InteropServices ;
4
2
5
3
namespace DotNext . Threading . Tasks ;
6
4
@@ -37,41 +35,31 @@ public static implicit operator ValueTask(CompletedTask task)
37
35
[ StructLayout ( LayoutKind . Auto ) ]
38
36
internal readonly struct CompletedTask < T >
39
37
{
40
- private readonly Exception ? failure ;
41
-
42
- [ AllowNull ]
43
- private readonly T result ;
38
+ private readonly Result < T > result ;
44
39
45
40
/// <summary>
46
41
/// Creates task that has completed with a specified exception.
47
42
/// </summary>
48
43
/// <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 ) ;
50
45
51
46
/// <summary>
52
47
/// Creates task that has completed successfully with a specified result.
53
48
/// </summary>
54
49
/// <param name="result">The task result.</param>
55
- public CompletedTask ( T result ) => this . result = result ;
50
+ public CompletedTask ( T result ) => this . result = new ( result ) ;
56
51
57
52
/// <summary>
58
53
/// Obtains <see cref="Task{TResult}"/> completed synchronously.
59
54
/// </summary>
60
55
/// <param name="task">Completed task.</param>
61
56
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 ( ) ;
63
58
64
59
/// <summary>
65
60
/// Obtains <see cref="ValueTask{TResult}"/> completed synchronously.
66
61
/// </summary>
67
62
/// <param name="task">Completed task.</param>
68
63
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 ( ) ) ;
77
65
}
0 commit comments