7
7
8
8
using System ;
9
9
using System . Threading . Tasks ;
10
+ #nullable enable
10
11
11
12
namespace Akka . Util
12
13
{
13
- //A generic type can't have a explicit layout
14
- //[StructLayout(LayoutKind.Explicit)]
15
14
/// <summary>
16
- /// TBD
15
+ /// A result type frequently used inside Akka.Streams and elsewhere.
17
16
/// </summary>
18
- /// <typeparam name="T">TBD</typeparam>
19
- public struct Result < T > : IEquatable < Result < T > >
17
+ public readonly record struct Result < T >
20
18
{
21
- //[FieldOffset(0)]
22
19
/// <summary>
23
- /// TBD
20
+ /// <c>true</c> if the result is successful, <c>false</c> otherwise.
24
21
/// </summary>
25
22
public readonly bool IsSuccess ;
26
- //[FieldOffset(1)]
27
- /// <summary>
28
- /// TBD
29
- /// </summary>
30
- public readonly T Value ;
31
- //[FieldOffset(1)]
23
+
32
24
/// <summary>
33
- /// TBD
25
+ /// <c>null</c> when <see cref="IsSuccess"/> is <c>false</c>.
34
26
/// </summary>
35
- public readonly Exception Exception ;
27
+ public readonly T ? Value ;
36
28
37
29
/// <summary>
38
- /// TBD
30
+ /// <c>null</c> when <see cref="IsSuccess"/> is <c>true</c>.
39
31
/// </summary>
40
- /// <param name="value">TBD</param>
32
+ public readonly Exception ? Exception ;
33
+
41
34
public Result ( T value ) : this ( )
42
35
{
43
36
IsSuccess = true ;
44
37
Value = value ;
45
38
}
46
- /// <summary>
47
- /// TBD
48
- /// </summary>
49
- /// <param name="exception">TBD</param>
39
+
50
40
public Result ( Exception exception ) : this ( )
51
41
{
52
42
IsSuccess = false ;
53
43
Exception = exception ;
54
44
}
55
-
56
-
57
- public bool Equals ( Result < T > other )
58
- {
59
- if ( IsSuccess ^ other . IsSuccess ) return false ;
60
- return IsSuccess
61
- ? Equals ( Value , other . Value )
62
- : Equals ( Exception , other . Exception ) ;
63
- }
64
-
65
45
66
- public override bool Equals ( object obj )
67
- {
68
- if ( obj is Result < T > result ) return Equals ( result ) ;
69
- return false ;
70
- }
71
-
72
-
73
- public override int GetHashCode ( )
74
- {
75
- return IsSuccess
76
- ? ( Value == null ? 0 : Value . GetHashCode ( ) )
77
- : ( Exception == null ? 0 : Exception . GetHashCode ( ) ) ;
78
- }
79
-
80
- /// <summary>
81
- /// Compares two specified <see cref="Result{T}"/> for equality.
82
- /// </summary>
83
- /// <param name="left">The first <see cref="Result{T}"/> used for comparison</param>
84
- /// <param name="right">The second <see cref="Result{T}"/> used for comparison</param>
85
- /// <returns><c>true</c> if both <see cref="Result{T}"/> are equal; otherwise <c>false</c></returns>
86
- public static bool operator == ( Result < T > left , Result < T > right )
87
- {
88
- return left . Equals ( right ) ;
89
- }
90
-
91
- /// <summary>
92
- /// Compares two specified <see cref="Result{T}"/> for inequality.
93
- /// </summary>
94
- /// <param name="left">The first <see cref="Result{T}"/> used for comparison</param>
95
- /// <param name="right">The second <see cref="Result{T}"/> used for comparison</param>
96
- /// <returns><c>true</c> if both <see cref="Result{T}"/> are not equal; otherwise <c>false</c></returns>
97
- public static bool operator != ( Result < T > left , Result < T > right )
98
- {
99
- return ! ( left == right ) ;
100
- }
101
-
102
46
public override string ToString ( ) => IsSuccess ? $ "Success ({ Value } )" : $ "Failure ({ Exception } )";
103
47
}
104
48
105
49
/// <summary>
106
- /// TBD
50
+ /// Helper methods for creating <see cref="Result{T}"/> instances.
107
51
/// </summary>
108
52
public static class Result
109
53
{
110
- /// <summary>
111
- /// TBD
112
- /// </summary>
113
- /// <typeparam name="T">TBD</typeparam>
114
- /// <param name="value">TBD</param>
115
- /// <returns>TBD</returns>
116
54
public static Result < T > Success < T > ( T value )
117
55
{
118
56
return new Result < T > ( value ) ;
119
57
}
120
-
121
- /// <summary>
122
- /// TBD
123
- /// </summary>
124
- /// <typeparam name="T">TBD</typeparam>
125
- /// <param name="exception">TBD</param>
126
- /// <returns>TBD</returns>
58
+
127
59
public static Result < T > Failure < T > ( Exception exception )
128
60
{
129
61
return new Result < T > ( exception ) ;
130
62
}
131
-
132
- /// <summary>
133
- /// TBD
134
- /// </summary>
135
- /// <typeparam name="T">TBD</typeparam>
136
- /// <param name="task">TBD</param>
137
- /// <returns>TBD</returns>
63
+
138
64
public static Result < T > FromTask < T > ( Task < T > task )
139
65
{
140
66
if ( ! task . IsCompleted )
@@ -143,7 +69,7 @@ public static Result<T> FromTask<T>(Task<T> task)
143
69
if ( task . Exception is not null )
144
70
return new Result < T > ( task . Exception ) ;
145
71
146
- if ( task . IsCanceled && task . Exception is null )
72
+ if ( task is { IsCanceled : true , Exception : null } )
147
73
{
148
74
try
149
75
{
@@ -157,18 +83,12 @@ public static Result<T> FromTask<T>(Task<T> task)
157
83
throw new InvalidOperationException ( "Should never reach this line!" ) ;
158
84
}
159
85
160
- if ( task . IsFaulted && task . Exception is null )
86
+ if ( task is { IsFaulted : true , Exception : null } )
161
87
throw new InvalidOperationException ( "Should never happen! something is wrong with .NET Task code!" ) ;
162
88
163
89
return new Result < T > ( task . Result ) ;
164
90
}
165
-
166
- /// <summary>
167
- /// TBD
168
- /// </summary>
169
- /// <typeparam name="T">TBD</typeparam>
170
- /// <param name="func">TBD</param>
171
- /// <returns>TBD</returns>
91
+
172
92
public static Result < T > From < T > ( Func < T > func )
173
93
{
174
94
try
@@ -181,8 +101,5 @@ public static Result<T> From<T>(Func<T> func)
181
101
return new Result < T > ( e ) ;
182
102
}
183
103
}
184
-
185
-
186
-
187
104
}
188
105
}
0 commit comments