From dbba600c6d960731d66384bdef1f6d6c7ca61784 Mon Sep 17 00:00:00 2001 From: xdBronch <51252236+xdBronch@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:41:53 -0500 Subject: [PATCH] Add getSentinel helper to Pointer and Array builtin.Types --- lib/std/builtin.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index ed18965e50..5351b0339d 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -607,6 +607,7 @@ pub const Type = union(enum) { /// The type of the sentinel is the element type of the pointer, which is /// the value of the `child` field in this struct. However there is no way /// to refer to that type here, so we use pointer to `anyopaque`. + /// See `getSentinel` for an easier way to access this value. sentinel: ?*const anyopaque, /// This data structure is used by the Zig language code generation and @@ -617,6 +618,14 @@ pub const Type = union(enum) { Slice, C, }; + + /// Returns the sentinel value casted to the child type + /// Asserts that `pointer.size` is `.Many` or `.Slice` + /// and that `pointer.sentinel` is non-null + pub fn getSentinel(pointer: Pointer) pointer.child { + std.debug.assert(pointer.size == .Many or pointer.size == .Slice); + return @as(*const pointer.child, @ptrCast(@alignCast(pointer.sentinel.?))).*; + } }; /// This data structure is used by the Zig language code generation and @@ -628,7 +637,14 @@ pub const Type = union(enum) { /// The type of the sentinel is the element type of the array, which is /// the value of the `child` field in this struct. However there is no way /// to refer to that type here, so we use pointer to `anyopaque`. + /// See `getSentinel` for an easier way to access this value. sentinel: ?*const anyopaque, + + /// Returns the sentinel value casted to the child type + /// Asserts that `array.sentinel` is non-null + pub fn getSentinel(array: Array) array.child { + return @as(*const array.child, @ptrCast(@alignCast(array.sentinel.?))).*; + } }; /// This data structure is used by the Zig language code generation and