@@ -5,6 +5,39 @@ Most of API Required DIP1008.
55+/
66module mir.exception ;
77
8+ version (D_Exceptions):
9+
10+ // /
11+ auto ref enforce (string fmt, string file = __FILE__ , int line = __LINE__ , Expr)(scope auto return ref Expr arg) @trusted
12+ {
13+ import mir.functional: forward;
14+ import mir.utility: _expect;
15+ static if (__traits(compiles, arg ! is null ))
16+ {
17+ if (_expect(arg ! is null , true ))
18+ return forward! arg[0 ];
19+ }
20+ else
21+ {
22+ if (_expect(cast (bool )arg, true ))
23+ return forward! arg[0 ];
24+ }
25+ static immutable exception = new Exception (fmt, file, line);
26+ throw exception;
27+ }
28+
29+ // /
30+ @safe pure nothrow @nogc
31+ version (mir_test) unittest
32+ {
33+ import mir.exception;
34+ try enforce! " Msg" (false );
35+ catch (Exception e) assert (e.msg == " Msg" );
36+ }
37+
38+ static if (__traits(compiles, (()@nogc {throw new Exception (" " );})()))
39+ {
40+
841/+ +
942+/
1043class MirException : Exception
@@ -123,33 +156,6 @@ version (mir_test) unittest
123156// catch(Exception e) assert(e.msg == "Msg");
124157// }
125158
126- // /
127- auto ref enforce (string fmt, string file = __FILE__ , int line = __LINE__ , Expr)(scope auto return ref Expr arg) @trusted
128- {
129- import mir.functional: forward;
130- import mir.utility: _expect;
131- static if (__traits(compiles, arg ! is null ))
132- {
133- if (_expect(arg ! is null , true ))
134- return forward! arg[0 ];
135- }
136- else
137- {
138- if (_expect(cast (bool )arg, true ))
139- return forward! arg[0 ];
140- }
141- static immutable exception = new Exception (fmt, file, line);
142- throw exception;
143- }
144-
145- // /
146- @safe pure nothrow @nogc
147- version (mir_test) unittest
148- {
149- import mir.exception;
150- try enforce! " Msg" (false );
151- catch (Exception e) assert (e.msg == " Msg" );
152- }
153159
154160/+ +
155161+/
@@ -283,3 +289,5 @@ private const(char)[] initilizePayload(ref return char[maxMsgLen] payload, scope
283289 (() @trusted => memcpy(payload.ptr, msg.ptr, msg.length))();
284290 return payload[0 .. msg.length];
285291}
292+
293+ }
0 commit comments