From 20e2d8da616aa6406572e64274fec0740cb626c6 Mon Sep 17 00:00:00 2001 From: tgschultz Date: Wed, 6 Feb 2019 04:04:38 +0000 Subject: [PATCH] Fixed Serializer and BitOutStream when used with streams that have empty error sets. --- std/io.zig | 10 +++++----- std/io_test.zig | 11 ++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/std/io.zig b/std/io.zig index c8701aeda..81d90def6 100644 --- a/std/io.zig +++ b/std/io.zig @@ -912,7 +912,7 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type { } /// Flush any remaining bits to the stream. - pub fn flushBits(self: *Self) !void { + pub fn flushBits(self: *Self) Error!void { if (self.bit_count == 0) return; try self.out_stream.writeByte(self.bit_buffer); self.bit_buffer = 0; @@ -1079,7 +1079,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, is_packed: bool, comptime E } //@BUG: inferred error issue. See: #1386 - fn deserializeInt(self: *Self, comptime T: type) (Stream.Error || error{EndOfStream})!T { + fn deserializeInt(self: *Self, comptime T: type) (Error || error{EndOfStream})!T { comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); const u8_bit_count = 8; @@ -1287,11 +1287,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com } /// Flushes any unwritten bits to the stream - pub fn flush(self: *Self) Stream.Error!void { + pub fn flush(self: *Self) Error!void { if (is_packed) return self.out_stream.flushBits(); } - fn serializeInt(self: *Self, value: var) !void { + fn serializeInt(self: *Self, value: var) Error!void { const T = @typeOf(value); comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); @@ -1323,7 +1323,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime is_packed: bool, com } /// Serializes the passed value into the stream - pub fn serialize(self: *Self, value: var) !void { + pub fn serialize(self: *Self, value: var) Error!void { const T = comptime @typeOf(value); if (comptime trait.isIndexable(T)) { diff --git a/std/io_test.zig b/std/io_test.zig index 0bee0ddaf..9a0687ec6 100644 --- a/std/io_test.zig +++ b/std/io_test.zig @@ -357,6 +357,15 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte; assert(in.pos == if (is_packed) total_packed_bytes else total_bytes); + + //Verify that empty error set works with serializer. + //deserializer is covered by SliceInStream + const NullError = io.NullOutStream.Error; + var null_out = io.NullOutStream.init(); + var null_out_stream = &null_out.stream; + var null_serializer = io.Serializer(endian, is_packed, NullError).init(null_out_stream); + try null_serializer.serialize(data_mem[0..]); + try null_serializer.flush(); } test "Serializer/Deserializer Int" { @@ -568,4 +577,4 @@ test "Deserializer bad data" { try testBadData(builtin.Endian.Little, false); try testBadData(builtin.Endian.Big, true); try testBadData(builtin.Endian.Little, true); -} \ No newline at end of file +}