aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-12-09 20:42:08 +0100
committerLemonBoy <thatlemon@gmail.com>2020-12-10 09:23:48 +0100
commitef7db9717ec6b08a7ef7ed043819bf4855e0a828 (patch)
tree3a1796cb0ac3bd3919f7460a2fb9648d6394fb98
parent23c1b7faee13f95bce6ba48051b9ac1a1fab9576 (diff)
downloadzig-ef7db9717ec6b08a7ef7ed043819bf4855e0a828.tar.gz
zig-ef7db9717ec6b08a7ef7ed043819bf4855e0a828.zip
std: introduce meta.traits.is{Integral,Float}
-rw-r--r--lib/std/meta/trait.zig32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig
index a288b2adfd..eb294a857c 100644
--- a/lib/std/meta/trait.zig
+++ b/lib/std/meta/trait.zig
@@ -312,6 +312,38 @@ test "std.meta.trait.isNumber" {
testing.expect(!isNumber(NotANumber));
}
+pub fn isIntegral(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .Int, .ComptimeInt => true,
+ else => false,
+ };
+}
+
+test "isIntegral" {
+ testing.expect(isIntegral(u32));
+ testing.expect(!isIntegral(f32));
+ testing.expect(isIntegral(@TypeOf(102)));
+ testing.expect(!isIntegral(@TypeOf(102.123)));
+ testing.expect(!isIntegral(*u8));
+ testing.expect(!isIntegral([]u8));
+}
+
+pub fn isFloat(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .Float, .ComptimeFloat => true,
+ else => false,
+ };
+}
+
+test "isFloat" {
+ testing.expect(!isFloat(u32));
+ testing.expect(isFloat(f32));
+ testing.expect(!isFloat(@TypeOf(102)));
+ testing.expect(isFloat(@TypeOf(102.123)));
+ testing.expect(!isFloat(*f64));
+ testing.expect(!isFloat([]f32));
+}
+
pub fn isConstPtr(comptime T: type) bool {
if (!comptime is(.Pointer)(T)) return false;
return @typeInfo(T).Pointer.is_const;