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

master
tgschultz 2019-02-06 04:04:38 +00:00 committed by Andrew Kelley
parent fd28b9d501
commit 20e2d8da61
2 changed files with 15 additions and 6 deletions

View File

@ -912,7 +912,7 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
} }
/// Flush any remaining bits to the stream. /// 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; if (self.bit_count == 0) return;
try self.out_stream.writeByte(self.bit_buffer); try self.out_stream.writeByte(self.bit_buffer);
self.bit_buffer = 0; 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 //@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)); comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));
const u8_bit_count = 8; 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 /// 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(); 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); const T = @typeOf(value);
comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); 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 /// 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); const T = comptime @typeOf(value);
if (comptime trait.isIndexable(T)) { if (comptime trait.isIndexable(T)) {

View File

@ -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; const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte;
assert(in.pos == if (is_packed) total_packed_bytes else total_bytes); 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" { test "Serializer/Deserializer Int" {
@ -568,4 +577,4 @@ test "Deserializer bad data" {
try testBadData(builtin.Endian.Little, false); try testBadData(builtin.Endian.Little, false);
try testBadData(builtin.Endian.Big, true); try testBadData(builtin.Endian.Big, true);
try testBadData(builtin.Endian.Little, true); try testBadData(builtin.Endian.Little, true);
} }