authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2019-04-03 20:05:24+00:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2019-04-03 20:05:24+00:00
logfe33d8ea146429af7db514621e25870508975d62
treeaa7314cff0ab16e583cc6603cd9bdeff89cdf3d6
parentba774c5697e5dcdf0f0676e2077a3034c310baf3

Changes as suggested by andrewrk


1 files changed, 5 insertions(+), 11 deletions(-)

std/io.zig+5-11
......@@ -1272,7 +1272,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
12721272 return error.InvalidEnumTag;
12731273 }
12741274 @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++
1275 " because it is an untagged union Use a custom deserialize().");
1275 " because it is an untagged union. Use a custom deserialize().");
12761276 },
12771277 builtin.TypeId.Optional => {
12781278 const OC = comptime meta.Child(C);
......@@ -1282,11 +1282,8 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
12821282 return;
12831283 }
12841284
1285 //This should ensure that the optional is set to non-null.
1286 ptr.* = OC(undefined);
1287 //The way non-pointer optionals are implemented ensures a pointer to them
1288 // will point to the value. The flag is stored at the end of that data.
1289 var val_ptr = @ptrCast(*OC, ptr);
1285 ptr.* = OC(undefined); //make it non-null so the following .? is guaranteed safe
1286 const val_ptr = &ptr.*.?;
12901287 try self.deserializeInto(val_ptr);
12911288 },
12921289 builtin.TypeId.Enum => {
......@@ -1426,7 +1423,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
14261423 unreachable;
14271424 }
14281425 @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++
1429 " because it is an untagged union Use a custom serialize().");
1426 " because it is an untagged union. Use a custom serialize().");
14301427 },
14311428 builtin.TypeId.Optional => {
14321429 if (value == null) {
......@@ -1436,10 +1433,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
14361433 try self.serializeInt(u1(@boolToInt(true)));
14371434
14381435 const OC = comptime meta.Child(T);
1439
1440 //The way non-pointer optionals are implemented ensures a pointer to them
1441 // will point to the value. The flag is stored at the end of that data.
1442 var val_ptr = @ptrCast(*const OC, &value);
1436 const val_ptr = &value.?;
14431437 try self.serialize(val_ptr.*);
14441438 },
14451439 builtin.TypeId.Enum => {