authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-09 20:15:51 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-09 20:15:51 -08:00
log9744f2b35f0d839d0bfddad241688c5b017951e8
tree43e2a1b200794f35d0696154041f8ef583422257
parent562fd5ffa90dd564f474d92285231e73213bef79

BufIndexer: don't use a fat pointer since we can verify the length is sufficient


1 files changed, 4 insertions(+), 3 deletions(-)

src/lib.zig+4-3
...@@ -387,20 +387,21 @@ pub fn indexBufferT(bytes: []const u8, comptime T: type, endian: std.builtin.End...@@ -387,20 +387,21 @@ pub fn indexBufferT(bytes: []const u8, comptime T: type, endian: std.builtin.End
387387
388pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {388pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {
389 return struct {389 return struct {
390 bytes: []const u8,390 bytes: [*]const u8,
391 max_len: usize,391 max_len: usize,
392392
393 const Self = @This();393 const Self = @This();
394394
395 pub fn init(bytes: []const u8, max_len: usize) Self {395 pub fn init(bytes: []const u8, max_len: usize) Self {
396 std.debug.assert(bytes.len >= @sizeOf(T) * max_len);
396 return .{397 return .{
397 .bytes = bytes,398 .bytes = bytes.ptr,
398 .max_len = max_len,399 .max_len = max_len,
399 };400 };
400 }401 }
401402
402 pub fn at(self: *const Self, idx: usize) T {403 pub fn at(self: *const Self, idx: usize) T {
403 return indexBufferT(self.bytes, T, endian, idx, self.max_len);404 return indexBufferT(self.bytes[0 .. @sizeOf(T) * self.max_len], T, endian, idx, self.max_len);
404 }405 }
405 };406 };
406}407}