takeStruct
Asserts the buffer was initialized with a capacity at least @sizeOf(T).
This function is inline to avoid referencing std.mem.byteSwapAllFields
when endian is comptime-known and matches the host endianness.
See also:
takeStructPointerpeekStruct
Function parameters
Parameters
- r:*Reader
- T:type
- endian:std.builtin.Endian
Type definitions in this namespace
Types
Functions in this namespace
Functions
- fixed
- Constructs a `Reader` such that it will read from `buffer` and then end.
- streamExact
- "Pump" exactly `n` bytes from the reader to the writer.
- streamExact64
- "Pump" exactly `n` bytes from the reader to the writer.
- streamExactPreserve
- "Pump" exactly `n` bytes from the reader to the writer.
- streamRemaining
- "Pump" data from the reader to the writer, handling `error.EndOfStream` as
- discardRemaining
- Consumes the stream until the end, ignoring all the data, returning the
- allocRemaining
- Transfers all bytes from the current position to the end of the stream, up
- appendRemaining
- Transfers all bytes from the current position to the end of the stream, up
- readVec
- Writes bytes from the internally tracked stream position to `data`.
- defaultReadVec
- Writes to `Reader.buffer` or `data`, whichever has larger capacity.
- peek
- Returns the next `len` bytes from the stream, filling the buffer as
- peekGreedy
- Returns all the next buffered bytes, after filling the buffer to ensure it
- toss
- Skips the next `n` bytes from the stream, advancing the seek position.
- tossBuffered
- Equivalent to `toss(r.bufferedLen())`.
- take
- Equivalent to `peek` followed by `toss`.
- takeArray
- Returns the next `n` bytes from the stream as an array, filling the buffer
- peekArray
- Returns the next `n` bytes from the stream as an array, filling the buffer
- discardAll
- Skips the next `n` bytes from the stream, advancing the seek position.
- discardShort
- Skips the next `n` bytes from the stream, advancing the seek position.
- readSliceAll
- Fill `buffer` with the next `buffer.len` bytes from the stream, advancing
- readSliceShort
- Fill `buffer` with the next `buffer.len` bytes from the stream, advancing
- readSliceEndian
- Fill `buffer` with the next `buffer.len` bytes from the stream, advancing
- readSliceEndianAlloc
- The function is inline to avoid the dead code in case `endian` is
- readAlloc
- Shortcut for calling `readSliceAll` with a buffer provided by `allocator`.
- takeSentinel
- Returns a slice of the next bytes of buffered data from the stream until
- peekSentinel
- Returns a slice of the next bytes of buffered data from the stream until
- takeDelimiterInclusive
- Returns a slice of the next bytes of buffered data from the stream until
- peekDelimiterInclusive
- Returns a slice of the next bytes of buffered data from the stream until
- takeDelimiterExclusive
- Returns a slice of the next bytes of buffered data from the stream until
- takeDelimiter
- Returns a slice of the next bytes of buffered data from the stream until
- peekDelimiterExclusive
- Returns a slice of the next bytes of buffered data from the stream until
- streamDelimiter
- Appends to `w` contents by reading from the stream until `delimiter` is
- streamDelimiterEnding
- Appends to `w` contents by reading from the stream until `delimiter` is found.
- streamDelimiterLimit
- Appends to `w` contents by reading from the stream until `delimiter` is found.
- discardDelimiterInclusive
- Reads from the stream until specified byte is found, discarding all data,
- discardDelimiterExclusive
- Reads from the stream until specified byte is found, discarding all data,
- discardDelimiterLimit
- Reads from the stream until specified byte is found, discarding all data,
- fill
- Fills the buffer such that it contains at least `n` bytes, without
- fillMore
- Without advancing the seek position, does exactly one underlying read, filling the buffer as
- peekByte
- Returns the next byte from the stream or returns `error.EndOfStream`.
- takeByte
- Reads 1 byte from the stream or returns `error.EndOfStream`.
- takeByteSigned
- Same as `takeByte` except the returned byte is signed.
- takeInt
- Asserts the buffer was initialized with a capacity at least `@bitSizeOf(T) / 8`.
- peekInt
- Asserts the buffer was initialized with a capacity at least `@bitSizeOf(T) / 8`.
- takeVarInt
- Asserts the buffer was initialized with a capacity at least `n`.
- takeStructPointer
- Obtains an unaligned pointer to the beginning of the stream, reinterpreted
- peekStructPointer
- Obtains an unaligned pointer to the beginning of the stream, reinterpreted
- takeStruct
- Asserts the buffer was initialized with a capacity at least `@sizeOf(T)`.
- peekStruct
- Asserts the buffer was initialized with a capacity at least `@sizeOf(T)`.
- takeEnum
- Reads an integer with the same size as the given enum's tag type.
- takeEnumNonexhaustive
- Reads an integer with the same size as the given nonexhaustive enum's tag type.
- takeLeb128
- Read a single LEB128 value as type T, or `error.Overflow` if the value cannot fit.
- rebase
- Ensures `capacity` data can be buffered without rebasing.
- Hashed
- Provides a `Reader` implementation by passing data from an underlying
Error sets in this namespace
Error Sets
= .{ .vtable = &.{ .stream = failingStream, .discard = failingDiscard, }, .buffer = &.{}, .seek = 0, .end = 0, }
Values
Source
Implementation
pub inline fn takeStruct(r: *Reader, comptime T: type, endian: std.builtin.Endian) Error!T {
switch (@typeInfo(T)) {
.@"struct" => |info| switch (info.layout) {
.auto => @compileError("ill-defined memory layout"),
.@"extern" => {
var res = (try r.takeStructPointer(T)).*;
if (native_endian != endian) std.mem.byteSwapAllFields(T, &res);
return res;
},
.@"packed" => {
return @bitCast(try takeInt(r, info.backing_integer.?, endian));
},
},
else => @compileError("not a struct"),
}
}