mirror of
https://github.com/dlang/phobos.git
synced 2025-05-11 23:05:34 +03:00
Turn enforce into an eponymous template + undocument enforceEx
This commit is contained in:
parent
a10c5ce123
commit
611e62c96f
5 changed files with 51 additions and 19 deletions
13
changelog/std-exception-enforce.dd
Normal file
13
changelog/std-exception-enforce.dd
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
std.exception.enforce can now be used as an eponymous template to create your own enforce function
|
||||||
|
|
||||||
|
$(REF enforce, std,exception) now mirrors the behavior of $(REF enforceEx, std,exception)
|
||||||
|
and can be used as an eponymous template:
|
||||||
|
|
||||||
|
---
|
||||||
|
import std.conv : ConvException;
|
||||||
|
alias convEnforce = enforce!ConvException;
|
||||||
|
assertNotThrown(convEnforce(true));
|
||||||
|
assertThrown!ConvException(convEnforce(false, "blah"));
|
||||||
|
---
|
||||||
|
|
||||||
|
With this change, $(REF enforce, std,exception) is a strict superset of $(REF enforceEx, std,exception), which will be deprecated in 2.079.
|
|
@ -17,7 +17,6 @@ $(TR $(TD Assumptions) $(TD
|
||||||
$(TR $(TD Enforce) $(TD
|
$(TR $(TD Enforce) $(TD
|
||||||
$(LREF doesPointTo)
|
$(LREF doesPointTo)
|
||||||
$(LREF enforce)
|
$(LREF enforce)
|
||||||
$(LREF enforceEx)
|
|
||||||
$(LREF errnoEnforce)
|
$(LREF errnoEnforce)
|
||||||
))
|
))
|
||||||
$(TR $(TD Handlers) $(TD
|
$(TR $(TD Handlers) $(TD
|
||||||
|
@ -386,12 +385,17 @@ void assertThrown(T : Throwable = Exception, E)
|
||||||
If a delegate is passed, the safety and purity of this function are inferred
|
If a delegate is passed, the safety and purity of this function are inferred
|
||||||
from `Dg`'s safety and purity.
|
from `Dg`'s safety and purity.
|
||||||
+/
|
+/
|
||||||
T enforce(E : Throwable = Exception, T)(T value, lazy const(char)[] msg = null,
|
template enforce(E : Throwable = Exception)
|
||||||
string file = __FILE__, size_t line = __LINE__)
|
if (is(typeof(new E("", __FILE__, __LINE__)) : Throwable) || is(typeof(new E(__FILE__, __LINE__)) : Throwable))
|
||||||
if (is(typeof({ if (!value) {} })))
|
|
||||||
{
|
{
|
||||||
if (!value) bailOut!E(file, line, msg);
|
|
||||||
return value;
|
T enforce(T)(T value, lazy const(char)[] msg = null,
|
||||||
|
string file = __FILE__, size_t line = __LINE__)
|
||||||
|
if (is(typeof({ if (!value) {} })))
|
||||||
|
{
|
||||||
|
if (!value) bailOut!E(file, line, msg);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ditto
|
/// ditto
|
||||||
|
@ -455,6 +459,15 @@ unittest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Alias your own enforce function
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
import std.conv : ConvException;
|
||||||
|
alias convEnforce = enforce!ConvException;
|
||||||
|
assertNotThrown(convEnforce(true));
|
||||||
|
assertThrown!ConvException(convEnforce(false, "blah"));
|
||||||
|
}
|
||||||
|
|
||||||
private void bailOut(E : Throwable = Exception)(string file, size_t line, in char[] msg)
|
private void bailOut(E : Throwable = Exception)(string file, size_t line, in char[] msg)
|
||||||
{
|
{
|
||||||
static if (is(typeof(new E(string.init, string.init, size_t.init))))
|
static if (is(typeof(new E(string.init, string.init, size_t.init))))
|
||||||
|
@ -584,8 +597,10 @@ T errnoEnforce(T, string file = __FILE__, size_t line = __LINE__)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @@@DEPRECATED_2.084@@@
|
||||||
/++
|
/++
|
||||||
|
$(RED Deprecated. Please use $(LREF enforce) instead. This function will be removed 2.084.)
|
||||||
|
|
||||||
If $(D !value) is $(D false), $(D value) is returned. Otherwise,
|
If $(D !value) is $(D false), $(D value) is returned. Otherwise,
|
||||||
$(D new E(msg, file, line)) is thrown. Or if $(D E) doesn't take a message
|
$(D new E(msg, file, line)) is thrown. Or if $(D E) doesn't take a message
|
||||||
and can be constructed with $(D new E(file, line)), then
|
and can be constructed with $(D new E(file, line)), then
|
||||||
|
@ -600,6 +615,7 @@ T errnoEnforce(T, string file = __FILE__, size_t line = __LINE__)
|
||||||
enforceEx!DataCorruptionException(line.length);
|
enforceEx!DataCorruptionException(line.length);
|
||||||
--------------------
|
--------------------
|
||||||
+/
|
+/
|
||||||
|
//deprecated("Please use enforce instead")
|
||||||
template enforceEx(E : Throwable)
|
template enforceEx(E : Throwable)
|
||||||
if (is(typeof(new E("", string.init, size_t.init))))
|
if (is(typeof(new E("", string.init, size_t.init))))
|
||||||
{
|
{
|
||||||
|
@ -611,7 +627,8 @@ if (is(typeof(new E("", string.init, size_t.init))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/++ Ditto +/
|
/+ Ditto +/
|
||||||
|
//deprecated("Please use enforce instead")
|
||||||
template enforceEx(E : Throwable)
|
template enforceEx(E : Throwable)
|
||||||
if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.init, size_t.init))))
|
if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.init, size_t.init))))
|
||||||
{
|
{
|
||||||
|
@ -623,6 +640,7 @@ if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//deprecated
|
||||||
@system unittest
|
@system unittest
|
||||||
{
|
{
|
||||||
import core.exception : OutOfMemoryError;
|
import core.exception : OutOfMemoryError;
|
||||||
|
@ -666,6 +684,7 @@ if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.i
|
||||||
static assert(!is(typeof(enforceEx!int(true))));
|
static assert(!is(typeof(enforceEx!int(true))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//deprecated
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
alias enf = enforceEx!Exception;
|
alias enf = enforceEx!Exception;
|
||||||
|
|
|
@ -82,7 +82,7 @@ class FormatException : Exception
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private alias enforceFmt = enforceEx!FormatException;
|
private alias enforceFmt = enforce!FormatException;
|
||||||
|
|
||||||
|
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
|
|
|
@ -101,7 +101,7 @@ JSON value node
|
||||||
*/
|
*/
|
||||||
struct JSONValue
|
struct JSONValue
|
||||||
{
|
{
|
||||||
import std.exception : enforceEx, enforce;
|
import std.exception : enforce;
|
||||||
|
|
||||||
union Store
|
union Store
|
||||||
{
|
{
|
||||||
|
@ -472,7 +472,7 @@ struct JSONValue
|
||||||
ref inout(JSONValue) opIndex(size_t i) inout pure @safe
|
ref inout(JSONValue) opIndex(size_t i) inout pure @safe
|
||||||
{
|
{
|
||||||
auto a = this.arrayNoRef;
|
auto a = this.arrayNoRef;
|
||||||
enforceEx!JSONException(i < a.length,
|
enforce!JSONException(i < a.length,
|
||||||
"JSONValue array index is out of range");
|
"JSONValue array index is out of range");
|
||||||
return a[i];
|
return a[i];
|
||||||
}
|
}
|
||||||
|
@ -512,7 +512,7 @@ struct JSONValue
|
||||||
*/
|
*/
|
||||||
void opIndexAssign(T)(auto ref T value, string key) pure
|
void opIndexAssign(T)(auto ref T value, string key) pure
|
||||||
{
|
{
|
||||||
enforceEx!JSONException(type == JSON_TYPE.OBJECT || type == JSON_TYPE.NULL,
|
enforce!JSONException(type == JSON_TYPE.OBJECT || type == JSON_TYPE.NULL,
|
||||||
"JSONValue must be object or null");
|
"JSONValue must be object or null");
|
||||||
JSONValue[string] aa = null;
|
JSONValue[string] aa = null;
|
||||||
if (type == JSON_TYPE.OBJECT)
|
if (type == JSON_TYPE.OBJECT)
|
||||||
|
@ -534,7 +534,7 @@ struct JSONValue
|
||||||
void opIndexAssign(T)(T arg, size_t i) pure
|
void opIndexAssign(T)(T arg, size_t i) pure
|
||||||
{
|
{
|
||||||
auto a = this.arrayNoRef;
|
auto a = this.arrayNoRef;
|
||||||
enforceEx!JSONException(i < a.length,
|
enforce!JSONException(i < a.length,
|
||||||
"JSONValue array index is out of range");
|
"JSONValue array index is out of range");
|
||||||
a[i] = arg;
|
a[i] = arg;
|
||||||
this.array = a;
|
this.array = a;
|
||||||
|
|
|
@ -1507,8 +1507,8 @@ private:
|
||||||
version (Posix)
|
version (Posix)
|
||||||
int performWait(bool block) @trusted
|
int performWait(bool block) @trusted
|
||||||
{
|
{
|
||||||
import std.exception : enforceEx;
|
import std.exception : enforce;
|
||||||
enforceEx!ProcessException(owned, "Can't wait on a detached process");
|
enforce!ProcessException(owned, "Can't wait on a detached process");
|
||||||
if (_processID == terminated) return _exitCode;
|
if (_processID == terminated) return _exitCode;
|
||||||
int exitCode;
|
int exitCode;
|
||||||
while (true)
|
while (true)
|
||||||
|
@ -1557,8 +1557,8 @@ private:
|
||||||
{
|
{
|
||||||
int performWait(bool block) @trusted
|
int performWait(bool block) @trusted
|
||||||
{
|
{
|
||||||
import std.exception : enforceEx;
|
import std.exception : enforce;
|
||||||
enforceEx!ProcessException(owned, "Can't wait on a detached process");
|
enforce!ProcessException(owned, "Can't wait on a detached process");
|
||||||
if (_processID == terminated) return _exitCode;
|
if (_processID == terminated) return _exitCode;
|
||||||
assert(_handle != INVALID_HANDLE_VALUE);
|
assert(_handle != INVALID_HANDLE_VALUE);
|
||||||
if (block)
|
if (block)
|
||||||
|
@ -1803,8 +1803,8 @@ void kill(Pid pid)
|
||||||
/// ditto
|
/// ditto
|
||||||
void kill(Pid pid, int codeOrSignal)
|
void kill(Pid pid, int codeOrSignal)
|
||||||
{
|
{
|
||||||
import std.exception : enforceEx;
|
import std.exception : enforce;
|
||||||
enforceEx!ProcessException(pid.owned, "Can't kill detached process");
|
enforce!ProcessException(pid.owned, "Can't kill detached process");
|
||||||
version (Windows)
|
version (Windows)
|
||||||
{
|
{
|
||||||
if (codeOrSignal < 0) throw new ProcessException("Invalid exit code");
|
if (codeOrSignal < 0) throw new ProcessException("Invalid exit code");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue