authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2019-02-06 04:04:38+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-06 00:06:27-05:00
log20e2d8da616aa6406572e64274fec0740cb626c6
tree31f65e6de8175ca90c2f286583fc97b542e50aa8
parentfd28b9d5013fe27f8885e117b8d410f3be462c56

Fixed Serializer and BitOutStream when used with streams that have empty error sets.


2 files changed, 15 insertions(+), 6 deletions(-)

std/io.zig+5-5
......@@ -912,7 +912,7 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
912912 }
913913
914914 /// Flush any remaining bits to the stream.
915 pub fn flushBits(self: *Self) !void {
915 pub fn flushBits(self: *Self) Error!void {
916916 if (self.bit_count == 0) return;
917917 try self.out_stream.writeByte(self.bit_buffer);
918918 self.bit_buffer = 0;
......@@ -1079,7 +1079,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, is_packed: bool, comptime E
10791079 }
10801080
10811081 //@BUG: inferred error issue. See: #1386
1082 fn deserializeInt(self: *Self, comptime T: type) (Stream.Error || error{EndOfStream})!T {
1082 fn deserializeInt(self: *Self, comptime T: type) (Error || error{EndOfStream})!T {
10831083 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));
10841084
10851085 const u8_bit_count = 8;
......@@ -1287,11 +1287,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com
12871287 }
12881288
12891289 /// Flushes any unwritten bits to the stream
1290 pub fn flush(self: *Self) Stream.Error!void {
1290 pub fn flush(self: *Self) Error!void {
12911291 if (is_packed) return self.out_stream.flushBits();
12921292 }
12931293
1294 fn serializeInt(self: *Self, value: var) !void {
1294 fn serializeInt(self: *Self, value: var) Error!void {
12951295 const T = @typeOf(value);
12961296 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));
12971297
......@@ -1323,7 +1323,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com
13231323 }
13241324
13251325 /// Serializes the passed value into the stream
1326 pub fn serialize(self: *Self, value: var) !void {
1326 pub fn serialize(self: *Self, value: var) Error!void {
13271327 const T = comptime @typeOf(value);
13281328
13291329 if (comptime trait.isIndexable(T)) {
std/io_test.zig+10-1
......@@ -357,6 +357,15 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa
357357 const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte;
358358
359359 assert(in.pos == if (is_packed) total_packed_bytes else total_bytes);
360
361 //Verify that empty error set works with serializer.
362 //deserializer is covered by SliceInStream
363 const NullError = io.NullOutStream.Error;
364 var null_out = io.NullOutStream.init();
365 var null_out_stream = &null_out.stream;
366 var null_serializer = io.Serializer(endian, is_packed, NullError).init(null_out_stream);
367 try null_serializer.serialize(data_mem[0..]);
368 try null_serializer.flush();
360369}
361370
362371test "Serializer/Deserializer Int" {
......@@ -568,4 +577,4 @@ test "Deserializer bad data" {
568577 try testBadData(builtin.Endian.Little, false);
569578 try testBadData(builtin.Endian.Big, true);
570579 try testBadData(builtin.Endian.Little, true);
571}
\ No newline at end of file
580}