Commit 5b00dee0c2

Andrew Kelley <superjoe30@gmail.com>
2018-03-27 21:20:07
std.math.cast handles signed integers
1 parent 6cb99fd
Changed files (1)
std
std/math/index.zig
@@ -515,15 +515,28 @@ test "math.negateCast" {
 
 /// Cast an integer to a different integer type. If the value doesn't fit, 
 /// return an error.
-pub fn cast(comptime T: type, x: var) !T {
+pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
     comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer
-    if (x > @maxValue(T)) {
+    comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Int); // must pass an integer
+    if (@maxValue(@typeOf(x)) > @maxValue(T) and x > @maxValue(T)) {
+        return error.Overflow;
+    } else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {
         return error.Overflow;
     } else {
         return T(x);
     }
 }
 
+test "math.cast" {
+    if (cast(u8, u32(300))) |_| @panic("fail") else |err| assert(err == error.Overflow);
+    if (cast(i8, i32(-200))) |_| @panic("fail") else |err| assert(err == error.Overflow);
+    if (cast(u8, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
+    if (cast(u64, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
+
+    assert((try cast(u8, u32(255))) == u8(255));
+    assert(@typeOf(try cast(u8, u32(255))) == u8);
+}
+
 pub fn floorPowerOfTwo(comptime T: type, value: T) T {
     var x = value;