From 783e8ad0319b950a1d42d5a93e4fbb9e9df1be10 Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Fri, 21 Feb 2020 19:46:53 +0100 Subject: [PATCH] remove @bytesToSlice, @sliceToBytes from std lib --- lib/std/crypto/gimli.zig | 4 ++-- lib/std/cstr.zig | 2 +- lib/std/fifo.zig | 8 ++++---- lib/std/fs/watch.zig | 2 +- lib/std/heap.zig | 6 +++--- lib/std/io/in_stream.zig | 2 +- lib/std/mem.zig | 12 ++++++------ lib/std/net.zig | 4 ++-- lib/std/os.zig | 2 +- lib/std/process.zig | 2 +- lib/std/unicode.zig | 12 ++++++------ 11 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/std/crypto/gimli.zig b/lib/std/crypto/gimli.zig index ed2c5e764f..30304f59cf 100644 --- a/lib/std/crypto/gimli.zig +++ b/lib/std/crypto/gimli.zig @@ -24,11 +24,11 @@ pub const State = struct { const Self = @This(); pub fn toSlice(self: *Self) []u8 { - return @sliceToBytes(self.data[0..]); + return mem.sliceAsBytes(self.data[0..]); } pub fn toSliceConst(self: *Self) []const u8 { - return @sliceToBytes(self.data[0..]); + return mem.sliceAsBytes(self.data[0..]); } pub fn permute(self: *Self) void { diff --git a/lib/std/cstr.zig b/lib/std/cstr.zig index 5d16e9387b..765e0a45cf 100644 --- a/lib/std/cstr.zig +++ b/lib/std/cstr.zig @@ -72,7 +72,7 @@ pub const NullTerminated2DArray = struct { errdefer allocator.free(buf); var write_index = index_size; - const index_buf = @bytesToSlice(?[*]u8, buf); + const index_buf = mem.bytesAsSlice(?[*]u8, buf); var i: usize = 0; for (slices) |slice| { diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index ac8589f2ce..85a0e4c9d2 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -101,7 +101,7 @@ pub fn LinearFifo( } } { // set unused area to undefined - const unused = @sliceToBytes(self.buf[self.count..]); + const unused = mem.sliceAsBytes(self.buf[self.count..]); @memset(unused.ptr, undefined, unused.len); } } @@ -166,12 +166,12 @@ pub fn LinearFifo( { // set old range to undefined. Note: may be wrapped around const slice = self.readableSliceMut(0); if (slice.len >= count) { - const unused = @sliceToBytes(slice[0..count]); + const unused = mem.sliceAsBytes(slice[0..count]); @memset(unused.ptr, undefined, unused.len); } else { - const unused = @sliceToBytes(slice[0..]); + const unused = mem.sliceAsBytes(slice[0..]); @memset(unused.ptr, undefined, unused.len); - const unused2 = @sliceToBytes(self.readableSliceMut(slice.len)[0 .. count - slice.len]); + const unused2 = mem.sliceAsBytes(self.readableSliceMut(slice.len)[0 .. count - slice.len]); @memset(unused2.ptr, undefined, unused2.len); } } diff --git a/lib/std/fs/watch.zig b/lib/std/fs/watch.zig index c904f110e2..0ff8c47ecf 100644 --- a/lib/std/fs/watch.zig +++ b/lib/std/fs/watch.zig @@ -26,7 +26,7 @@ fn eqlString(a: []const u16, b: []const u16) bool { } fn hashString(s: []const u16) u32 { - return @truncate(u32, std.hash.Wyhash.hash(0, @sliceToBytes(s))); + return @truncate(u32, std.hash.Wyhash.hash(0, mem.sliceAsBytes(s))); } const WatchEventError = error{ diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 1bce45081d..135d7297a2 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -283,14 +283,14 @@ const WasmPageAllocator = struct { fn getBit(self: FreeBlock, idx: usize) PageStatus { const bit_offset = 0; - return @intToEnum(PageStatus, Io.get(@sliceToBytes(self.data), idx, bit_offset)); + return @intToEnum(PageStatus, Io.get(mem.sliceAsBytes(self.data), idx, bit_offset)); } fn setBits(self: FreeBlock, start_idx: usize, len: usize, val: PageStatus) void { const bit_offset = 0; var i: usize = 0; while (i < len) : (i += 1) { - Io.set(@sliceToBytes(self.data), start_idx + i, bit_offset, @enumToInt(val)); + Io.set(mem.sliceAsBytes(self.data), start_idx + i, bit_offset, @enumToInt(val)); } } @@ -552,7 +552,7 @@ pub const ArenaAllocator = struct { if (len >= actual_min_size) break; } const buf = try self.child_allocator.alignedAlloc(u8, @alignOf(BufNode), len); - const buf_node_slice = @bytesToSlice(BufNode, buf[0..@sizeOf(BufNode)]); + const buf_node_slice = mem.bytesAsSlice(BufNode, buf[0..@sizeOf(BufNode)]); const buf_node = &buf_node_slice[0]; buf_node.* = BufNode{ .data = buf, diff --git a/lib/std/io/in_stream.zig b/lib/std/io/in_stream.zig index ab02d20eed..4f3bac3c21 100644 --- a/lib/std/io/in_stream.zig +++ b/lib/std/io/in_stream.zig @@ -235,7 +235,7 @@ pub fn InStream(comptime ReadError: type) type { // Only extern and packed structs have defined in-memory layout. comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto); var res: [1]T = undefined; - try self.readNoEof(@sliceToBytes(res[0..])); + try self.readNoEof(mem.sliceAsBytes(res[0..])); return res[0]; } diff --git a/lib/std/mem.zig b/lib/std/mem.zig index f4c117e537..01e289988b 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -132,7 +132,7 @@ pub const Allocator = struct { // their own frame with @Frame(func). return @intToPtr([*]T, @ptrToInt(byte_slice.ptr))[0..n]; } else { - return @bytesToSlice(T, @alignCast(a, byte_slice)); + return mem.bytesAsSlice(T, @alignCast(a, byte_slice)); } } @@ -173,7 +173,7 @@ pub const Allocator = struct { return @as([*]align(new_alignment) T, undefined)[0..0]; } - const old_byte_slice = @sliceToBytes(old_mem); + const old_byte_slice = mem.sliceAsBytes(old_mem); const byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory; // Note: can't set shrunk memory to undefined as memory shouldn't be modified on realloc failure const byte_slice = try self.reallocFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment); @@ -181,7 +181,7 @@ pub const Allocator = struct { if (new_n > old_mem.len) { @memset(byte_slice.ptr + old_byte_slice.len, undefined, byte_slice.len - old_byte_slice.len); } - return @bytesToSlice(T, @alignCast(new_alignment, byte_slice)); + return mem.bytesAsSlice(T, @alignCast(new_alignment, byte_slice)); } /// Prefer calling realloc to shrink if you can tolerate failure, such as @@ -221,18 +221,18 @@ pub const Allocator = struct { // new_n <= old_mem.len and the multiplication didn't overflow for that operation. const byte_count = @sizeOf(T) * new_n; - const old_byte_slice = @sliceToBytes(old_mem); + const old_byte_slice = mem.sliceAsBytes(old_mem); @memset(old_byte_slice.ptr + byte_count, undefined, old_byte_slice.len - byte_count); const byte_slice = self.shrinkFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment); assert(byte_slice.len == byte_count); - return @bytesToSlice(T, @alignCast(new_alignment, byte_slice)); + return mem.bytesAsSlice(T, @alignCast(new_alignment, byte_slice)); } /// Free an array allocated with `alloc`. To free a single item, /// see `destroy`. pub fn free(self: *Allocator, memory: var) void { const Slice = @typeInfo(@TypeOf(memory)).Pointer; - const bytes = @sliceToBytes(memory); + const bytes = mem.sliceAsBytes(memory); const bytes_len = bytes.len + if (Slice.sentinel != null) @sizeOf(Slice.child) else 0; if (bytes_len == 0) return; const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr)); diff --git a/lib/std/net.zig b/lib/std/net.zig index 27056bf1db..19092f25ca 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -120,7 +120,7 @@ pub const Address = extern union { ip_slice[10] = 0xff; ip_slice[11] = 0xff; - const ptr = @sliceToBytes(@as(*const [1]u32, &addr)[0..]); + const ptr = mem.sliceAsBytes(@as(*const [1]u32, &addr)[0..]); ip_slice[12] = ptr[0]; ip_slice[13] = ptr[1]; @@ -164,7 +164,7 @@ pub const Address = extern union { .addr = undefined, }, }; - const out_ptr = @sliceToBytes(@as(*[1]u32, &result.in.addr)[0..]); + const out_ptr = mem.sliceAsBytes(@as(*[1]u32, &result.in.addr)[0..]); var x: u8 = 0; var index: u8 = 0; diff --git a/lib/std/os.zig b/lib/std/os.zig index 0f492a25f2..038839eeaf 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1778,7 +1778,7 @@ pub fn isCygwinPty(handle: fd_t) bool { const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]); const name_bytes = name_info_bytes[size .. size + @as(usize, name_info.FileNameLength)]; - const name_wide = @bytesToSlice(u16, name_bytes); + const name_wide = mem.bytesAsSlice(u16, name_bytes); return mem.indexOf(u16, name_wide, &[_]u16{ 'm', 's', 'y', 's', '-' }) != null or mem.indexOf(u16, name_wide, &[_]u16{ '-', 'p', 't', 'y' }) != null; } diff --git a/lib/std/process.zig b/lib/std/process.zig index 89307b44da..d9ec0a056d 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -436,7 +436,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 { const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes); errdefer allocator.free(buf); - const result_slice_list = @bytesToSlice([]u8, buf[0..slice_list_bytes]); + const result_slice_list = mem.bytesAsSlice([]u8, buf[0..slice_list_bytes]); const result_contents = buf[slice_list_bytes..]; mem.copy(u8, result_contents, contents_slice); diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index ed24f83c33..85c91602d0 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -243,7 +243,7 @@ pub const Utf16LeIterator = struct { pub fn init(s: []const u16) Utf16LeIterator { return Utf16LeIterator{ - .bytes = @sliceToBytes(s), + .bytes = mem.sliceAsBytes(s), .i = 0, }; } @@ -496,7 +496,7 @@ pub fn utf16leToUtf8(utf8: []u8, utf16le: []const u16) !usize { test "utf16leToUtf8" { var utf16le: [2]u16 = undefined; - const utf16le_as_bytes = @sliceToBytes(utf16le[0..]); + const utf16le_as_bytes = mem.sliceAsBytes(utf16le[0..]); { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A'); @@ -606,12 +606,12 @@ test "utf8ToUtf16Le" { { const length = try utf8ToUtf16Le(utf16le[0..], "𐐷"); testing.expectEqual(@as(usize, 2), length); - testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16le[0..])); + testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", mem.sliceAsBytes(utf16le[0..])); } { const length = try utf8ToUtf16Le(utf16le[0..], "\u{10FFFF}"); testing.expectEqual(@as(usize, 2), length); - testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16le[0..])); + testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", mem.sliceAsBytes(utf16le[0..])); } } @@ -619,13 +619,13 @@ test "utf8ToUtf16LeWithNull" { { const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "𐐷"); defer testing.allocator.free(utf16); - testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..])); + testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", mem.sliceAsBytes(utf16[0..])); testing.expect(utf16[2] == 0); } { const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "\u{10FFFF}"); defer testing.allocator.free(utf16); - testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..])); + testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", mem.sliceAsBytes(utf16[0..])); testing.expect(utf16[2] == 0); } }