diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-04-11 17:55:25 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-04-13 02:20:32 -0400 |
| commit | f32a5d349d2c359a2a1f627aa70e1a7a6f6330ea (patch) | |
| tree | 05a0fe7de04e6aa91775e954f78dd1a478d7e675 /lib/std/bounded_array.zig | |
| parent | ec2888858102035f296c01df5aacbd255c35d06f (diff) | |
| download | zig-f32a5d349d2c359a2a1f627aa70e1a7a6f6330ea.tar.gz zig-f32a5d349d2c359a2a1f627aa70e1a7a6f6330ea.zip | |
std: eradicate u29 and embrace std.mem.Alignment
Diffstat (limited to 'lib/std/bounded_array.zig')
| -rw-r--r-- | lib/std/bounded_array.zig | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/lib/std/bounded_array.zig b/lib/std/bounded_array.zig index 42c77e2e77..1a4407e687 100644 --- a/lib/std/bounded_array.zig +++ b/lib/std/bounded_array.zig @@ -2,6 +2,7 @@ const std = @import("std.zig"); const assert = std.debug.assert; const mem = std.mem; const testing = std.testing; +const Alignment = std.mem.Alignment; /// A structure with an array and a length, that can be used as a slice. /// @@ -16,7 +17,7 @@ const testing = std.testing; /// 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 { - return BoundedArrayAligned(T, @alignOf(T), buffer_capacity); + return BoundedArrayAligned(T, .of(T), buffer_capacity); } /// A structure with an array, length and alignment, that can be used as a @@ -34,12 +35,12 @@ pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type { /// ``` pub fn BoundedArrayAligned( comptime T: type, - comptime alignment: u29, + comptime alignment: Alignment, comptime buffer_capacity: usize, ) type { return struct { const Self = @This(); - buffer: [buffer_capacity]T align(alignment) = undefined, + buffer: [buffer_capacity]T align(alignment.toByteUnits()) = undefined, len: usize = 0, /// Set the actual length of the slice. @@ -51,15 +52,15 @@ 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) [buffer_capacity]T => []align(alignment) T, - *align(alignment) const [buffer_capacity]T => []align(alignment) const T, + *align(alignment.toByteUnits()) [buffer_capacity]T => []align(alignment.toByteUnits()) T, + *align(alignment.toByteUnits()) const [buffer_capacity]T => []align(alignment.toByteUnits()) const T, else => unreachable, } { 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) const T { + pub fn constSlice(self: *const Self) []align(alignment.toByteUnits()) const T { return self.slice(); } @@ -120,7 +121,7 @@ pub fn BoundedArrayAligned( /// 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) [n]T { + 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]; @@ -128,7 +129,7 @@ pub fn BoundedArrayAligned( /// 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) T { + 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]; @@ -146,7 +147,7 @@ pub fn BoundedArrayAligned( /// This can be useful for writing directly into it. /// Note that such an operation must be followed up with a /// call to `resize()` - pub fn unusedCapacitySlice(self: *Self) []align(alignment) T { + pub fn unusedCapacitySlice(self: *Self) []align(alignment.toByteUnits()) T { return self.buffer[self.len..]; } @@ -399,7 +400,7 @@ test BoundedArray { } test "BoundedArrayAligned" { - var a = try BoundedArrayAligned(u8, 16, 4).init(0); + var a = try BoundedArrayAligned(u8, .@"16", 4).init(0); try a.append(0); try a.append(0); try a.append(255); |
