authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-03-11 23:05:46+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-12 00:17:35+01:00
log482424e2b12cfcfe71280c826a2d31cb5df13a1a
tree41a28f6fc82dee2ce01084e130001ed4b24e9448
parent4fc6f631e044b5ddfff6c610f04f3619a1bbeb8d

std: Handle empty MultiArrayList in items()

Closes #8211

1 files changed, 6 insertions(+), 1 deletions(-)

lib/std/multi_array_list.zig+6-1
...@@ -28,8 +28,11 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -28,8 +28,11 @@ pub fn MultiArrayList(comptime S: type) type {
28 capacity: usize,28 capacity: usize,
2929
30 pub fn items(self: Slice, comptime field: Field) []FieldType(field) {30 pub fn items(self: Slice, comptime field: Field) []FieldType(field) {
31 const byte_ptr = self.ptrs[@enumToInt(field)];
32 const F = FieldType(field);31 const F = FieldType(field);
32 if (self.len == 0) {
33 return &[_]F{};
34 }
35 const byte_ptr = self.ptrs[@enumToInt(field)];
33 const casted_ptr = @ptrCast([*]F, @alignCast(@alignOf(F), byte_ptr));36 const casted_ptr = @ptrCast([*]F, @alignCast(@alignOf(F), byte_ptr));
34 return casted_ptr[0..self.len];37 return casted_ptr[0..self.len];
35 }38 }
...@@ -300,6 +303,8 @@ test "basic usage" {...@@ -300,6 +303,8 @@ test "basic usage" {
300 var list = MultiArrayList(Foo){};303 var list = MultiArrayList(Foo){};
301 defer list.deinit(ally);304 defer list.deinit(ally);
302305
306 testing.expectEqual(@as(usize, 0), list.items(.a).len);
307
303 try list.ensureCapacity(ally, 2);308 try list.ensureCapacity(ally, 2);
304309
305 list.appendAssumeCapacity(.{310 list.appendAssumeCapacity(.{