| ... | @@ -8,6 +8,8 @@ const debug = std.debug; | ... | @@ -8,6 +8,8 @@ const debug = std.debug; |
| 8 | const assert = debug.assert; | 8 | const assert = debug.assert; |
| 9 | const os = std.os; | 9 | const os = std.os; |
| 10 | const mem = std.mem; | 10 | const mem = std.mem; |
| | 11 | const meta = std.meta; |
| | 12 | const trait = meta.trait; |
| 11 | const Buffer = std.Buffer; | 13 | const Buffer = std.Buffer; |
| 12 | const fmt = std.fmt; | 14 | const fmt = std.fmt; |
| 13 | const File = std.os.File; | 15 | const File = std.os.File; |
| ... | @@ -463,6 +465,153 @@ pub const SliceInStream = struct { | ... | @@ -463,6 +465,153 @@ pub const SliceInStream = struct { |
| 463 | } | 465 | } |
| 464 | }; | 466 | }; |
| 465 | | 467 | |
| | 468 | /// Creates a stream which allows for reading bit fields from another stream |
| | 469 | pub fn BitInStream(endian: builtin.Endian, comptime Error: type) type { |
| | 470 | return struct { |
| | 471 | const Self = @This(); |
| | 472 | |
| | 473 | in_stream: *Stream, |
| | 474 | bit_buffer: u7, |
| | 475 | bit_count: u3, |
| | 476 | stream: Stream, |
| | 477 | |
| | 478 | pub const Stream = InStream(Error); |
| | 479 | const u8_bit_count = comptime meta.bitCount(u8); |
| | 480 | const u7_bit_count = comptime meta.bitCount(u7); |
| | 481 | const u4_bit_count = comptime meta.bitCount(u4); |
| | 482 | |
| | 483 | pub fn init(in_stream: *Stream) Self { |
| | 484 | return Self{ |
| | 485 | .in_stream = in_stream, |
| | 486 | .bit_buffer = 0, |
| | 487 | .bit_count = 0, |
| | 488 | .stream = Stream{ .readFn = read }, |
| | 489 | }; |
| | 490 | } |
| | 491 | |
| | 492 | /// Reads `bits` bits from the stream and returns a specified unsigned int type |
| | 493 | /// containing them in the least significant end, returning an error if the |
| | 494 | /// specified number of bits could not be read. |
| | 495 | pub fn readBitsNoEof(self: *Self, comptime U: type, bits: usize) !U { |
| | 496 | var n: usize = undefined; |
| | 497 | const result = try self.readBits(U, bits, &n); |
| | 498 | if (n < bits) return error.EndOfStream; |
| | 499 | return result; |
| | 500 | } |
| | 501 | |
| | 502 | /// Reads `bits` bits from the stream and returns a specified unsigned int type |
| | 503 | /// containing them in the least significant end. The number of bits successfully |
| | 504 | /// read is placed in `out_bits`, as reaching the end of the stream is not an error. |
| | 505 | pub fn readBits(self: *Self, comptime U: type, bits: usize, out_bits: *usize) Error!U { |
| | 506 | debug.assert(trait.isUnsignedInt(U)); |
| | 507 | |
| | 508 | //by extending the buffer to a minimum of u8 we can cover a number of edge cases |
| | 509 | // related to shifting and casting. |
| | 510 | const u_bit_count = comptime meta.bitCount(U); |
| | 511 | const buf_bit_count = bc: { |
| | 512 | debug.assert(u_bit_count >= bits); |
| | 513 | break :bc if (u_bit_count <= u8_bit_count) u8_bit_count else u_bit_count; |
| | 514 | }; |
| | 515 | const Buf = @IntType(false, buf_bit_count); |
| | 516 | const BufShift = math.Log2Int(Buf); |
| | 517 | |
| | 518 | out_bits.* = usize(0); |
| | 519 | if (U == u0 or bits == 0) return 0; |
| | 520 | var out_buffer = Buf(0); |
| | 521 | |
| | 522 | if (self.bit_count > 0) { |
| | 523 | const n = if (self.bit_count >= bits) @intCast(u3, bits) else self.bit_count; |
| | 524 | const shift = u7_bit_count - n; |
| | 525 | switch (endian) { |
| | 526 | builtin.Endian.Big => { |
| | 527 | out_buffer = Buf(self.bit_buffer >> shift); |
| | 528 | self.bit_buffer <<= n; |
| | 529 | }, |
| | 530 | builtin.Endian.Little => { |
| | 531 | const value = (self.bit_buffer << shift) >> shift; |
| | 532 | out_buffer = Buf(value); |
| | 533 | self.bit_buffer >>= n; |
| | 534 | }, |
| | 535 | } |
| | 536 | self.bit_count -= n; |
| | 537 | out_bits.* = n; |
| | 538 | } |
| | 539 | //at this point we know bit_buffer is empty |
| | 540 | |
| | 541 | //copy bytes until we have enough bits, then leave the rest in bit_buffer |
| | 542 | while (out_bits.* < bits) { |
| | 543 | const n = bits - out_bits.*; |
| | 544 | const next_byte = self.in_stream.readByte() catch |err| { |
| | 545 | if (err == error.EndOfStream) { |
| | 546 | return @intCast(U, out_buffer); |
| | 547 | } |
| | 548 | //@BUG: See #1810. Not sure if the bug is that I have to do this for some |
| | 549 | // streams, or that I don't for streams with emtpy errorsets. |
| | 550 | return @errSetCast(Error, err); |
| | 551 | }; |
| | 552 | |
| | 553 | switch (endian) { |
| | 554 | builtin.Endian.Big => { |
| | 555 | if (n >= u8_bit_count) { |
| | 556 | out_buffer <<= @intCast(u3, u8_bit_count - 1); |
| | 557 | out_buffer <<= 1; |
| | 558 | out_buffer |= Buf(next_byte); |
| | 559 | out_bits.* += u8_bit_count; |
| | 560 | continue; |
| | 561 | } |
| | 562 | |
| | 563 | const shift = @intCast(u3, u8_bit_count - n); |
| | 564 | out_buffer <<= @intCast(BufShift, n); |
| | 565 | out_buffer |= Buf(next_byte >> shift); |
| | 566 | out_bits.* += n; |
| | 567 | self.bit_buffer = @truncate(u7, next_byte << @intCast(u3, n - 1)); |
| | 568 | self.bit_count = shift; |
| | 569 | }, |
| | 570 | builtin.Endian.Little => { |
| | 571 | if (n >= u8_bit_count) { |
| | 572 | out_buffer |= Buf(next_byte) << @intCast(BufShift, out_bits.*); |
| | 573 | out_bits.* += u8_bit_count; |
| | 574 | continue; |
| | 575 | } |
| | 576 | |
| | 577 | const shift = @intCast(u3, u8_bit_count - n); |
| | 578 | const value = (next_byte << shift) >> shift; |
| | 579 | out_buffer |= Buf(value) << @intCast(BufShift, out_bits.*); |
| | 580 | out_bits.* += n; |
| | 581 | self.bit_buffer = @truncate(u7, next_byte >> @intCast(u3, n)); |
| | 582 | self.bit_count = shift; |
| | 583 | }, |
| | 584 | } |
| | 585 | } |
| | 586 | |
| | 587 | return @intCast(U, out_buffer); |
| | 588 | } |
| | 589 | |
| | 590 | pub fn alignToByte(self: *Self) void { |
| | 591 | self.bit_buffer = 0; |
| | 592 | self.bit_count = 0; |
| | 593 | } |
| | 594 | |
| | 595 | pub fn read(self_stream: *Stream, buffer: []u8) Error!usize { |
| | 596 | var self = @fieldParentPtr(Self, "stream", self_stream); |
| | 597 | |
| | 598 | var out_bits: usize = undefined; |
| | 599 | var out_bits_total = usize(0); |
| | 600 | //@NOTE: I'm not sure this is a good idea, maybe alignToByte should be forced |
| | 601 | if (self.bit_count > 0) { |
| | 602 | for (buffer) |*b, i| { |
| | 603 | b.* = try self.readBits(u8, u8_bit_count, &out_bits); |
| | 604 | out_bits_total += out_bits; |
| | 605 | } |
| | 606 | const incomplete_byte = @boolToInt(out_bits_total % u8_bit_count > 0); |
| | 607 | return (out_bits_total / u8_bit_count) + incomplete_byte; |
| | 608 | } |
| | 609 | |
| | 610 | return self.in_stream.read(buffer); |
| | 611 | } |
| | 612 | }; |
| | 613 | } |
| | 614 | |
| 466 | /// This is a simple OutStream that writes to a slice, and returns an error | 615 | /// This is a simple OutStream that writes to a slice, and returns an error |
| 467 | /// when it runs out of space. | 616 | /// when it runs out of space. |
| 468 | pub const SliceOutStream = struct { | 617 | pub const SliceOutStream = struct { |
| ... | @@ -656,6 +805,137 @@ pub const BufferOutStream = struct { | ... | @@ -656,6 +805,137 @@ pub const BufferOutStream = struct { |
| 656 | } | 805 | } |
| 657 | }; | 806 | }; |
| 658 | | 807 | |
| | 808 | /// Creates a stream which allows for writing bit fields to another stream |
| | 809 | pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type { |
| | 810 | return struct { |
| | 811 | const Self = @This(); |
| | 812 | |
| | 813 | out_stream: *Stream, |
| | 814 | bit_buffer: u8, |
| | 815 | bit_count: u4, |
| | 816 | stream: Stream, |
| | 817 | |
| | 818 | pub const Stream = OutStream(Error); |
| | 819 | const u8_bit_count = comptime meta.bitCount(u8); |
| | 820 | const u4_bit_count = comptime meta.bitCount(u4); |
| | 821 | |
| | 822 | pub fn init(out_stream: *Stream) Self { |
| | 823 | return Self{ |
| | 824 | .out_stream = out_stream, |
| | 825 | .bit_buffer = 0, |
| | 826 | .bit_count = 0, |
| | 827 | .stream = Stream{ .writeFn = write }, |
| | 828 | }; |
| | 829 | } |
| | 830 | |
| | 831 | /// Write the specified number of bits to the stream from the least significant bits of |
| | 832 | /// the specified unsigned int value. Bits will only be written to the stream when there |
| | 833 | /// are enough to fill a byte. |
| | 834 | pub fn writeBits(self: *Self, value: var, bits: usize) Error!void { |
| | 835 | if (bits == 0) return; |
| | 836 | |
| | 837 | const U = @typeOf(value); |
| | 838 | debug.assert(trait.isUnsignedInt(U)); |
| | 839 | |
| | 840 | //by extending the buffer to a minimum of u8 we can cover a number of edge cases |
| | 841 | // related to shifting and casting. |
| | 842 | const u_bit_count = comptime meta.bitCount(U); |
| | 843 | const buf_bit_count = bc: { |
| | 844 | debug.assert(u_bit_count >= bits); |
| | 845 | break :bc if (u_bit_count <= u8_bit_count) u8_bit_count else u_bit_count; |
| | 846 | }; |
| | 847 | const Buf = @IntType(false, buf_bit_count); |
| | 848 | const BufShift = math.Log2Int(Buf); |
| | 849 | |
| | 850 | const buf_value = @intCast(Buf, value); |
| | 851 | |
| | 852 | const high_byte_shift = @intCast(BufShift, buf_bit_count - u8_bit_count); |
| | 853 | var in_buffer = switch (endian) { |
| | 854 | builtin.Endian.Big => buf_value << @intCast(BufShift, buf_bit_count - bits), |
| | 855 | builtin.Endian.Little => buf_value, |
| | 856 | }; |
| | 857 | var in_bits = bits; |
| | 858 | |
| | 859 | if (self.bit_count > 0) { |
| | 860 | const bits_remaining = u8_bit_count - self.bit_count; |
| | 861 | const n = @intCast(u3, if (bits_remaining > bits) bits else bits_remaining); |
| | 862 | switch (endian) { |
| | 863 | builtin.Endian.Big => { |
| | 864 | const shift = @intCast(BufShift, high_byte_shift + self.bit_count); |
| | 865 | const v = @intCast(u8, in_buffer >> shift); |
| | 866 | self.bit_buffer |= v; |
| | 867 | in_buffer <<= n; |
| | 868 | }, |
| | 869 | builtin.Endian.Little => { |
| | 870 | const v = @truncate(u8, in_buffer) << @intCast(u3, self.bit_count); |
| | 871 | self.bit_buffer |= v; |
| | 872 | in_buffer >>= n; |
| | 873 | }, |
| | 874 | } |
| | 875 | self.bit_count += n; |
| | 876 | in_bits -= n; |
| | 877 | |
| | 878 | //if we didn't fill the buffer, it's because bits < bits_remaining; |
| | 879 | if (self.bit_count != u8_bit_count) return; |
| | 880 | try self.out_stream.writeByte(self.bit_buffer); |
| | 881 | self.bit_buffer = 0; |
| | 882 | self.bit_count = 0; |
| | 883 | } |
| | 884 | //at this point we know bit_buffer is empty |
| | 885 | |
| | 886 | //copy bytes until we can't fill one anymore, then leave the rest in bit_buffer |
| | 887 | while (in_bits >= u8_bit_count) { |
| | 888 | switch (endian) { |
| | 889 | builtin.Endian.Big => { |
| | 890 | const v = @intCast(u8, in_buffer >> high_byte_shift); |
| | 891 | try self.out_stream.writeByte(v); |
| | 892 | in_buffer <<= @intCast(u3, u8_bit_count - 1); |
| | 893 | in_buffer <<= 1; |
| | 894 | }, |
| | 895 | builtin.Endian.Little => { |
| | 896 | const v = @truncate(u8, in_buffer); |
| | 897 | try self.out_stream.writeByte(v); |
| | 898 | in_buffer >>= @intCast(u3, u8_bit_count - 1); |
| | 899 | in_buffer >>= 1; |
| | 900 | }, |
| | 901 | } |
| | 902 | in_bits -= u8_bit_count; |
| | 903 | } |
| | 904 | |
| | 905 | if (in_bits > 0) { |
| | 906 | self.bit_count = @intCast(u4, in_bits); |
| | 907 | self.bit_buffer = switch (endian) { |
| | 908 | builtin.Endian.Big => @truncate(u8, in_buffer >> high_byte_shift), |
| | 909 | builtin.Endian.Little => @truncate(u8, in_buffer), |
| | 910 | }; |
| | 911 | } |
| | 912 | } |
| | 913 | |
| | 914 | /// Flush any remaining bits to the stream. |
| | 915 | pub fn flushBits(self: *Self) !void { |
| | 916 | if (self.bit_count == 0) return; |
| | 917 | try self.out_stream.writeByte(self.bit_buffer); |
| | 918 | self.bit_buffer = 0; |
| | 919 | self.bit_count = 0; |
| | 920 | } |
| | 921 | |
| | 922 | pub fn write(self_stream: *Stream, buffer: []const u8) Error!void { |
| | 923 | var self = @fieldParentPtr(Self, "stream", self_stream); |
| | 924 | |
| | 925 | //@NOTE: I'm not sure this is a good idea, maybe flushBits should be forced |
| | 926 | if (self.bit_count > 0) { |
| | 927 | for (buffer) |b, i| |
| | 928 | try self.writeBits(b, u8_bit_count); |
| | 929 | return; |
| | 930 | } |
| | 931 | |
| | 932 | return self.out_stream.write(buffer); |
| | 933 | } |
| | 934 | }; |
| | 935 | } |
| | 936 | |
| | 937 | |
| | 938 | |
| 659 | pub const BufferedAtomicFile = struct { | 939 | pub const BufferedAtomicFile = struct { |
| 660 | atomic_file: os.AtomicFile, | 940 | atomic_file: os.AtomicFile, |
| 661 | file_stream: os.File.OutStream, | 941 | file_stream: os.File.OutStream, |
| ... | @@ -696,11 +976,6 @@ pub const BufferedAtomicFile = struct { | ... | @@ -696,11 +976,6 @@ pub const BufferedAtomicFile = struct { |
| 696 | } | 976 | } |
| 697 | }; | 977 | }; |
| 698 | | 978 | |
| 699 | test "import io tests" { | | |
| 700 | comptime { | | |
| 701 | _ = @import("io_test.zig"); | | |
| 702 | } | | |
| 703 | } | | |
| 704 | | 979 | |
| 705 | pub fn readLine(buf: *std.Buffer) ![]u8 { | 980 | pub fn readLine(buf: *std.Buffer) ![]u8 { |
| 706 | var stdin = try getStdIn(); | 981 | var stdin = try getStdIn(); |
| ... | @@ -772,3 +1047,364 @@ test "io.readLineSliceFrom" { | ... | @@ -772,3 +1047,364 @@ test "io.readLineSliceFrom" { |
| 772 | debug.assert(mem.eql(u8, "Line 1", try readLineSliceFrom(stream, buf[0..]))); | 1047 | debug.assert(mem.eql(u8, "Line 1", try readLineSliceFrom(stream, buf[0..]))); |
| 773 | debug.assertError(readLineSliceFrom(stream, buf[0..]), error.OutOfMemory); | 1048 | debug.assertError(readLineSliceFrom(stream, buf[0..]), error.OutOfMemory); |
| 774 | } | 1049 | } |
| | 1050 | |
| | 1051 | /// Creates a deserializer that deserializes types from any stream. |
| | 1052 | /// If `is_packed` is true, the data stream is treated as bit-packed, |
| | 1053 | /// otherwise data is expected to be packed to the smallest byte. |
| | 1054 | /// Types may implement a custom deserialization routine with a |
| | 1055 | /// function named `deserialize` in the form of: |
| | 1056 | /// pub fn deserialize(self: *Self, deserializer: var) !void |
| | 1057 | /// which will be called when the deserializer is used to deserialize |
| | 1058 | /// that type. It will pass a pointer to the type instance to deserialize |
| | 1059 | /// into and a pointer to the deserializer struct. |
| | 1060 | pub fn Deserializer(endian: builtin.Endian, is_packed: bool, comptime Error: type) type { |
| | 1061 | return struct { |
| | 1062 | const Self = @This(); |
| | 1063 | |
| | 1064 | in_stream: if (is_packed) BitInStream(endian, Stream.Error) else *Stream, |
| | 1065 | |
| | 1066 | pub const Stream = InStream(Error); |
| | 1067 | |
| | 1068 | pub fn init(in_stream: *Stream) Self { |
| | 1069 | return Self{ .in_stream = switch (is_packed) { |
| | 1070 | true => BitInStream(endian, Stream.Error).init(in_stream), |
| | 1071 | else => in_stream, |
| | 1072 | } }; |
| | 1073 | } |
| | 1074 | |
| | 1075 | pub fn alignToByte(self: *Self) void { |
| | 1076 | if(!is_packed) return; |
| | 1077 | self.in_stream.alignToByte(); |
| | 1078 | } |
| | 1079 | |
| | 1080 | //@BUG: inferred error issue. See: #1386 |
| | 1081 | fn deserializeInt(self: *Self, comptime T: type) (Stream.Error || error{EndOfStream})!T { |
| | 1082 | debug.assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); |
| | 1083 | |
| | 1084 | const u8_bit_count = comptime meta.bitCount(u8); |
| | 1085 | const t_bit_count = comptime meta.bitCount(T); |
| | 1086 | |
| | 1087 | const U = @IntType(false, t_bit_count); |
| | 1088 | const Log2U = math.Log2Int(U); |
| | 1089 | const int_size = @sizeOf(U); |
| | 1090 | |
| | 1091 | if (is_packed) { |
| | 1092 | const result = try self.in_stream.readBitsNoEof(U, t_bit_count); |
| | 1093 | return @bitCast(T, result); |
| | 1094 | } |
| | 1095 | |
| | 1096 | var buffer: [int_size]u8 = undefined; |
| | 1097 | const read_size = try self.in_stream.read(buffer[0..]); |
| | 1098 | if (read_size < int_size) return error.EndOfStream; |
| | 1099 | |
| | 1100 | if (int_size == 1) return @bitCast(T, buffer[0]); |
| | 1101 | |
| | 1102 | var result = U(0); |
| | 1103 | for (buffer) |byte, i| { |
| | 1104 | switch (endian) { |
| | 1105 | builtin.Endian.Big => { |
| | 1106 | result = (result << @intCast(u4, u8_bit_count)) | byte; |
| | 1107 | }, |
| | 1108 | builtin.Endian.Little => { |
| | 1109 | result |= U(byte) << @intCast(Log2U, u8_bit_count * i); |
| | 1110 | }, |
| | 1111 | } |
| | 1112 | } |
| | 1113 | |
| | 1114 | return @bitCast(T, result); |
| | 1115 | } |
| | 1116 | |
| | 1117 | //@TODO: Replace this with @unionInit or whatever when it is added |
| | 1118 | // see: #1315 |
| | 1119 | fn setTag(ptr: var, tag: var) void { |
| | 1120 | const T = @typeOf(ptr); |
| | 1121 | comptime debug.assert(trait.isPtrTo(builtin.TypeId.Union)(T)); |
| | 1122 | const U = meta.Child(T); |
| | 1123 | |
| | 1124 | const info = @typeInfo(U).Union; |
| | 1125 | if (info.tag_type) |TagType| { |
| | 1126 | debug.assert(TagType == @typeOf(tag)); |
| | 1127 | |
| | 1128 | var ptr_tag = ptr: { |
| | 1129 | if (@alignOf(TagType) >= @alignOf(U)) break :ptr @ptrCast(*TagType, ptr); |
| | 1130 | const offset = comptime max: { |
| | 1131 | var max_field_size: comptime_int = 0; |
| | 1132 | for (info.fields) |field_info| { |
| | 1133 | const field_size = @sizeOf(field_info.field_type); |
| | 1134 | max_field_size = math.max(max_field_size, field_size); |
| | 1135 | } |
| | 1136 | break :max math.max(max_field_size, @alignOf(U)); |
| | 1137 | }; |
| | 1138 | break :ptr @intToPtr(*TagType, @ptrToInt(ptr) + offset); |
| | 1139 | }; |
| | 1140 | ptr_tag.* = tag; |
| | 1141 | } |
| | 1142 | } |
| | 1143 | |
| | 1144 | /// Deserializes and returns data of the specified type from the stream |
| | 1145 | pub fn deserialize(self: *Self, comptime T: type) !T { |
| | 1146 | var value: T = undefined; |
| | 1147 | try self.deserializeInto(&value); |
| | 1148 | return value; |
| | 1149 | } |
| | 1150 | |
| | 1151 | /// Deserializes data into the type pointed to by `ptr` |
| | 1152 | pub fn deserializeInto(self: *Self, ptr: var) !void { |
| | 1153 | const T = @typeOf(ptr); |
| | 1154 | debug.assert(trait.is(builtin.TypeId.Pointer)(T)); |
| | 1155 | |
| | 1156 | if (comptime trait.isSlice(T) or comptime trait.isPtrTo(builtin.TypeId.Array)(T)) { |
| | 1157 | for (ptr) |*v| |
| | 1158 | try self.deserializeInto(v); |
| | 1159 | return; |
| | 1160 | } |
| | 1161 | |
| | 1162 | comptime debug.assert(trait.isSingleItemPtr(T)); |
| | 1163 | |
| | 1164 | const C = comptime meta.Child(T); |
| | 1165 | const child_type_id = @typeId(C); |
| | 1166 | |
| | 1167 | //custom deserializer: fn(self: *Self, deserializer: var) !void |
| | 1168 | if (comptime trait.hasFn("deserialize")(C)) return C.deserialize(ptr, self); |
| | 1169 | |
| | 1170 | if (comptime trait.isPacked(C) and !is_packed) { |
| | 1171 | var packed_deserializer = Deserializer(endian, true, Error).init(self.in_stream); |
| | 1172 | return packed_deserializer.deserializeInto(ptr); |
| | 1173 | } |
| | 1174 | |
| | 1175 | switch (child_type_id) { |
| | 1176 | builtin.TypeId.Void => return, |
| | 1177 | builtin.TypeId.Bool => ptr.* = (try self.deserializeInt(u1)) > 0, |
| | 1178 | builtin.TypeId.Float, builtin.TypeId.Int => ptr.* = try self.deserializeInt(C), |
| | 1179 | builtin.TypeId.Struct => { |
| | 1180 | const info = @typeInfo(C).Struct; |
| | 1181 | |
| | 1182 | inline for (info.fields) |*field_info| { |
| | 1183 | const name = field_info.name; |
| | 1184 | const FieldType = field_info.field_type; |
| | 1185 | |
| | 1186 | if (FieldType == void or FieldType == u0) continue; |
| | 1187 | |
| | 1188 | //it doesn't make any sense to read pointers |
| | 1189 | if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) { |
| | 1190 | @compileError("Will not " ++ "read field " ++ name ++ " of struct " ++ |
| | 1191 | @typeName(C) ++ " because it " ++ "is of pointer-type " ++ |
| | 1192 | @typeName(FieldType) ++ "."); |
| | 1193 | } |
| | 1194 | |
| | 1195 | try self.deserializeInto(&@field(ptr, name)); |
| | 1196 | } |
| | 1197 | }, |
| | 1198 | builtin.TypeId.Union => { |
| | 1199 | const info = @typeInfo(C).Union; |
| | 1200 | if (info.tag_type) |TagType| { |
| | 1201 | //we avoid duplicate iteration over the enum tags |
| | 1202 | // by getting the int directly and casting it without |
| | 1203 | // safety. If it is bad, it will be caught anyway. |
| | 1204 | const TagInt = @TagType(TagType); |
| | 1205 | const tag = try self.deserializeInt(TagInt); |
| | 1206 | |
| | 1207 | { |
| | 1208 | @setRuntimeSafety(false); |
| | 1209 | //See: #1315 |
| | 1210 | setTag(ptr, @intToEnum(TagType, tag)); |
| | 1211 | } |
| | 1212 | |
| | 1213 | inline for (info.fields) |field_info| { |
| | 1214 | if (field_info.enum_field.?.value == tag) { |
| | 1215 | const name = field_info.name; |
| | 1216 | const FieldType = field_info.field_type; |
| | 1217 | @field(ptr, name) = FieldType(undefined); |
| | 1218 | try self.deserializeInto(&@field(ptr, name)); |
| | 1219 | return; |
| | 1220 | } |
| | 1221 | } |
| | 1222 | //This is reachable if the enum data is bad |
| | 1223 | return error.InvalidEnumTag; |
| | 1224 | } |
| | 1225 | @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++ |
| | 1226 | " because it is an untagged union Use a custom deserialize()."); |
| | 1227 | }, |
| | 1228 | builtin.TypeId.Optional => { |
| | 1229 | const OC = comptime meta.Child(C); |
| | 1230 | const exists = (try self.deserializeInt(u1)) > 0; |
| | 1231 | if (!exists) { |
| | 1232 | ptr.* = null; |
| | 1233 | return; |
| | 1234 | } |
| | 1235 | |
| | 1236 | //The way non-pointer optionals are implemented ensures a pointer to them |
| | 1237 | // will point to the value. The flag is stored at the end of that data. |
| | 1238 | var val_ptr = @ptrCast(*OC, ptr); |
| | 1239 | try self.deserializeInto(val_ptr); |
| | 1240 | //This bit ensures the null flag isn't set. Any actual copying should be |
| | 1241 | // optimized out... I hope. |
| | 1242 | ptr.* = val_ptr.*; |
| | 1243 | }, |
| | 1244 | builtin.TypeId.Enum => { |
| | 1245 | var value = try self.deserializeInt(@TagType(C)); |
| | 1246 | ptr.* = try meta.intToEnum(C, value); |
| | 1247 | }, |
| | 1248 | else => { |
| | 1249 | @compileError("Cannot deserialize " ++ @tagName(child_type_id) ++ " types (unimplemented)."); |
| | 1250 | }, |
| | 1251 | } |
| | 1252 | } |
| | 1253 | }; |
| | 1254 | } |
| | 1255 | |
| | 1256 | /// Creates a serializer that serializes types to any stream. |
| | 1257 | /// If `is_packed` is true, the data will be bit-packed into the stream. |
| | 1258 | /// Note that the you must call `serializer.flush()` when you are done |
| | 1259 | /// writing bit-packed data in order ensure any unwritten bits are committed. |
| | 1260 | /// If `is_packed` is false, data is packed to the smallest byte. In the case |
| | 1261 | /// of packed structs, the struct will written bit-packed and with the specified |
| | 1262 | /// endianess, after which data will resume being written at the next byte boundary. |
| | 1263 | /// Types may implement a custom serialization routine with a |
| | 1264 | /// function named `serialize` in the form of: |
| | 1265 | /// pub fn serialize(self: Self, serializer: var) !void |
| | 1266 | /// which will be called when the serializer is used to serialize that type. It will |
| | 1267 | /// pass a const pointer to the type instance to be serialized and a pointer |
| | 1268 | /// to the serializer struct. |
| | 1269 | pub fn Serializer(endian: builtin.Endian, is_packed: bool, comptime Error: type) type { |
| | 1270 | return struct { |
| | 1271 | const Self = @This(); |
| | 1272 | |
| | 1273 | out_stream: if (is_packed) BitOutStream(endian, Stream.Error) else *Stream, |
| | 1274 | |
| | 1275 | pub const Stream = OutStream(Error); |
| | 1276 | |
| | 1277 | pub fn init(out_stream: *Stream) Self { |
| | 1278 | return Self{ .out_stream = switch (is_packed) { |
| | 1279 | true => BitOutStream(endian, Stream.Error).init(out_stream), |
| | 1280 | else => out_stream, |
| | 1281 | } }; |
| | 1282 | } |
| | 1283 | |
| | 1284 | /// Flushes any unwritten bits to the stream |
| | 1285 | pub fn flush(self: *Self) Stream.Error!void { |
| | 1286 | if (is_packed) return self.out_stream.flushBits(); |
| | 1287 | } |
| | 1288 | |
| | 1289 | fn serializeInt(self: *Self, value: var) !void { |
| | 1290 | const T = @typeOf(value); |
| | 1291 | debug.assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); |
| | 1292 | |
| | 1293 | const t_bit_count = comptime meta.bitCount(T); |
| | 1294 | const u8_bit_count = comptime meta.bitCount(u8); |
| | 1295 | |
| | 1296 | const U = @IntType(false, t_bit_count); |
| | 1297 | const Log2U = math.Log2Int(U); |
| | 1298 | const int_size = @sizeOf(U); |
| | 1299 | |
| | 1300 | const u_value = @bitCast(U, value); |
| | 1301 | |
| | 1302 | if (is_packed) return self.out_stream.writeBits(u_value, t_bit_count); |
| | 1303 | |
| | 1304 | var buffer: [int_size]u8 = undefined; |
| | 1305 | if (int_size == 1) buffer[0] = u_value; |
| | 1306 | |
| | 1307 | for (buffer) |*byte, i| { |
| | 1308 | const idx = switch (endian) { |
| | 1309 | builtin.Endian.Big => int_size - i - 1, |
| | 1310 | builtin.Endian.Little => i, |
| | 1311 | }; |
| | 1312 | const shift = @intCast(Log2U, idx * u8_bit_count); |
| | 1313 | const v = u_value >> shift; |
| | 1314 | byte.* = if (t_bit_count < u8_bit_count) v else @truncate(u8, v); |
| | 1315 | } |
| | 1316 | |
| | 1317 | try self.out_stream.write(buffer); |
| | 1318 | } |
| | 1319 | |
| | 1320 | /// Serializes the passed value into the stream |
| | 1321 | pub fn serialize(self: *Self, value: var) !void { |
| | 1322 | const T = comptime @typeOf(value); |
| | 1323 | |
| | 1324 | if (comptime trait.isIndexable(T)) { |
| | 1325 | for (value) |v| |
| | 1326 | try self.serialize(v); |
| | 1327 | return; |
| | 1328 | } |
| | 1329 | |
| | 1330 | //custom serializer: fn(self: Self, serializer: var) !void |
| | 1331 | if (comptime trait.hasFn("serialize")(T)) return T.serialize(value, self); |
| | 1332 | |
| | 1333 | if (comptime trait.isPacked(T) and !is_packed) { |
| | 1334 | var packed_serializer = Serializer(endian, true, Error).init(self.out_stream); |
| | 1335 | try packed_serializer.serialize(value); |
| | 1336 | try packed_serializer.flush(); |
| | 1337 | return; |
| | 1338 | } |
| | 1339 | |
| | 1340 | switch (@typeId(T)) { |
| | 1341 | builtin.TypeId.Void => return, |
| | 1342 | builtin.TypeId.Bool => try self.serializeInt(u1(@boolToInt(value))), |
| | 1343 | builtin.TypeId.Float, builtin.TypeId.Int => try self.serializeInt(value), |
| | 1344 | builtin.TypeId.Struct => { |
| | 1345 | const info = @typeInfo(T); |
| | 1346 | |
| | 1347 | inline for (info.Struct.fields) |*field_info| { |
| | 1348 | const name = field_info.name; |
| | 1349 | const FieldType = field_info.field_type; |
| | 1350 | |
| | 1351 | if (FieldType == void or FieldType == u0) continue; |
| | 1352 | |
| | 1353 | //It doesn't make sense to write pointers |
| | 1354 | if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) { |
| | 1355 | @compileError("Will not " ++ "serialize field " ++ name ++ |
| | 1356 | " of struct " ++ @typeName(T) ++ " because it " ++ |
| | 1357 | "is of pointer-type " ++ @typeName(FieldType) ++ "."); |
| | 1358 | } |
| | 1359 | try self.serialize(@field(value, name)); |
| | 1360 | } |
| | 1361 | }, |
| | 1362 | builtin.TypeId.Union => { |
| | 1363 | const info = @typeInfo(T).Union; |
| | 1364 | if (info.tag_type) |TagType| { |
| | 1365 | const active_tag = meta.activeTag(value); |
| | 1366 | try self.serialize(active_tag); |
| | 1367 | //This inline loop is necessary because active_tag is a runtime |
| | 1368 | // value, but @field requires a comptime value. Our alternative |
| | 1369 | // is to check each field for a match |
| | 1370 | inline for (info.fields) |field_info| { |
| | 1371 | if (field_info.enum_field.?.value == @enumToInt(active_tag)) { |
| | 1372 | const name = field_info.name; |
| | 1373 | const FieldType = field_info.field_type; |
| | 1374 | try self.serialize(@field(value, name)); |
| | 1375 | return; |
| | 1376 | } |
| | 1377 | } |
| | 1378 | unreachable; |
| | 1379 | } |
| | 1380 | @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++ |
| | 1381 | " because it is an untagged union Use a custom serialize()."); |
| | 1382 | }, |
| | 1383 | builtin.TypeId.Optional => { |
| | 1384 | if (value == null) { |
| | 1385 | try self.serializeInt(u1(@boolToInt(false))); |
| | 1386 | return; |
| | 1387 | } |
| | 1388 | try self.serializeInt(u1(@boolToInt(true))); |
| | 1389 | |
| | 1390 | const OC = comptime meta.Child(T); |
| | 1391 | |
| | 1392 | //The way non-pointer optionals are implemented ensures a pointer to them |
| | 1393 | // will point to the value. The flag is stored at the end of that data. |
| | 1394 | var val_ptr = @ptrCast(*const OC, &value); |
| | 1395 | try self.serialize(val_ptr.*); |
| | 1396 | }, |
| | 1397 | builtin.TypeId.Enum => { |
| | 1398 | try self.serializeInt(@enumToInt(value)); |
| | 1399 | }, |
| | 1400 | else => @compileError("Cannot serialize " ++ @tagName(@typeId(T)) ++ " types (unimplemented)."), |
| | 1401 | } |
| | 1402 | } |
| | 1403 | }; |
| | 1404 | } |
| | 1405 | |
| | 1406 | test "import io tests" { |
| | 1407 | comptime { |
| | 1408 | _ = @import("io_test.zig"); |
| | 1409 | } |
| | 1410 | } |