Skip to content

remove .slice() and .constSlice() from std.BoundedArray in lieu of .items() #24007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/compiler/aro/aro/Driver/Multilib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const Detected = struct {

pub fn filter(self: *Detected, multilib_filter: Filter, fs: Filesystem) void {
var found_count: usize = 0;
for (self.multilibs.constSlice()) |multilib| {
for (self.multilibs.items()) |multilib| {
if (multilib_filter.exists(multilib, fs)) {
self.multilibs.set(found_count, multilib);
found_count += 1;
Expand All @@ -26,9 +26,9 @@ pub const Detected = struct {

pub fn select(self: *Detected, flags: Flags) !bool {
var filtered: MultilibArray = .{};
for (self.multilibs.constSlice()) |multilib| {
for (multilib.flags.constSlice()) |multilib_flag| {
const matched = for (flags.constSlice()) |arg_flag| {
for (self.multilibs.items()) |multilib| {
for (multilib.flags.items()) |multilib_flag| {
const matched = for (flags.items()) |arg_flag| {
if (std.mem.eql(u8, arg_flag[1..], multilib_flag[1..])) break arg_flag;
} else multilib_flag;
if (matched[0] != multilib_flag[0]) break;
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/Toolchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn getProgramPath(tc: *const Toolchain, name: []const u8, buf: []u8) []const u8
var tool_specific_buf: [64]u8 = undefined;
const possible_names = possibleProgramNames(tc.driver.raw_target_triple, name, &tool_specific_buf);

for (possible_names.constSlice()) |tool_name| {
for (possible_names.items()) |tool_name| {
for (tc.program_paths.items) |program_path| {
defer fib.reset();

Expand Down
6 changes: 3 additions & 3 deletions lib/docs/wasm/markdown/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ fn appendBlockStart(p: *Parser, block_start: BlockStart) !void {
// available in the BlockStart. We can immediately parse and append
// these children now.
const containing_table = p.pending_blocks.items[p.pending_blocks.items.len - 2];
const column_alignments = containing_table.data.table.column_alignments.slice();
for (block_start.data.table_row.cells.slice(), 0..) |cell_content, i| {
const column_alignments = containing_table.data.table.column_alignments.items();
for (block_start.data.table_row.cells.items(), 0..) |cell_content, i| {
const cell_children = try p.parseInlines(cell_content);
const alignment = if (i < column_alignments.len) column_alignments[i] else .unset;
const cell = try p.addNode(.{
Expand Down Expand Up @@ -650,7 +650,7 @@ fn parseTableHeaderDelimiter(
row_cells: std.BoundedArray([]const u8, max_table_columns),
) ?std.BoundedArray(Node.TableCellAlignment, max_table_columns) {
var alignments: std.BoundedArray(Node.TableCellAlignment, max_table_columns) = .{};
for (row_cells.slice()) |content| {
for (row_cells.items()) |content| {
const alignment = parseTableHeaderDelimiterCell(content) orelse return null;
alignments.appendAssumeCapacity(alignment);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/base64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,13 @@ fn testAllApis(codecs: Codecs, expected_decoded: []const u8, expected_encoded: [
// stream encode
var list = try std.BoundedArray(u8, 0x100).init(0);
try codecs.Encoder.encodeWriter(list.writer(), expected_decoded);
try testing.expectEqualSlices(u8, expected_encoded, list.slice());
try testing.expectEqualSlices(u8, expected_encoded, list.items());

// reader to writer encode
var stream = std.io.fixedBufferStream(expected_decoded);
list = try std.BoundedArray(u8, 0x100).init(0);
try codecs.Encoder.encodeFromReaderToWriter(list.writer(), stream.reader());
try testing.expectEqualSlices(u8, expected_encoded, list.slice());
try testing.expectEqualSlices(u8, expected_encoded, list.items());
}

// Base64Decoder
Expand Down
83 changes: 44 additions & 39 deletions lib/std/bounded_array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Alignment = std.mem.Alignment;
/// ```zig
/// var actual_size = 32;
/// var a = try BoundedArray(u8, 64).init(actual_size);
/// var slice = a.slice(); // a slice of the 64-byte array
/// var slice = a.items(); // a slice of the 64-byte array
/// var a_clone = a; // creates a copy - the structure doesn't use any internal pointers
/// ```
pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
Expand All @@ -30,7 +30,7 @@ pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
// var a = try BoundedArrayAligned(u8, 16, 2).init(0);
// try a.append(255);
// try a.append(255);
// const b = @ptrCast(*const [1]u16, a.constSlice().ptr);
// const b = @ptrCast(*const [1]u16, a.items().ptr);
// try testing.expectEqual(@as(u16, 65535), b[0]);
/// ```
pub fn BoundedArrayAligned(
Expand All @@ -51,17 +51,22 @@ pub fn BoundedArrayAligned(
}

/// View the internal array as a slice whose size was previously set.
pub fn slice(self: anytype) switch (@TypeOf(&self.buffer)) {
*align(alignment.toByteUnits()) [buffer_capacity]T => []align(alignment.toByteUnits()) T,
*align(alignment.toByteUnits()) const [buffer_capacity]T => []align(alignment.toByteUnits()) const T,
else => unreachable,
pub fn items(self: anytype) switch (@TypeOf(self)) {
*Self => []align(alignment.toByteUnits()) T,
*const Self => []align(alignment.toByteUnits()) const T,
else => @compileError("Bad type"),
} {
return self.buffer[0..self.len];
}

/// View the internal array as a constant slice whose size was previously set.
pub fn constSlice(self: *const Self) []align(alignment.toByteUnits()) const T {
return self.slice();
pub fn constSlice(self: *const Self) void {
_ = self;
@compileError("This function has been deprecated, use .items() instead");
}

pub fn slice(self: *const Self) void {
_ = self;
@compileError("This function has been deprecated, use .items() instead");
}

/// Adjust the slice's length to `len`.
Expand All @@ -79,18 +84,18 @@ pub fn BoundedArrayAligned(
/// Copy the content of an existing slice.
pub fn fromSlice(m: []const T) error{Overflow}!Self {
var list = try init(m.len);
@memcpy(list.slice(), m);
@memcpy(list.items(), m);
return list;
}

/// Return the element at index `i` of the slice.
pub fn get(self: Self, i: usize) T {
return self.constSlice()[i];
return self.items()[i];
}

/// Set the value of the element at index `i` of the slice.
pub fn set(self: *Self, i: usize, item: T) void {
self.slice()[i] = item;
self.items()[i] = item;
}

/// Return the maximum length of a slice.
Expand All @@ -116,23 +121,23 @@ pub fn BoundedArrayAligned(
pub fn addOneAssumeCapacity(self: *Self) *T {
assert(self.len < buffer_capacity);
self.len += 1;
return &self.slice()[self.len - 1];
return &self.items()[self.len - 1];
}

/// Resize the slice, adding `n` new elements, which have `undefined` values.
/// The return value is a pointer to the array of uninitialized elements.
pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment.toByteUnits()) [n]T {
const prev_len = self.len;
try self.resize(self.len + n);
return self.slice()[prev_len..][0..n];
return self.items()[prev_len..][0..n];
}

/// Resize the slice, adding `n` new elements, which have `undefined` values.
/// The return value is a slice pointing to the uninitialized elements.
pub fn addManyAsSlice(self: *Self, n: usize) error{Overflow}![]align(alignment.toByteUnits()) T {
const prev_len = self.len;
try self.resize(self.len + n);
return self.slice()[prev_len..][0..n];
return self.items()[prev_len..][0..n];
}

/// Remove and return the last element from the slice, or return `null` if the slice is empty.
Expand Down Expand Up @@ -162,18 +167,18 @@ pub fn BoundedArrayAligned(
return error.Overflow;
}
_ = try self.addOne();
var s = self.slice();
var s = self.items();
mem.copyBackwards(T, s[i + 1 .. s.len], s[i .. s.len - 1]);
self.buffer[i] = item;
}

/// Insert slice `items` at index `i` by moving `slice[i .. slice.len]` to make room.
/// This operation is O(N).
pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void {
try self.ensureUnusedCapacity(items.len);
self.len += items.len;
mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]);
@memcpy(self.slice()[i..][0..items.len], items);
pub fn insertSlice(self: *Self, i: usize, new_items: []const T) error{Overflow}!void {
try self.ensureUnusedCapacity(new_items.len);
self.len += new_items.len;
mem.copyBackwards(T, self.items()[i + new_items.len .. self.len], self.items()[i .. self.len - new_items.len]);
@memcpy(self.items()[i..][0..new_items.len], new_items);
}

/// Replace range of elements `slice[start..][0..len]` with `new_items`.
Expand All @@ -186,7 +191,7 @@ pub fn BoundedArrayAligned(
new_items: []const T,
) error{Overflow}!void {
const after_range = start + len;
var range = self.slice()[start..after_range];
var range = self.items()[start..after_range];

if (range.len == new_items.len) {
@memcpy(range[0..new_items.len], new_items);
Expand All @@ -198,8 +203,8 @@ pub fn BoundedArrayAligned(
} else {
@memcpy(range[0..new_items.len], new_items);
const after_subrange = start + new_items.len;
for (self.constSlice()[after_range..], 0..) |item, i| {
self.slice()[after_subrange..][i] = item;
for (self.items()[after_range..], 0..) |item, i| {
self.items()[after_subrange..][i] = item;
}
self.len -= len - new_items.len;
}
Expand All @@ -226,7 +231,7 @@ pub fn BoundedArrayAligned(
const newlen = self.len - 1;
if (newlen == i) return self.pop().?;
const old_item = self.get(i);
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
for (self.items()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
self.set(newlen, undefined);
self.len = newlen;
return old_item;
Expand All @@ -243,25 +248,25 @@ pub fn BoundedArrayAligned(
}

/// Append the slice of items to the slice.
pub fn appendSlice(self: *Self, items: []const T) error{Overflow}!void {
try self.ensureUnusedCapacity(items.len);
self.appendSliceAssumeCapacity(items);
pub fn appendSlice(self: *Self, new_items: []const T) error{Overflow}!void {
try self.ensureUnusedCapacity(new_items.len);
self.appendSliceAssumeCapacity(new_items);
}

/// Append the slice of items to the slice, asserting the capacity is already
/// enough to store the new items.
pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {
pub fn appendSliceAssumeCapacity(self: *Self, new_items: []const T) void {
const old_len = self.len;
self.len += items.len;
@memcpy(self.slice()[old_len..][0..items.len], items);
self.len += new_items.len;
@memcpy(self.items()[old_len..][0..new_items.len], new_items);
}

/// Append a value to the slice `n` times.
/// Allocates more memory as necessary.
pub fn appendNTimes(self: *Self, value: T, n: usize) error{Overflow}!void {
const old_len = self.len;
try self.resize(old_len + n);
@memset(self.slice()[old_len..self.len], value);
@memset(self.items()[old_len..self.len], value);
}

/// Append a value to the slice `n` times.
Expand All @@ -270,7 +275,7 @@ pub fn BoundedArrayAligned(
const old_len = self.len;
self.len += n;
assert(self.len <= buffer_capacity);
@memset(self.slice()[old_len..self.len], value);
@memset(self.items()[old_len..self.len], value);
}

pub const Writer = if (T != u8)
Expand All @@ -297,18 +302,18 @@ test BoundedArray {
var a = try BoundedArray(u8, 64).init(32);

try testing.expectEqual(a.capacity(), 64);
try testing.expectEqual(a.slice().len, 32);
try testing.expectEqual(a.constSlice().len, 32);
try testing.expectEqual(a.items().len, 32);
try testing.expectEqual(a.items().len, 32);

try a.resize(48);
try testing.expectEqual(a.len, 48);

const x = [_]u8{1} ** 10;
a = try BoundedArray(u8, 64).fromSlice(&x);
try testing.expectEqualSlices(u8, &x, a.constSlice());
try testing.expectEqualSlices(u8, &x, a.items());

var a2 = a;
try testing.expectEqualSlices(u8, a.constSlice(), a2.constSlice());
try testing.expectEqualSlices(u8, a.items(), a2.items());
a2.set(0, 0);
try testing.expect(a.get(0) != a2.get(0));

Expand Down Expand Up @@ -396,7 +401,7 @@ test BoundedArray {
const w = a.writer();
const s = "hello, this is a test string";
try w.writeAll(s);
try testing.expectEqualStrings(s, a.constSlice());
try testing.expectEqualStrings(s, a.items());
}

test "BoundedArrayAligned" {
Expand All @@ -406,7 +411,7 @@ test "BoundedArrayAligned" {
try a.append(255);
try a.append(255);

const b = @as(*const [2]u16, @ptrCast(a.constSlice().ptr));
const b = @as(*const [2]u16, @ptrCast(a.items().ptr));
try testing.expectEqual(@as(u16, 0), b[0]);
try testing.expectEqual(@as(u16, 65535), b[1]);
}
4 changes: 2 additions & 2 deletions lib/std/io/Reader/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ test "readBoundedBytes correctly reads into a new bounded array" {
const reader = fis.reader();

var array = try reader.readBoundedBytes(10000);
try testing.expectEqualStrings(array.slice(), test_string);
try testing.expectEqualStrings(array.items(), test_string);
}

test "readIntoBoundedBytes correctly reads into a provided bounded array" {
Expand All @@ -368,5 +368,5 @@ test "readIntoBoundedBytes correctly reads into a provided bounded array" {

// compile time error if the size is not the same at the provided `bounded.capacity()`
try reader.readIntoBoundedBytes(10000, &bounded_array);
try testing.expectEqualStrings(bounded_array.slice(), test_string);
try testing.expectEqualStrings(bounded_array.items(), test_string);
}
2 changes: 1 addition & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4443,7 +4443,7 @@ fn performAllTheWorkInner(
try zcu.flushRetryableFailures();

// It's analysis time! Queue up our initial analysis.
for (zcu.analysis_roots.slice()) |mod| {
for (zcu.analysis_roots.items()) |mod| {
try comp.queueJob(.{ .analyze_mod = mod });
}

Expand Down
2 changes: 1 addition & 1 deletion src/Zcu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4071,7 +4071,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
try result.ensureTotalCapacity(gpa, @intCast(zcu.reference_table.count()));

try type_queue.ensureTotalCapacity(gpa, zcu.analysis_roots.len);
for (zcu.analysis_roots.slice()) |mod| {
for (zcu.analysis_roots.items()) |mod| {
const file = zcu.module_roots.get(mod).?.unwrap() orelse continue;
const root_ty = zcu.fileRootType(file);
if (root_ty == .none) continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Zcu/PerThread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,7 @@ pub fn computeAliveFiles(pt: Zcu.PerThread) Allocator.Error!bool {

// The roots of our file liveness analysis will be the analysis roots.
try zcu.alive_files.ensureTotalCapacity(gpa, zcu.analysis_roots.len);
for (zcu.analysis_roots.slice()) |mod| {
for (zcu.analysis_roots.items()) |mod| {
const file_index = zcu.module_roots.get(mod).?.unwrap() orelse continue;
const file = zcu.fileByIndex(file_index);

Expand Down