| author | |
| committer | |
| log | 9eeae556cc77b408c8704c9bd43960f949e0ec10 |
| tree | baccdbf880f842cd8934d718c7894997f03283c8 |
| parent | 4e80253e207258f01620b15e44866bc46708e6b1 |
All but 3 callsites of this function in the standard library and
compiler were unnecessary and were removed in faf2fd18.
In this commit, the remaining 3 callsites are removed. One of them
turned out to also be unnecessary and has been replaced by slicing
directly with the length..
The 2 remaining callsites were in the very pointer-math heavy
std/os/linux/vdso.zig code which should perhaps be refactored to better
utilize slices. These 2 callsites are replaced with a plain
@ptrCast([*:0]u8, ptr) though could likely use std.mem.sliceTo() if the
surrounding code was refactored.3 files changed, 4 insertions(+), 38 deletions(-)
lib/std/fs.zig+1-1| ... | @@ -3028,7 +3028,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { | ... | @@ -3028,7 +3028,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { |
| 3028 | /// The result is UTF16LE-encoded. | 3028 | /// The result is UTF16LE-encoded. |
| 3029 | pub fn selfExePathW() [:0]const u16 { | 3029 | pub fn selfExePathW() [:0]const u16 { |
| 3030 | const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName; | 3030 | const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName; |
| 3031 | return mem.sliceTo(std.meta.assumeSentinel(image_path_name.Buffer, 0), 0); | 3031 | return image_path_name.Buffer[0 .. image_path_name.Length / 2 :0]; |
| 3032 | } | 3032 | } |
| 3033 | 3033 | ||
| 3034 | /// `selfExeDirPath` except allocates the result on the heap. | 3034 | /// `selfExeDirPath` except allocates the result on the heap. |
lib/std/meta.zig+1-35| ... | @@ -332,41 +332,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { | ... | @@ -332,41 +332,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { |
| 332 | @compileError("Unable to derive a sentinel pointer type from " ++ @typeName(T)); | 332 | @compileError("Unable to derive a sentinel pointer type from " ++ @typeName(T)); |
| 333 | } | 333 | } |
| 334 | 334 | ||
| 335 | /// Takes a Slice or Many Pointer and returns it with the Type modified to have the given sentinel value. | 335 | const assumeSentinel = @compileError("This function has been removed, consider using std.mem.sliceTo() or if needed a @ptrCast()"); |
| 336 | /// This function assumes the caller has verified the memory contains the sentinel value. | ||
| 337 | pub fn assumeSentinel(p: anytype, comptime sentinel_val: Elem(@TypeOf(p))) Sentinel(@TypeOf(p), sentinel_val) { | ||
| 338 | const T = @TypeOf(p); | ||
| 339 | const ReturnType = Sentinel(T, sentinel_val); | ||
| 340 | switch (@typeInfo(T)) { | ||
| 341 | .Pointer => |info| switch (info.size) { | ||
| 342 | .Slice, .Many, .One => return @ptrCast(ReturnType, p), | ||
| 343 | .C => {}, | ||
| 344 | }, | ||
| 345 | .Optional => |info| switch (@typeInfo(info.child)) { | ||
| 346 | .Pointer => |ptr_info| switch (ptr_info.size) { | ||
| 347 | .Many => return @ptrCast(ReturnType, p), | ||
| 348 | else => {}, | ||
| 349 | }, | ||
| 350 | else => {}, | ||
| 351 | }, | ||
| 352 | else => {}, | ||
| 353 | } | ||
| 354 | @compileError("Unable to derive a sentinel pointer type from " ++ @typeName(T)); | ||
| 355 | } | ||
| 356 | |||
| 357 | test "std.meta.assumeSentinel" { | ||
| 358 | try testing.expect([*:0]u8 == @TypeOf(assumeSentinel(@as([*]u8, undefined), 0))); | ||
| 359 | try testing.expect([:0]u8 == @TypeOf(assumeSentinel(@as([]u8, undefined), 0))); | ||
| 360 | try testing.expect([*:0]const u8 == @TypeOf(assumeSentinel(@as([*]const u8, undefined), 0))); | ||
| 361 | try testing.expect([:0]const u8 == @TypeOf(assumeSentinel(@as([]const u8, undefined), 0))); | ||
| 362 | try testing.expect([*:0]u16 == @TypeOf(assumeSentinel(@as([*]u16, undefined), 0))); | ||
| 363 | try testing.expect([:0]const u16 == @TypeOf(assumeSentinel(@as([]const u16, undefined), 0))); | ||
| 364 | try testing.expect([*:3]u8 == @TypeOf(assumeSentinel(@as([*:1]u8, undefined), 3))); | ||
| 365 | try testing.expect([:null]?[*]u8 == @TypeOf(assumeSentinel(@as([]?[*]u8, undefined), null))); | ||
| 366 | try testing.expect([*:null]?[*]u8 == @TypeOf(assumeSentinel(@as([*]?[*]u8, undefined), null))); | ||
| 367 | try testing.expect(*[10:0]u8 == @TypeOf(assumeSentinel(@as(*[10]u8, undefined), 0))); | ||
| 368 | try testing.expect(?[*:0]u8 == @TypeOf(assumeSentinel(@as(?[*]u8, undefined), 0))); | ||
| 369 | } | ||
| 370 | 336 | ||
| 371 | pub fn containerLayout(comptime T: type) Type.ContainerLayout { | 337 | pub fn containerLayout(comptime T: type) Type.ContainerLayout { |
| 372 | return switch (@typeInfo(T)) { | 338 | return switch (@typeInfo(T)) { |
lib/std/os/linux/vdso.zig+2-2| ... | @@ -68,7 +68,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { | ... | @@ -68,7 +68,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { |
| 68 | if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue; | 68 | if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue; |
| 69 | if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue; | 69 | if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue; |
| 70 | if (0 == syms[i].st_shndx) continue; | 70 | if (0 == syms[i].st_shndx) continue; |
| 71 | const sym_name = std.meta.assumeSentinel(strings + syms[i].st_name, 0); | 71 | const sym_name = @ptrCast([*:0]u8, strings + syms[i].st_name); |
| 72 | if (!mem.eql(u8, name, mem.sliceTo(sym_name, 0))) continue; | 72 | if (!mem.eql(u8, name, mem.sliceTo(sym_name, 0))) continue; |
| 73 | if (maybe_versym) |versym| { | 73 | if (maybe_versym) |versym| { |
| 74 | if (!checkver(maybe_verdef.?, versym[i], vername, strings)) | 74 | if (!checkver(maybe_verdef.?, versym[i], vername, strings)) |
| ... | @@ -91,6 +91,6 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ | ... | @@ -91,6 +91,6 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ |
| 91 | def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); | 91 | def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); |
| 92 | } | 92 | } |
| 93 | const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); | 93 | const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); |
| 94 | const vda_name = std.meta.assumeSentinel(strings + aux.vda_name, 0); | 94 | const vda_name = @ptrCast([*:0]u8, strings + aux.vda_name); |
| 95 | return mem.eql(u8, vername, mem.sliceTo(vda_name, 0)); | 95 | return mem.eql(u8, vername, mem.sliceTo(vda_name, 0)); |
| 96 | } | 96 | } |