diff options
| author | 2023-02-23 23:57:19 -0800 | |
|---|---|---|
| committer | 2023-02-23 23:57:19 -0800 | |
| commit | 3f04f8d0a653cf5decef2225c2044742b382718a (patch) | |
| tree | 91eb6500834e3157ecb9ab208101aa368a1191c8 /src/bun.zig | |
| parent | b5bdde28ed34070cbb1d34d13f414f4c513ee40d (diff) | |
| download | bun-3f04f8d0a653cf5decef2225c2044742b382718a.tar.gz bun-3f04f8d0a653cf5decef2225c2044742b382718a.tar.zst bun-3f04f8d0a653cf5decef2225c2044742b382718a.zip | |
Upgrade Zig (#2151)
* fixup
* Upgrade Zig
* Remove bad assertion
* strings
* bump
* mode -> optimize
* optimize
* Linux build
* Update bindgen.zig
Diffstat (limited to '')
| -rw-r--r-- | src/bun.zig | 77 |
1 files changed, 71 insertions, 6 deletions
diff --git a/src/bun.zig b/src/bun.zig index 7659dd3fb..5d1aac9b5 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -282,7 +282,57 @@ pub fn len(value: anytype) usize { }; } -pub fn span(ptr: anytype) std.mem.Span(@TypeOf(ptr)) { +fn Span(comptime T: type) type { + switch (@typeInfo(T)) { + .Optional => |optional_info| { + return ?Span(optional_info.child); + }, + .Pointer => |ptr_info| { + var new_ptr_info = ptr_info; + switch (ptr_info.size) { + .One => switch (@typeInfo(ptr_info.child)) { + .Array => |info| { + new_ptr_info.child = info.child; + new_ptr_info.sentinel = info.sentinel; + }, + else => @compileError("invalid type given to std.mem.Span"), + }, + .C => { + new_ptr_info.sentinel = &@as(ptr_info.child, 0); + new_ptr_info.is_allowzero = false; + }, + .Many, .Slice => {}, + } + new_ptr_info.size = .Slice; + return @Type(.{ .Pointer = new_ptr_info }); + }, + else => @compileError("invalid type given to std.mem.Span"), + } +} +// fn Span(comptime T: type) type { +// switch (@typeInfo(T)) { +// .Optional => |optional_info| { +// return ?Span(optional_info.child); +// }, +// .Pointer => |ptr_info| { +// var new_ptr_info = ptr_info; +// switch (ptr_info.size) { +// .C => { +// new_ptr_info.sentinel = &@as(ptr_info.child, 0); +// new_ptr_info.is_allowzero = false; +// }, +// .Many => if (ptr_info.sentinel == null) @compileError("invalid type given to bun.span: " ++ @typeName(T)), +// else => {}, +// } +// new_ptr_info.size = .Slice; +// return @Type(.{ .Pointer = new_ptr_info }); +// }, +// else => {}, +// } +// @compileError("invalid type given to bun.span: " ++ @typeName(T)); +// } + +pub fn span(ptr: anytype) Span(@TypeOf(ptr)) { if (@typeInfo(@TypeOf(ptr)) == .Optional) { if (ptr) |non_null| { return span(non_null); @@ -290,7 +340,7 @@ pub fn span(ptr: anytype) std.mem.Span(@TypeOf(ptr)) { return null; } } - const Result = std.mem.Span(@TypeOf(ptr)); + const Result = Span(@TypeOf(ptr)); const l = len(ptr); const ptr_info = @typeInfo(Result).Pointer; if (ptr_info.sentinel) |s_ptr| { @@ -376,8 +426,8 @@ pub fn cloneWithType(comptime T: type, item: T, allocator: std.mem.Allocator) !T assertDefined(item); if (comptime hasCloneFn(Child)) { - var slice = try allocator.alloc(Child, std.mem.len(item)); - for (slice) |*val, i| { + var slice = try allocator.alloc(Child, item.len); + for (slice, 0..) |*val, i| { val.* = try item[i].clone(allocator); } return slice; @@ -595,7 +645,7 @@ pub const MimallocArena = @import("./mimalloc_arena.zig").Arena; /// Zig's sliceTo(0) is scalar pub fn getenvZ(path_: [:0]const u8) ?[]const u8 { const ptr = std.c.getenv(path_.ptr) orelse return null; - return span(ptr); + return sliceTo(ptr, 0); } // These wrappers exist to use our strings.eqlLong function @@ -691,7 +741,7 @@ pub const SignalCode = enum(u8) { pub fn name(value: SignalCode) ?[]const u8 { if (@enumToInt(value) <= @enumToInt(SignalCode.SIGSYS)) { - return std.mem.span(@tagName(value)); + return asByteSlice(@tagName(value)); } return null; @@ -947,3 +997,18 @@ pub fn cstring(input: []const u8) [:0]const u8 { } pub const Semver = @import("./install/semver.zig"); + +pub fn asByteSlice(buffer: anytype) []const u8 { + return switch (@TypeOf(buffer)) { + []const u8, []u8, [:0]const u8, [:0]u8 => buffer.ptr[0..buffer.len], + [*:0]u8, [*:0]const u8 => buffer[0..len(buffer)], + [*c]const u8, [*c]u8 => span(buffer), + else => |T| { + if (comptime std.meta.trait.isPtrTo(.Array)(T)) { + return @as([]const u8, buffer); + } + + @compileError("Unsupported type " ++ @typeName(T)); + }, + }; +} |
