authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-08-26 13:38:58-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-09-07 06:23:27-06:00
log771f35c59381dc6359640d7f572411d24e379240
treeaaaacae0132fb1939abb825ddc49d066bf02adeb
parentac19ccf5955488a43b29ea13675c33426aada430
signaturelock-open Commit is signed but in an unrecognized format.

Use less inefficient method of replacing TypeInfo.UnionField.enum_field


5 files changed, 8 insertions(+), 58 deletions(-)

lib/std/fmt.zig+1-7
...@@ -395,17 +395,11 @@ pub fn formatType(...@@ -395,17 +395,11 @@ pub fn formatType(
395 }395 }
396 const info = @typeInfo(T).Union;396 const info = @typeInfo(T).Union;
397 if (info.tag_type) |UnionTagType| {397 if (info.tag_type) |UnionTagType| {
398 const tag_info = @typeInfo(UnionTagType).Enum;
399 try writer.writeAll("{ .");398 try writer.writeAll("{ .");
400 try writer.writeAll(@tagName(@as(UnionTagType, value)));399 try writer.writeAll(@tagName(@as(UnionTagType, value)));
401 try writer.writeAll(" = ");400 try writer.writeAll(" = ");
402 inline for (info.fields) |u_field| {401 inline for (info.fields) |u_field| {
403 comptime var tag_value: @TagType(UnionTagType) = undefined;402 if (value == @field(UnionTagType, u_field.name)) {
404 inline for (tag_info.fields) |e_field| {
405 if (comptime mem.eql(u8, u_field.name, e_field.name))
406 tag_value = e_field.value;
407 }
408 if (@enumToInt(@as(UnionTagType, value)) == tag_value) {
409 try formatType(@field(value, u_field.name), fmt, options, writer, max_depth - 1);403 try formatType(@field(value, u_field.name), fmt, options, writer, max_depth - 1);
410 }404 }
411 }405 }
lib/std/hash/auto_hash.zig+1-7
...@@ -136,16 +136,10 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {...@@ -136,16 +136,10 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
136136
137 .Union => |info| {137 .Union => |info| {
138 if (info.tag_type) |tag_type| {138 if (info.tag_type) |tag_type| {
139 const tag_info = @typeInfo(tag_type).Enum;
140 const tag = meta.activeTag(key);139 const tag = meta.activeTag(key);
141 const s = hash(hasher, tag, strat);140 const s = hash(hasher, tag, strat);
142 inline for (info.fields) |field| {141 inline for (info.fields) |field| {
143 comptime var tag_value: @TagType(tag_type) = undefined;142 if (@field(tag_type, field.name) == tag) {
144 inline for (tag_info.fields) |enum_field| {
145 if (comptime mem.eql(u8, field.name, enum_field.name))
146 tag_value = enum_field.value;
147 }
148 if (@enumToInt(tag) == tag_value) {
149 hash(hasher, @field(key, field.name), strat);143 hash(hasher, @field(key, field.name), strat);
150 // TODO use a labelled break when it does not crash the compiler. cf #2908144 // TODO use a labelled break when it does not crash the compiler. cf #2908
151 // break :blk;145 // break :blk;
lib/std/io/serialization.zig+2-16
...@@ -149,7 +149,6 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -149,7 +149,6 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
149 .Union => {149 .Union => {
150 const info = @typeInfo(C).Union;150 const info = @typeInfo(C).Union;
151 if (info.tag_type) |TagType| {151 if (info.tag_type) |TagType| {
152 const tag_info = @typeInfo(TagType).Enum;
153 //we avoid duplicate iteration over the enum tags152 //we avoid duplicate iteration over the enum tags
154 // by getting the int directly and casting it without153 // by getting the int directly and casting it without
155 // safety. If it is bad, it will be caught anyway.154 // safety. If it is bad, it will be caught anyway.
...@@ -157,13 +156,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -157,13 +156,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
157 const tag = try self.deserializeInt(TagInt);156 const tag = try self.deserializeInt(TagInt);
158157
159 inline for (info.fields) |field_info| {158 inline for (info.fields) |field_info| {
160 comptime var tag_value: TagInt = undefined;159 if (@enumToInt(@field(TagType, field_info.name)) == tag) {
161 inline for (tag_info.fields) |enum_field_info| {
162 if (comptime std.mem.eql(u8, field_info.name, enum_field_info.name)) {
163 tag_value = enum_field_info.value;
164 }
165 }
166 if (tag_value == tag) {
167 const name = field_info.name;160 const name = field_info.name;
168 const FieldType = field_info.field_type;161 const FieldType = field_info.field_type;
169 ptr.* = @unionInit(C, name, undefined);162 ptr.* = @unionInit(C, name, undefined);
...@@ -321,20 +314,13 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -321,20 +314,13 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
321 .Union => {314 .Union => {
322 const info = @typeInfo(T).Union;315 const info = @typeInfo(T).Union;
323 if (info.tag_type) |TagType| {316 if (info.tag_type) |TagType| {
324 const tag_info = @typeInfo(TagType).Enum;
325 const active_tag = meta.activeTag(value);317 const active_tag = meta.activeTag(value);
326 try self.serialize(active_tag);318 try self.serialize(active_tag);
327 //This inline loop is necessary because active_tag is a runtime319 //This inline loop is necessary because active_tag is a runtime
328 // value, but @field requires a comptime value. Our alternative320 // value, but @field requires a comptime value. Our alternative
329 // is to check each field for a match321 // is to check each field for a match
330 inline for (info.fields) |field_info| {322 inline for (info.fields) |field_info| {
331 comptime var tag_value: @TagType(TagType) = undefined;323 if (@field(TagType, field_info.name) == active_tag) {
332 inline for (tag_info.fields) |enum_field_info| {
333 if (comptime std.mem.eql(u8, field_info.name, enum_field_info.name)) {
334 tag_value = enum_field_info.value;
335 }
336 }
337 if (tag_value == @enumToInt(active_tag)) {
338 const name = field_info.name;324 const name = field_info.name;
339 const FieldType = field_info.field_type;325 const FieldType = field_info.field_type;
340 try self.serialize(@field(value, name));326 try self.serialize(@field(value, name));
lib/std/json.zig+2-14
...@@ -1612,14 +1612,8 @@ pub fn parseFree(comptime T: type, value: T, options: ParseOptions) void {...@@ -1612,14 +1612,8 @@ pub fn parseFree(comptime T: type, value: T, options: ParseOptions) void {
1612 },1612 },
1613 .Union => |unionInfo| {1613 .Union => |unionInfo| {
1614 if (unionInfo.tag_type) |UnionTagType| {1614 if (unionInfo.tag_type) |UnionTagType| {
1615 const tag_info = @typeInfo(UnionTagType).Enum;
1616 inline for (unionInfo.fields) |u_field| {1615 inline for (unionInfo.fields) |u_field| {
1617 comptime var tag_value: @TagType(UnionTagType) = undefined;1616 if (value == @field(UnionTagType, u_field.name)) {
1618 inline for (tag_info.fields) |e_field| {
1619 if (comptime mem.eql(u8, u_field.name, e_field.name))
1620 tag_value = e_field.value;
1621 }
1622 if (@enumToInt(@as(UnionTagType, value)) == tag_value) {
1623 parseFree(u_field.field_type, @field(value, u_field.name), options);1617 parseFree(u_field.field_type, @field(value, u_field.name), options);
1624 break;1618 break;
1625 }1619 }
...@@ -2463,14 +2457,8 @@ pub fn stringify(...@@ -2463,14 +2457,8 @@ pub fn stringify(
24632457
2464 const info = @typeInfo(T).Union;2458 const info = @typeInfo(T).Union;
2465 if (info.tag_type) |UnionTagType| {2459 if (info.tag_type) |UnionTagType| {
2466 const tag_info = @typeInfo(UnionTagType).Enum;
2467 inline for (info.fields) |u_field| {2460 inline for (info.fields) |u_field| {
2468 comptime var tag_value: @TagType(UnionTagType) = undefined;2461 if (value == @field(UnionTagType, u_field.name)) {
2469 inline for (tag_info.fields) |e_field| {
2470 if (comptime mem.eql(u8, u_field.name, e_field.name))
2471 tag_value = e_field.value;
2472 }
2473 if (@enumToInt(@as(UnionTagType, value)) == tag_value) {
2474 return try stringify(@field(value, u_field.name), options, out_stream);2462 return try stringify(@field(value, u_field.name), options, out_stream);
2475 }2463 }
2476 }2464 }
lib/std/meta.zig+2-14
...@@ -467,14 +467,8 @@ pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {...@@ -467,14 +467,8 @@ pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {
467 const info = @typeInfo(U).Union;467 const info = @typeInfo(U).Union;
468 const tag_info = @typeInfo(@TagType(U)).Enum;468 const tag_info = @typeInfo(@TagType(U)).Enum;
469469
470 comptime var name: []const u8 = undefined;
471 inline for (tag_info.fields) |enum_field_info| {
472 if (@enumToInt(tag) == enum_field_info.value)
473 name = enum_field_info.name;
474 }
475
476 inline for (info.fields) |field_info| {470 inline for (info.fields) |field_info| {
477 if (comptime mem.eql(u8, field_info.name, name))471 if (comptime mem.eql(u8, field_info.name, @tagName(tag)))
478 return field_info.field_type;472 return field_info.field_type;
479 }473 }
480474
...@@ -518,14 +512,8 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {...@@ -518,14 +512,8 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
518 const tag_b = activeTag(b);512 const tag_b = activeTag(b);
519 if (tag_a != tag_b) return false;513 if (tag_a != tag_b) return false;
520514
521 const tag_info = @typeInfo(Tag).Enum;
522 inline for (info.fields) |field_info| {515 inline for (info.fields) |field_info| {
523 comptime var tag_value: @TagType(Tag) = undefined;516 if (@field(Tag, field_info.name) == tag_a) {
524 inline for (tag_info.fields) |enum_field_info| {
525 if (comptime mem.eql(u8, field_info.name, enum_field_info.name))
526 tag_value = enum_field_info.value;
527 }
528 if (tag_value == @enumToInt(tag_a)) {
529 return eql(@field(a, field_info.name), @field(b, field_info.name));517 return eql(@field(a, field_info.name), @field(b, field_info.name));
530 }518 }
531 }519 }