mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
42 lines
1.4 KiB
Zig
42 lines
1.4 KiB
Zig
const std = @import("std");
|
|
|
|
pub usingnamespace std.meta;
|
|
|
|
pub fn ReturnOf(comptime function: anytype) type {
|
|
return ReturnOfType(@TypeOf(function));
|
|
}
|
|
|
|
pub fn ReturnOfType(comptime Type: type) type {
|
|
const typeinfo: std.builtin.Type.Fn = @typeInfo(Type).Fn;
|
|
return typeinfo.return_type orelse void;
|
|
}
|
|
|
|
// partially emulates behaviour of @typeName in previous Zig versions,
|
|
// converting "some.namespace.MyType" to "MyType"
|
|
pub fn typeBaseName(comptime fullname: []const u8) []const u8 {
|
|
|
|
// leave type name like "namespace.WrapperType(namespace.MyType)" as it is
|
|
const baseidx = comptime std.mem.indexOf(u8, fullname, "(");
|
|
if (baseidx != null) return fullname;
|
|
|
|
const idx = comptime std.mem.lastIndexOf(u8, fullname, ".");
|
|
|
|
const name = if (idx == null) fullname else fullname[(idx.? + 1)..];
|
|
return comptime std.fmt.comptimePrint("{s}", .{name});
|
|
}
|
|
|
|
pub fn enumFieldNames(comptime Type: type) []const []const u8 {
|
|
var names: [std.meta.fields(Type).len][]const u8 = std.meta.fieldNames(Type).*;
|
|
var i: usize = 0;
|
|
for (names) |name| {
|
|
// zig seems to include "_" or an empty string in the list of enum field names
|
|
// it makes sense, but humans don't want that
|
|
if (@import("root").bun.strings.eqlAnyComptime(name, &.{ "_none", "", "_" })) {
|
|
continue;
|
|
}
|
|
names[i] = name;
|
|
i += 1;
|
|
}
|
|
return names[0..i];
|
|
}
|