diff options
| -rw-r--r-- | std/fmt.zig | 3 | ||||
| -rw-r--r-- | std/segmented_list.zig | 29 |
2 files changed, 21 insertions, 11 deletions
diff --git a/std/fmt.zig b/std/fmt.zig index 479ea76e8c..7c08cd14ee 100644 --- a/std/fmt.zig +++ b/std/fmt.zig @@ -426,6 +426,9 @@ pub fn formatType( if (info.child == u8) { return formatText(value, fmt, options, context, Errors, output); } + if (value.len == 0) { + return format(context, Errors, output, "[0]{}", @typeName(T.Child)); + } return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(&value)); }, .Fn => { diff --git a/std/segmented_list.zig b/std/segmented_list.zig index e0b84d5c0d..3bbbde782e 100644 --- a/std/segmented_list.zig +++ b/std/segmented_list.zig @@ -77,15 +77,19 @@ const Allocator = std.mem.Allocator; pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type { return struct { const Self = @This(); - const prealloc_exp = blk: { - // we don't use the prealloc_exp constant when prealloc_item_count is 0. - assert(prealloc_item_count != 0); - assert(std.math.isPowerOfTwo(prealloc_item_count)); + const ShelfIndex = std.math.Log2Int(usize); - const value = std.math.log2_int(usize, prealloc_item_count); - break :blk @typeOf(1)(value); + const prealloc_exp: ShelfIndex = blk: { + // we don't use the prealloc_exp constant when prealloc_item_count is 0 + // but lazy-init may still be triggered by other code so supply a value + if (prealloc_item_count == 0) { + break :blk 0; + } else { + assert(std.math.isPowerOfTwo(prealloc_item_count)); + const value = std.math.log2_int(usize, prealloc_item_count); + break :blk value; + } }; - const ShelfIndex = std.math.Log2Int(usize); prealloc_segment: [prealloc_item_count]T, dynamic_segments: [][*]T, @@ -157,11 +161,12 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type /// Grows or shrinks capacity to match usage. pub fn setCapacity(self: *Self, new_capacity: usize) !void { - if (new_capacity <= usize(1) << (prealloc_exp + self.dynamic_segments.len)) { - return self.shrinkCapacity(new_capacity); - } else { - return self.growCapacity(new_capacity); + if (prealloc_item_count != 0) { + if (new_capacity <= usize(1) << (prealloc_exp + @intCast(ShelfIndex, self.dynamic_segments.len))) { + return self.shrinkCapacity(new_capacity); + } } + return self.growCapacity(new_capacity); } /// Only grows capacity, or retains current capacity @@ -399,4 +404,6 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void { testing.expect(item == i); list.shrinkCapacity(list.len); } + + try list.setCapacity(0); } |
