diff options
| author | Isaac Freund <mail@isaacfreund.com> | 2023-01-24 11:37:30 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-01-24 15:39:52 -0500 |
| commit | 9eeae556cc77b408c8704c9bd43960f949e0ec10 (patch) | |
| tree | baccdbf880f842cd8934d718c7894997f03283c8 /lib/std/meta.zig | |
| parent | 4e80253e207258f01620b15e44866bc46708e6b1 (diff) | |
| download | zig-9eeae556cc77b408c8704c9bd43960f949e0ec10.tar.gz zig-9eeae556cc77b408c8704c9bd43960f949e0ec10.zip | |
std: remove meta.assumeSentinel
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.
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 36 |
1 files changed, 1 insertions, 35 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index e97edf1718..c7ec4b1702 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -332,41 +332,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { @compileError("Unable to derive a sentinel pointer type from " ++ @typeName(T)); } -/// Takes a Slice or Many Pointer and returns it with the Type modified to have the given sentinel value. -/// This function assumes the caller has verified the memory contains the sentinel value. -pub fn assumeSentinel(p: anytype, comptime sentinel_val: Elem(@TypeOf(p))) Sentinel(@TypeOf(p), sentinel_val) { - const T = @TypeOf(p); - const ReturnType = Sentinel(T, sentinel_val); - switch (@typeInfo(T)) { - .Pointer => |info| switch (info.size) { - .Slice, .Many, .One => return @ptrCast(ReturnType, p), - .C => {}, - }, - .Optional => |info| switch (@typeInfo(info.child)) { - .Pointer => |ptr_info| switch (ptr_info.size) { - .Many => return @ptrCast(ReturnType, p), - else => {}, - }, - else => {}, - }, - else => {}, - } - @compileError("Unable to derive a sentinel pointer type from " ++ @typeName(T)); -} - -test "std.meta.assumeSentinel" { - try testing.expect([*:0]u8 == @TypeOf(assumeSentinel(@as([*]u8, undefined), 0))); - try testing.expect([:0]u8 == @TypeOf(assumeSentinel(@as([]u8, undefined), 0))); - try testing.expect([*:0]const u8 == @TypeOf(assumeSentinel(@as([*]const u8, undefined), 0))); - try testing.expect([:0]const u8 == @TypeOf(assumeSentinel(@as([]const u8, undefined), 0))); - try testing.expect([*:0]u16 == @TypeOf(assumeSentinel(@as([*]u16, undefined), 0))); - try testing.expect([:0]const u16 == @TypeOf(assumeSentinel(@as([]const u16, undefined), 0))); - try testing.expect([*:3]u8 == @TypeOf(assumeSentinel(@as([*:1]u8, undefined), 3))); - try testing.expect([:null]?[*]u8 == @TypeOf(assumeSentinel(@as([]?[*]u8, undefined), null))); - try testing.expect([*:null]?[*]u8 == @TypeOf(assumeSentinel(@as([*]?[*]u8, undefined), null))); - try testing.expect(*[10:0]u8 == @TypeOf(assumeSentinel(@as(*[10]u8, undefined), 0))); - try testing.expect(?[*:0]u8 == @TypeOf(assumeSentinel(@as(?[*]u8, undefined), 0))); -} +const assumeSentinel = @compileError("This function has been removed, consider using std.mem.sliceTo() or if needed a @ptrCast()"); pub fn containerLayout(comptime T: type) Type.ContainerLayout { return switch (@typeInfo(T)) { |
