| author | |
| committer | |
| log | 1188da926ff4cddd5818d85d45b087f6207dfef9 |
| tree | 839e4ef303dd468f29ae7dda5a386cd46b0c971d |
| parent | 8423bd423b7a8cf32246f9d27a400a2cf14ce770 |
2 files changed, 10 insertions(+), 6 deletions(-)
std/io.zig+3-3| ... | @@ -1146,7 +1146,7 @@ pub fn Deserializer(endian: builtin.Endian, is_packed: bool, comptime Error: typ | ... | @@ -1146,7 +1146,7 @@ pub fn Deserializer(endian: builtin.Endian, is_packed: bool, comptime Error: typ |
| 1146 | const child_type_id = @typeId(C); | 1146 | const child_type_id = @typeId(C); |
| 1147 | 1147 | ||
| 1148 | //custom deserializer: fn(self: *Self, deserializer: var) !void | 1148 | //custom deserializer: fn(self: *Self, deserializer: var) !void |
| 1149 | if (comptime trait.hasFn("deserialize")(C)) return ptr.deserialize(self); | 1149 | if (comptime trait.hasFn("deserialize")(C)) return C.deserialize(ptr, self); |
| 1150 | 1150 | ||
| 1151 | if (comptime trait.isPacked(C) and !is_packed) { | 1151 | if (comptime trait.isPacked(C) and !is_packed) { |
| 1152 | var packed_deserializer = Deserializer(endian, true, Error).init(self.in_stream); | 1152 | var packed_deserializer = Deserializer(endian, true, Error).init(self.in_stream); |
| ... | @@ -1308,8 +1308,8 @@ pub fn Serializer(endian: builtin.Endian, is_packed: bool, comptime Error: type) | ... | @@ -1308,8 +1308,8 @@ pub fn Serializer(endian: builtin.Endian, is_packed: bool, comptime Error: type) |
| 1308 | return; | 1308 | return; |
| 1309 | } | 1309 | } |
| 1310 | 1310 | ||
| 1311 | //custom serializer: fn(self: *const Self, serializer: var) !void | 1311 | //custom serializer: fn(self: Self, serializer: var) !void |
| 1312 | if (comptime trait.hasFn("serialize")(T)) return value.serialize(self); | 1312 | if (comptime trait.hasFn("serialize")(T)) return T.serialize(value, self); |
| 1313 | 1313 | ||
| 1314 | if (comptime trait.isPacked(T) and !is_packed) { | 1314 | if (comptime trait.isPacked(T) and !is_packed) { |
| 1315 | var packed_serializer = Serializer(endian, true, Error).init(self.out_stream); | 1315 | var packed_serializer = Serializer(endian, true, Error).init(self.out_stream); |
std/io_test.zig+7-3| ... | @@ -416,6 +416,10 @@ test "Serializer/Deserializer Int: Inf/NaN" { | ... | @@ -416,6 +416,10 @@ test "Serializer/Deserializer Int: Inf/NaN" { |
| 416 | try testIntSerializerDeserializerInfNaN(builtin.Endian.Little, true); | 416 | try testIntSerializerDeserializerInfNaN(builtin.Endian.Little, true); |
| 417 | } | 417 | } |
| 418 | 418 | ||
| 419 | fn testAlternateSerializer(self: var, serializer: var) !void { | ||
| 420 | try serializer.serialize(self.f_f16); | ||
| 421 | } | ||
| 422 | |||
| 419 | fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packed: bool) !void { | 423 | fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packed: bool) !void { |
| 420 | const ColorType = enum(u4) { | 424 | const ColorType = enum(u4) { |
| 421 | RGB8 = 1, | 425 | RGB8 = 1, |
| ... | @@ -448,6 +452,8 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe | ... | @@ -448,6 +452,8 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe |
| 448 | f_u2: u2, | 452 | f_u2: u2, |
| 449 | }; | 453 | }; |
| 450 | 454 | ||
| 455 | |||
| 456 | |||
| 451 | //to test custom serialization | 457 | //to test custom serialization |
| 452 | const Custom = struct { | 458 | const Custom = struct { |
| 453 | f_f16: f16, | 459 | f_f16: f16, |
| ... | @@ -458,9 +464,7 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe | ... | @@ -458,9 +464,7 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe |
| 458 | self.f_unused_u32 = 47; | 464 | self.f_unused_u32 = 47; |
| 459 | } | 465 | } |
| 460 | 466 | ||
| 461 | pub fn serialize(self: *const @This(), serializer: var) !void { | 467 | pub const serialize = testAlternateSerializer; |
| 462 | try serializer.serialize(self.f_f16); | ||
| 463 | } | ||
| 464 | }; | 468 | }; |
| 465 | 469 | ||
| 466 | const MyStruct = struct { | 470 | const MyStruct = struct { |