authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-09 20:42:08+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-10 09:23:48+01:00
logef7db9717ec6b08a7ef7ed043819bf4855e0a828
tree3a1796cb0ac3bd3919f7460a2fb9648d6394fb98
parent23c1b7faee13f95bce6ba48051b9ac1a1fab9576

std: introduce meta.traits.is{Integral,Float}


1 files changed, 32 insertions(+), 0 deletions(-)

lib/std/meta/trait.zig+32
...@@ -312,6 +312,38 @@ test "std.meta.trait.isNumber" {...@@ -312,6 +312,38 @@ test "std.meta.trait.isNumber" {
312 testing.expect(!isNumber(NotANumber));312 testing.expect(!isNumber(NotANumber));
313}313}
314314
315pub fn isIntegral(comptime T: type) bool {
316 return switch (@typeInfo(T)) {
317 .Int, .ComptimeInt => true,
318 else => false,
319 };
320}
321
322test "isIntegral" {
323 testing.expect(isIntegral(u32));
324 testing.expect(!isIntegral(f32));
325 testing.expect(isIntegral(@TypeOf(102)));
326 testing.expect(!isIntegral(@TypeOf(102.123)));
327 testing.expect(!isIntegral(*u8));
328 testing.expect(!isIntegral([]u8));
329}
330
331pub fn isFloat(comptime T: type) bool {
332 return switch (@typeInfo(T)) {
333 .Float, .ComptimeFloat => true,
334 else => false,
335 };
336}
337
338test "isFloat" {
339 testing.expect(!isFloat(u32));
340 testing.expect(isFloat(f32));
341 testing.expect(!isFloat(@TypeOf(102)));
342 testing.expect(isFloat(@TypeOf(102.123)));
343 testing.expect(!isFloat(*f64));
344 testing.expect(!isFloat([]f32));
345}
346
315pub fn isConstPtr(comptime T: type) bool {347pub fn isConstPtr(comptime T: type) bool {
316 if (!comptime is(.Pointer)(T)) return false;348 if (!comptime is(.Pointer)(T)) return false;
317 return @typeInfo(T).Pointer.is_const;349 return @typeInfo(T).Pointer.is_const;