From f9c5f72fbcd01ea62c2069263b4ecb9bda8e4b55 Mon Sep 17 00:00:00 2001 From: jmdavis Date: Sun, 11 Nov 2012 01:51:25 -0800 Subject: [PATCH] Added zero property to Duration, TickDuration, and FracSec. It makes it more pleasant to get those types with a value of zero that way. --- src/core/time.d | 56 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/src/core/time.d b/src/core/time.d index 9c46d6c4427..2c2c4d2ccdd 100644 --- a/src/core/time.d +++ b/src/core/time.d @@ -109,6 +109,17 @@ struct Duration public: + /++ + A $(D Duration) of $(D 0). It's shorter than doing something like + $(D dur!"seconds"(0)) and more explicit than $(D Duration.init). + +/ + static @property @safe pure nothrow Duration zero() { return Duration(0); } + + unittest + { + assert(zero == dur!"seconds"(0)); + } + /++ Compares this $(D Duration) with the given $(D Duration). @@ -1450,6 +1461,19 @@ struct TickDuration static immutable TickDuration appOrigin; + /++ + It's the same as $(D TickDuration(0)), but it's provided to be + consistent with $(D Duration) and $(D FracSec), which provide $(D zero) + properties. + +/ + static @property @safe pure nothrow TickDuration zero() { return TickDuration(0); } + + unittest + { + assert(zero == TickDuration(0)); + } + + @trusted shared static this() { version(Windows) @@ -2303,6 +2327,18 @@ struct FracSec { public: + /++ + A $(D FracSec) of $(D 0). It's shorter than doing something like + $(D FracSec.from!"msecs"(0)) and more explicit than $(D FracSec.init). + +/ + static @property @safe pure nothrow FracSec zero() { return FracSec(0); } + + unittest + { + assert(zero == FracSec.from!"msecs"(0)); + } + + /++ Create a $(D FracSec) from the given units ($(D "msecs"), $(D "usecs"), or $(D "hnsecs")). @@ -2322,7 +2358,9 @@ public: units == "hnsecs" || units == "nsecs") { - return FracSec(cast(int)convert!(units, "hnsecs")(value)); + immutable hnsecs = cast(int)convert!(units, "hnsecs")(value); + _enforceValid(hnsecs); + return FracSec(hnsecs); } unittest @@ -2368,10 +2406,7 @@ public: FracSec opUnary(string op)() @safe const pure nothrow if(op == "-") { - try - return FracSec(-_hnsecs); - catch(Exception e) - assert(0, "FracSec's constructor threw."); + return FracSec(-_hnsecs); } unittest @@ -2425,7 +2460,6 @@ public: @property void msecs(int milliseconds) @safe pure { immutable hnsecs = cast(int)convert!("msecs", "hnsecs")(milliseconds); - _enforceValid(hnsecs); _hnsecs = hnsecs; } @@ -2498,7 +2532,6 @@ public: @property void usecs(int microseconds) @safe pure { immutable hnsecs = cast(int)convert!("usecs", "hnsecs")(microseconds); - _enforceValid(hnsecs); _hnsecs = hnsecs; } @@ -2651,7 +2684,6 @@ public: @property void nsecs(long nsecs) @safe pure { immutable hnsecs = cast(int)convert!("nsecs", "hnsecs")(nsecs); - _enforceValid(hnsecs); _hnsecs = hnsecs; } @@ -2830,8 +2862,7 @@ private: +/ static bool _valid(int hnsecs) @safe pure { - enum second = convert!("seconds", "hnsecs")(1); - + immutable second = convert!("seconds", "hnsecs")(1); return hnsecs > -second && hnsecs < second; } @@ -2855,9 +2886,8 @@ private: $(D TimeException) if the given hnsecs less than 0 or would result in a $(D FracSec) not within the range (-1 second, 1 second). +/ - @safe pure this(int hnsecs) + @safe pure nothrow this(int hnsecs) { - _enforceValid(hnsecs); _hnsecs = hnsecs; } @@ -3289,7 +3319,7 @@ version(unittest) void _assertThrown(T : Throwable = Exception, E) { immutable tail = msg.length == 0 ? "." : ": " ~ msg; - throw new AssertError("assertThrown() failed: No " ~ E.stringof ~ " was thrown" ~ tail, file, line); + throw new AssertError("assertThrown() failed: No " ~ T.stringof ~ " was thrown" ~ tail, file, line); } }