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 {...@@ -912,7 +912,7 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
912 }912 }
913913
914 /// Flush any remaining bits to the stream.914 /// Flush any remaining bits to the stream.
915 pub fn flushBits(self: *Self) !void {915 pub fn flushBits(self: *Self) Error!void {
916 if (self.bit_count == 0) return;916 if (self.bit_count == 0) return;
917 try self.out_stream.writeByte(self.bit_buffer);917 try self.out_stream.writeByte(self.bit_buffer);
918 self.bit_buffer = 0;918 self.bit_buffer = 0;
...@@ -1079,7 +1079,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, is_packed: bool, comptime E...@@ -1079,7 +1079,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, is_packed: bool, comptime E
1079 }1079 }
10801080
1081 //@BUG: inferred error issue. See: #1386 1081 //@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 {
1083 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));1083 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));
10841084
1085 const u8_bit_count = 8;1085 const u8_bit_count = 8;
...@@ -1287,11 +1287,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com...@@ -1287,11 +1287,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com
1287 }1287 }
12881288
1289 /// Flushes any unwritten bits to the stream1289 /// Flushes any unwritten bits to the stream
1290 pub fn flush(self: *Self) Stream.Error!void {1290 pub fn flush(self: *Self) Error!void {
1291 if (is_packed) return self.out_stream.flushBits();1291 if (is_packed) return self.out_stream.flushBits();
1292 }1292 }
12931293
1294 fn serializeInt(self: *Self, value: var) !void {1294 fn serializeInt(self: *Self, value: var) Error!void {
1295 const T = @typeOf(value);1295 const T = @typeOf(value);
1296 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));1296 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...@@ -1323,7 +1323,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com
1323 }1323 }
13241324
1325 /// Serializes the passed value into the stream1325 /// 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 {
1327 const T = comptime @typeOf(value);1327 const T = comptime @typeOf(value);
13281328
1329 if (comptime trait.isIndexable(T)) {1329 if (comptime trait.isIndexable(T)) {
std/io_test.zig+10-1
...@@ -357,6 +357,15 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa...@@ -357,6 +357,15 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa
357 const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte;357 const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte;
358358
359 assert(in.pos == if (is_packed) total_packed_bytes else total_bytes);359 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();
360}369}
361370
362test "Serializer/Deserializer Int" {371test "Serializer/Deserializer Int" {
...@@ -568,4 +577,4 @@ test "Deserializer bad data" {...@@ -568,4 +577,4 @@ test "Deserializer bad data" {
568 try testBadData(builtin.Endian.Little, false);577 try testBadData(builtin.Endian.Little, false);
569 try testBadData(builtin.Endian.Big, true);578 try testBadData(builtin.Endian.Big, true);
570 try testBadData(builtin.Endian.Little, true);579 try testBadData(builtin.Endian.Little, true);
571}
\ No newline at end of file
580}