Skip to content

Commit 3f0ecdf

Browse files
author
Benjamin Feng
committed
Enable structs
1 parent f2060ef commit 3f0ecdf

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

lib/std/fmtgen.zig

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -412,29 +412,28 @@ pub fn formatType(
412412
return formatInt(@ptrToInt(&value), 16, false, FormatOptions{}, generator);
413413
}
414414
},
415-
// .Struct => {
416-
// if (comptime std.meta.trait.hasFn("format")(T)) {
417-
// return value.format(fmt, options, context, Errors, output);
418-
// }
419-
420-
// try output(context, @typeName(T));
421-
// if (max_depth == 0) {
422-
// return output(context, "{ ... }");
423-
// }
424-
// comptime var field_i = 0;
425-
// try output(context, "{");
426-
// inline while (field_i < @memberCount(T)) : (field_i += 1) {
427-
// if (field_i == 0) {
428-
// try output(context, " .");
429-
// } else {
430-
// try output(context, ", .");
431-
// }
432-
// try output(context, @memberName(T, field_i));
433-
// try output(context, " = ");
434-
// try formatType(@field(value, @memberName(T, field_i)), "", options, context, Errors, output, max_depth - 1);
435-
// }
436-
// try output(context, " }");
437-
// },
415+
.Struct => {
416+
// if (comptime std.meta.trait.hasFn("format")(T)) {
417+
// return value.format(fmt, options, context, Errors, output);
418+
// }
419+
generator.yield(@typeName(T));
420+
if (max_depth == 0) {
421+
return generator.yield("{ ... }");
422+
}
423+
comptime var field_i = 0;
424+
generator.yield("{");
425+
inline while (field_i < @memberCount(T)) : (field_i += 1) {
426+
if (field_i == 0) {
427+
generator.yield(" .");
428+
} else {
429+
generator.yield(", .");
430+
}
431+
generator.yield(@memberName(T, field_i));
432+
generator.yield(" = ");
433+
formatType(@field(value, @memberName(T, field_i)), "", options, generator, max_depth - 1);
434+
}
435+
generator.yield(" }");
436+
},
438437
.Pointer => |ptr_info| switch (ptr_info.size) {
439438
.One => switch (@typeInfo(ptr_info.child)) {
440439
builtin.TypeId.Array => |info| {
@@ -1305,24 +1304,24 @@ test "cstr" {
13051304
// try testFmt("file size: 66.06MB\n", "file size: {B:.2}\n", .{@as(usize, 63 * 1024 * 1024)});
13061305
// }
13071306

1308-
// test "struct" {
1309-
// {
1310-
// const Struct = struct {
1311-
// field: u8,
1312-
// };
1313-
// const value = Struct{ .field = 42 };
1314-
// try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{value});
1315-
// try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{&value});
1316-
// }
1317-
// {
1318-
// const Struct = struct {
1319-
// a: u0,
1320-
// b: u1,
1321-
// };
1322-
// const value = Struct{ .a = 0, .b = 1 };
1323-
// try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
1324-
// }
1325-
// }
1307+
test "struct" {
1308+
{
1309+
const Struct = struct {
1310+
field: u8,
1311+
};
1312+
const value = Struct{ .field = 42 };
1313+
try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{value});
1314+
try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{&value});
1315+
}
1316+
{
1317+
const Struct = struct {
1318+
a: u0,
1319+
b: u1,
1320+
};
1321+
const value = Struct{ .a = 0, .b = 1 };
1322+
try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
1323+
}
1324+
}
13261325

13271326
test "enum" {
13281327
const Enum = enum {
@@ -1455,19 +1454,19 @@ test "enum" {
14551454
// try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{value});
14561455
// }
14571456

1458-
// test "struct" {
1459-
// const S = struct {
1460-
// a: u32,
1461-
// b: anyerror,
1462-
// };
1457+
test "struct" {
1458+
const S = struct {
1459+
a: u32,
1460+
b: anyerror,
1461+
};
14631462

1464-
// const inst = S{
1465-
// .a = 456,
1466-
// .b = error.Unused,
1467-
// };
1463+
const inst = S{
1464+
.a = 456,
1465+
.b = error.Unused,
1466+
};
14681467

1469-
// try testFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst});
1470-
// }
1468+
try testFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst});
1469+
}
14711470

14721471
test "union" {
14731472
const TU = union(enum) {

0 commit comments

Comments
 (0)