authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2019-10-15 19:29:47+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-15 18:14:13-04:00
log6a629d3a9f60b185ad24adb7b88b1f29b66e38cf
treefa7b0647013691fbb559f62999740ef338657c48
parent9050cd847a2a767df34bc3e0bbc9c789b8a49a91

Replaced setTag hack in Deserialize with @unionInit


1 files changed, 2 insertions(+), 35 deletions(-)

lib/std/io.zig+2-35
......@@ -1022,33 +1022,6 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
10221022 return @bitCast(T, result);
10231023 }
10241024
1025 //@TODO: Replace this with @unionInit or whatever when it is added
1026 // see: #1315
1027 fn setTag(ptr: var, tag: var) void {
1028 const T = @typeOf(ptr);
1029 comptime assert(trait.isPtrTo(builtin.TypeId.Union)(T));
1030 const U = meta.Child(T);
1031
1032 const info = @typeInfo(U).Union;
1033 if (info.tag_type) |TagType| {
1034 comptime assert(TagType == @typeOf(tag));
1035
1036 var ptr_tag = ptr: {
1037 if (@alignOf(TagType) >= @alignOf(U)) break :ptr @ptrCast(*TagType, ptr);
1038 const offset = comptime max: {
1039 var max_field_size: comptime_int = 0;
1040 for (info.fields) |field_info| {
1041 const field_size = @sizeOf(field_info.field_type);
1042 max_field_size = math.max(max_field_size, field_size);
1043 }
1044 break :max math.max(max_field_size, @alignOf(U));
1045 };
1046 break :ptr @intToPtr(*TagType, @ptrToInt(ptr) + offset);
1047 };
1048 ptr_tag.* = tag;
1049 }
1050 }
1051
10521025 /// Deserializes and returns data of the specified type from the stream
10531026 pub fn deserialize(self: *Self, comptime T: type) !T {
10541027 var value: T = undefined;
......@@ -1111,18 +1084,12 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
11111084 // safety. If it is bad, it will be caught anyway.
11121085 const TagInt = @TagType(TagType);
11131086 const tag = try self.deserializeInt(TagInt);
1114
1115 {
1116 @setRuntimeSafety(false);
1117 //See: #1315
1118 setTag(ptr, @intToEnum(TagType, tag));
1119 }
1120
1087
11211088 inline for (info.fields) |field_info| {
11221089 if (field_info.enum_field.?.value == tag) {
11231090 const name = field_info.name;
11241091 const FieldType = field_info.field_type;
1125 @field(ptr, name) = FieldType(undefined);
1092 ptr.* = @unionInit(C, name, undefined);
11261093 try self.deserializeInto(&@field(ptr, name));
11271094 return;
11281095 }