11using ObjectLayoutInspector ;
2- using System . Diagnostics ;
32using System . Diagnostics . CodeAnalysis ;
4- using System . Runtime . CompilerServices ;
53using System . Runtime . InteropServices ;
64
75namespace StructTest ;
@@ -11,6 +9,12 @@ internal static class StructTest
119
1210 #region Constants & Statics
1311
12+ public static void Complex_Test ( )
13+ {
14+ var r = new Result < BaseError > ( new BaseError ( 11 , null , null ) ) ;
15+ TypeLayout . PrintLayout ( r . GetType ( ) , true ) ;
16+ }
17+
1418 public static void Simple_Test ( )
1519 {
1620 var box = new BoxStruct ( false , 0 , new InnerStruct ( 100 , true ) ) ;
@@ -66,11 +70,6 @@ public static void Simple_Test()
6670
6771 #endregion
6872
69- public static void Complex_Test ( )
70- {
71- var r = new Result < BaseError > ( new BaseError ( ) ) ;
72- TypeLayout . PrintLayout ( r . GetType ( ) , true ) ;
73- }
7473}
7574
7675#region Simple Structs
@@ -108,37 +107,19 @@ public InnerStruct(int intField, bool boolField)
108107
109108#region Complex
110109
111- // [StructLayout(LayoutKind.Auto, Pack = 1)]
112- [ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
110+ [ StructLayout ( LayoutKind . Auto , Pack = 1 ) ]
111+ // [StructLayout(LayoutKind.Sequential, Pack = 1)]
113112[ SuppressMessage ( "Style" , "IDE1006:Naming Styles" , Justification = "<Pending>" ) ]
114113public readonly record struct Result < TError >
115114 where TError : struct
116115{
117-
118- #region Constants & Statics
119-
120- /// <summary>
121- /// No errors, just return.
122- /// </summary>
123- private static readonly Result < TError > Ok = new ( true ) ;
124-
125- #endregion
126-
127116 /// <summary>
128117 /// Gets the error.
129118 /// </summary>
130119 internal readonly TError _error ;
131120
132121 internal readonly bool _hasError ;
133122
134- #pragma warning disable IDE0060 // Remove unused parameter
135- private Result ( bool isOk )
136- #pragma warning restore IDE0060 // Remove unused parameter
137- {
138- // just use for Ok
139- _hasError = false ;
140- }
141-
142123 /// <summary>
143124 /// Initializes a new instance of the <see cref="Result{TError}"/> with default <typeparamref name="TError"/>.
144125 /// </summary>
@@ -156,168 +137,30 @@ public Result(in TError error)
156137 _error = error ;
157138 _hasError = true ;
158139 }
159-
160- #region Methods
161-
162- [ UnscopedRef ]
163- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
164- internal ref readonly TError UnwrapErrorWithoutCheck ( )
165- {
166- Debug . Assert ( _hasError , $ "{ nameof ( _hasError ) } is true") ;
167- return ref _error ;
168- }
169-
170- /// <summary>
171- /// Determines whether this instance is error.
172- /// </summary>
173- /// <returns>
174- /// <c>true</c> if this instance is error; otherwise <c>false</c>.
175- /// </returns>
176- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
177- public readonly bool IsError ( )
178- {
179- return _hasError ;
180- }
181-
182- /// <summary>
183- /// Determines whether this instance is error.
184- /// </summary>
185- /// <returns>
186- /// <c>true</c> if this instance is error, and error must not be null; otherwise, <c>false</c>.
187- /// </returns>
188- [ SuppressMessage (
189- "Critical Code Smell" ,
190- "S3874:\" out\" and \" ref\" parameters should not be used" ,
191- Justification = "<Pending>" ) ]
192- public readonly bool IsError ( [ NotNullWhen ( true ) ] out TError ? error )
193- {
194- error = null ;
195-
196- if ( _hasError )
197- {
198- error = _error ;
199- return true ;
200- }
201-
202- return false ;
203- }
204-
205- #endregion
206-
207- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
208- public static implicit operator Result < TError > ( in TError error ) => new ( in error ) ;
209140}
210141
211- //[StructLayout(LayoutKind.Auto, Pack = 1)]
212- [ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
213- public readonly record struct BaseError ( BaseErrorCode Code , string ? Reason = null /*, Exception? Exception = null*/ )
214- //: IBaseError<BaseError, BaseErrorCode>
142+ [ StructLayout ( LayoutKind . Auto , Pack = 1 ) ]
143+ //[StructLayout(LayoutKind.Sequential, Pack = 1)]
144+ public readonly record struct BaseError
215145{
216- public BaseError ( ) : this ( BaseErrorCode . Failed )
217- {
218- }
219-
220- #region IBaseError implementations
221-
222- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
223- public static Result < BaseError > Result ( BaseErrorCode code , string ? reason = null , Exception ? exception = null )
224- {
225- return new BaseError ( code , reason /*, exception*/ ) ;
226- }
227-
228- #endregion
229-
230- #region IError implementations
231-
232- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
233- public static Result < BaseError > Result ( )
234- {
235- return new BaseError ( ) ;
236- }
237-
238- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
239- public static Result < BaseError > Result ( BaseErrorCode code )
240- {
241- return new BaseError ( code ) ;
242- }
243-
244- #endregion
245-
246- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
247- public static implicit operator BaseError ( BaseErrorCode errorCode ) => new ( errorCode ) ;
248- }
249-
250- public interface IError < TError , TCode >
251- where TError : struct
252- {
253-
254- #region Constants & Statics
255-
256- /// <summary>
257- /// Create a default result.
258- /// </summary>
259- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
260- public static abstract Result < TError > Result ( ) ;
261-
262- /// <summary>
263- /// Create a result with the specified code.
264- /// </summary>
265- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
266- public static abstract Result < TError > Result ( TCode code ) ;
267-
268- #endregion
269-
270- #region Properties
271-
272- /// <summary>
273- /// Gets the error code.
274- /// </summary>
275- public TCode Code { get ; }
276-
277- #endregion
278-
279- }
280-
281- public interface IBaseError < TError , TCode > : IError < TError , TCode >
282- where TError : struct
283- {
284-
285- #region Constants & Statics
286-
287- /// <summary>
288- /// Create a result with the specified code, reason and exception.
289- /// </summary>
290- /// <param name="code">The code.</param>
291- /// <param name="reason">The reason.</param>
292- /// <param name="exception">The exception.</param>
293- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
294- public static abstract Result < TError > Result ( TCode code , string ? reason = null , Exception ? exception = null ) ;
295-
296- #endregion
297-
298- #region Properties
146+ public readonly int Code ;
299147
300148 /// <summary>
301149 /// Gets the exception.
302150 /// </summary>
303- public Exception ? Exception { get ; }
151+ public readonly Exception ? Exception ;
304152
305153 /// <summary>
306154 /// Gets the reason.
307155 /// </summary>
308- public string ? Reason { get ; }
309-
310- #endregion
311-
312- }
313-
314- public enum BaseErrorCode
315- {
316- Failure = 0 ,
156+ public readonly string ? Reason ;
317157
318- Failed = 10 ,
319-
320- NotFound = 99
158+ public BaseError ( int code , Exception ? exception , string ? reason )
159+ {
160+ Code = code ;
161+ //Exception = exception;
162+ Reason = reason ;
163+ }
321164}
322165
323166#endregion
0 commit comments