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,...@@ -1022,33 +1022,6 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
1022 return @bitCast(T, result);1022 return @bitCast(T, result);
1023 }1023 }
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
1052 /// Deserializes and returns data of the specified type from the stream1025 /// Deserializes and returns data of the specified type from the stream
1053 pub fn deserialize(self: *Self, comptime T: type) !T {1026 pub fn deserialize(self: *Self, comptime T: type) !T {
1054 var value: T = undefined;1027 var value: T = undefined;
...@@ -1111,18 +1084,12 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -1111,18 +1084,12 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
1111 // safety. If it is bad, it will be caught anyway.1084 // safety. If it is bad, it will be caught anyway.
1112 const TagInt = @TagType(TagType);1085 const TagInt = @TagType(TagType);
1113 const tag = try self.deserializeInt(TagInt);1086 const tag = try self.deserializeInt(TagInt);
11141087
1115 {
1116 @setRuntimeSafety(false);
1117 //See: #1315
1118 setTag(ptr, @intToEnum(TagType, tag));
1119 }
1120
1121 inline for (info.fields) |field_info| {1088 inline for (info.fields) |field_info| {
1122 if (field_info.enum_field.?.value == tag) {1089 if (field_info.enum_field.?.value == tag) {
1123 const name = field_info.name;1090 const name = field_info.name;
1124 const FieldType = field_info.field_type;1091 const FieldType = field_info.field_type;
1125 @field(ptr, name) = FieldType(undefined);1092 ptr.* = @unionInit(C, name, undefined);
1126 try self.deserializeInto(&@field(ptr, name));1093 try self.deserializeInto(&@field(ptr, name));
1127 return;1094 return;
1128 }1095 }