mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
63 lines
1.7 KiB
Zig
63 lines
1.7 KiB
Zig
const std = @import("std");
|
|
|
|
pub fn Bitflags(comptime T: type) type {
|
|
const tyinfo = @typeInfo(T);
|
|
const IntType = tyinfo.Struct.backing_integer.?;
|
|
|
|
return struct {
|
|
pub inline fn empty() T {
|
|
return @bitCast(@as(IntType, 0));
|
|
}
|
|
|
|
pub inline fn intersects(lhs: T, rhs: T) bool {
|
|
return asBits(lhs) & asBits(rhs) != 0;
|
|
}
|
|
|
|
pub inline fn fromName(comptime name: []const u8) T {
|
|
var this: T = .{};
|
|
@field(this, name) = true;
|
|
return this;
|
|
}
|
|
|
|
pub inline fn fromNames(comptime names: []const []const u8) T {
|
|
var this: T = .{};
|
|
inline for (names) |name| {
|
|
@field(this, name) = true;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
pub fn bitwiseOr(lhs: T, rhs: T) T {
|
|
return @bitCast(@as(IntType, @bitCast(lhs)) | @as(IntType, @bitCast(rhs)));
|
|
}
|
|
|
|
pub fn bitwiseAnd(lhs: T, rhs: T) T {
|
|
return @bitCast(@as(IntType, asBits(lhs) & asBits(rhs)));
|
|
}
|
|
|
|
pub inline fn insert(this: *T, other: T) void {
|
|
this.* = bitwiseOr(this.*, other);
|
|
}
|
|
|
|
pub fn contains(lhs: T, rhs: T) bool {
|
|
return @as(IntType, @bitCast(lhs)) & @as(IntType, @bitCast(rhs)) != 0;
|
|
}
|
|
|
|
pub inline fn asBits(this: T) IntType {
|
|
return @as(IntType, @bitCast(this));
|
|
}
|
|
|
|
pub fn isEmpty(this: T) bool {
|
|
return asBits(this) == 0;
|
|
}
|
|
|
|
pub fn eq(lhs: T, rhs: T) bool {
|
|
return asBits(lhs) == asBits(rhs);
|
|
}
|
|
|
|
pub fn neq(lhs: T, rhs: T) bool {
|
|
return asBits(lhs) != asBits(rhs);
|
|
}
|
|
};
|
|
}
|