authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-04-11 16:26:29+02:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-04-11 16:26:29+02:00
loga5007d819a0bd4d247602786a36cabae821f52b9
tree6d43960f769b8f5375f07aa9bdffd4b8e25e0d43
parentcab1d5c1a320239af5be88571d7989ba230b90be

std.meta: add isError


4 files changed, 23 insertions(+), 14 deletions(-)

lib/std/json/test.zig+2-4
...@@ -29,8 +29,7 @@ fn ok(s: []const u8) !void {...@@ -29,8 +29,7 @@ fn ok(s: []const u8) !void {
29fn err(s: []const u8) void {29fn err(s: []const u8) void {
30 testing.expect(!json.validate(s));30 testing.expect(!json.validate(s));
3131
32 testNonStreaming(s) catch return;32 testing.expect(std.meta.isError(testNonStreaming(s)));
33 testing.expect(false);
34}33}
3534
36fn utf8Error(s: []const u8) void {35fn utf8Error(s: []const u8) void {
...@@ -48,8 +47,7 @@ fn any(s: []const u8) void {...@@ -48,8 +47,7 @@ fn any(s: []const u8) void {
48fn anyStreamingErrNonStreaming(s: []const u8) void {47fn anyStreamingErrNonStreaming(s: []const u8) void {
49 _ = json.validate(s);48 _ = json.validate(s);
5049
51 testNonStreaming(s) catch return;50 testing.expect(std.meta.isError(testNonStreaming(s)));
52 testing.expect(false);
53}51}
5452
55fn roundTrip(s: []const u8) !void {53fn roundTrip(s: []const u8) !void {
lib/std/meta.zig+10
...@@ -1334,3 +1334,13 @@ test "shuffleVectorIndex" {...@@ -1334,3 +1334,13 @@ test "shuffleVectorIndex" {
1334 testing.expect(shuffleVectorIndex(6, vector_len) == -3);1334 testing.expect(shuffleVectorIndex(6, vector_len) == -3);
1335 testing.expect(shuffleVectorIndex(7, vector_len) == -4);1335 testing.expect(shuffleVectorIndex(7, vector_len) == -4);
1336}1336}
1337
1338/// Returns whether `error_union` contains an error.
1339pub fn isError(error_union: anytype) bool {
1340 return if(error_union) |_| false else |_| true;
1341}
1342
1343test "isError" {
1344 std.testing.expect(isError(math.absInt(@as(i8, -128))));
1345 std.testing.expect(!isError(math.absInt(@as(i8, -127))));
1346}
lib/std/unicode.zig+1-1
...@@ -206,7 +206,7 @@ pub fn utf8ValidateSlice(s: []const u8) bool {...@@ -206,7 +206,7 @@ pub fn utf8ValidateSlice(s: []const u8) bool {
206 return false;206 return false;
207 }207 }
208208
209 if (utf8Decode(s[i .. i + cp_len])) |_| {} else |_| {209 if (std.meta.isError(utf8Decode(s[i .. i + cp_len]))) {
210 return false;210 return false;
211 }211 }
212 i += cp_len;212 i += cp_len;
src/translate_c.zig+10-9
...@@ -8,6 +8,7 @@ const ctok = std.c.tokenizer;...@@ -8,6 +8,7 @@ const ctok = std.c.tokenizer;
8const CToken = std.c.Token;8const CToken = std.c.Token;
9const mem = std.mem;9const mem = std.mem;
10const math = std.math;10const math = std.math;
11const meta = std.meta;
11const ast = @import("translate_c/ast.zig");12const ast = @import("translate_c/ast.zig");
12const Node = ast.Node;13const Node = ast.Node;
13const Tag = Node.Tag;14const Tag = Node.Tag;
...@@ -1737,7 +1738,7 @@ fn transImplicitCastExpr(...@@ -1737,7 +1738,7 @@ fn transImplicitCastExpr(
1737}1738}
17381739
1739fn isBuiltinDefined(name: []const u8) bool {1740fn isBuiltinDefined(name: []const u8) bool {
1740 inline for (std.meta.declarations(c_builtins)) |decl| {1741 inline for (meta.declarations(c_builtins)) |decl| {
1741 if (std.mem.eql(u8, name, decl.name)) return true;1742 if (std.mem.eql(u8, name, decl.name)) return true;
1742 }1743 }
1743 return false;1744 return false;
...@@ -3136,7 +3137,7 @@ const ClangFunctionType = union(enum) {...@@ -3136,7 +3137,7 @@ const ClangFunctionType = union(enum) {
3136 NoProto: *const clang.FunctionType,3137 NoProto: *const clang.FunctionType,
31373138
3138 fn getReturnType(self: @This()) clang.QualType {3139 fn getReturnType(self: @This()) clang.QualType {
3139 switch (@as(std.meta.Tag(@This()), self)) {3140 switch (@as(meta.Tag(@This()), self)) {
3140 .Proto => return self.Proto.getReturnType(),3141 .Proto => return self.Proto.getReturnType(),
3141 .NoProto => return self.NoProto.getReturnType(),3142 .NoProto => return self.NoProto.getReturnType(),
3142 }3143 }
...@@ -4072,7 +4073,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {...@@ -4072,7 +4073,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node {
4072}4073}
40734074
4074fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node {4075fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node {
4075 const fmt_s = if (comptime std.meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";4076 const fmt_s = if (comptime meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}";
4076 const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});4077 const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num});
4077 if (num_kind == .float)4078 if (num_kind == .float)
4078 return Tag.float_literal.create(c.arena, str)4079 return Tag.float_literal.create(c.arena, str)
...@@ -4827,12 +4828,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {...@@ -4827,12 +4828,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
4827 // make the output less noisy by skipping promoteIntLiteral where4828 // make the output less noisy by skipping promoteIntLiteral where
4828 // it's guaranteed to not be required because of C standard type constraints4829 // it's guaranteed to not be required because of C standard type constraints
4829 const guaranteed_to_fit = switch (suffix) {4830 const guaranteed_to_fit = switch (suffix) {
4830 .none => if (math.cast(i16, value)) |_| true else |_| false,4831 .none => !meta.isError(math.cast(i16, value)),
4831 .u => if (math.cast(u16, value)) |_| true else |_| false,4832 .u => !meta.isError(math.cast(u16, value)),
4832 .l => if (math.cast(i32, value)) |_| true else |_| false,4833 .l => !meta.isError(math.cast(i32, value)),
4833 .lu => if (math.cast(u32, value)) |_| true else |_| false,4834 .lu => !meta.isError(math.cast(u32, value)),
4834 .ll => if (math.cast(i64, value)) |_| true else |_| false,4835 .ll => !meta.isError(math.cast(i64, value)),
4835 .llu => if (math.cast(u64, value)) |_| true else |_| false,4836 .llu => !meta.isError(math.cast(u64, value)),
4836 .f => unreachable,4837 .f => unreachable,
4837 };4838 };
48384839