diff options
| author | tgschultz <tgschultz@gmail.com> | 2019-04-03 20:05:24 +0000 |
|---|---|---|
| committer | tgschultz <tgschultz@gmail.com> | 2019-04-03 20:05:24 +0000 |
| commit | fe33d8ea146429af7db514621e25870508975d62 (patch) | |
| tree | aa7314cff0ab16e583cc6603cd9bdeff89cdf3d6 /std/io.zig | |
| parent | ba774c5697e5dcdf0f0676e2077a3034c310baf3 (diff) | |
| download | zig-fe33d8ea146429af7db514621e25870508975d62.tar.gz zig-fe33d8ea146429af7db514621e25870508975d62.zip | |
Changes as suggested by andrewrk
Diffstat (limited to 'std/io.zig')
| -rw-r--r-- | std/io.zig | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/std/io.zig b/std/io.zig index 7f153c37cd..afbd8198fd 100644 --- a/std/io.zig +++ b/std/io.zig @@ -1272,7 +1272,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, return error.InvalidEnumTag; } @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++ - " because it is an untagged union Use a custom deserialize()."); + " because it is an untagged union. Use a custom deserialize()."); }, builtin.TypeId.Optional => { const OC = comptime meta.Child(C); @@ -1282,11 +1282,8 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, return; } - //This should ensure that the optional is set to non-null. - ptr.* = OC(undefined); - //The way non-pointer optionals are implemented ensures a pointer to them - // will point to the value. The flag is stored at the end of that data. - var val_ptr = @ptrCast(*OC, ptr); + ptr.* = OC(undefined); //make it non-null so the following .? is guaranteed safe + const val_ptr = &ptr.*.?; try self.deserializeInto(val_ptr); }, builtin.TypeId.Enum => { @@ -1426,7 +1423,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co unreachable; } @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++ - " because it is an untagged union Use a custom serialize()."); + " because it is an untagged union. Use a custom serialize()."); }, builtin.TypeId.Optional => { if (value == null) { @@ -1436,10 +1433,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co try self.serializeInt(u1(@boolToInt(true))); const OC = comptime meta.Child(T); - - //The way non-pointer optionals are implemented ensures a pointer to them - // will point to the value. The flag is stored at the end of that data. - var val_ptr = @ptrCast(*const OC, &value); + const val_ptr = &value.?; try self.serialize(val_ptr.*); }, builtin.TypeId.Enum => { |
