graphics
This commit is contained in:
@@ -66,6 +66,17 @@ pub const Type = union(enum) {
|
||||
if (std.mem.eql(u8, name, "f32")) return .f32;
|
||||
if (std.mem.eql(u8, name, "f64")) return .f64;
|
||||
if (std.mem.eql(u8, name, "Any")) return .any_type;
|
||||
// Sentinel-terminated slice: [:0]T → string_type when T is u8
|
||||
if (name.len >= 5 and name[0] == '[' and name[1] == ':') {
|
||||
// Find closing ']'
|
||||
if (std.mem.indexOfScalar(u8, name, ']')) |close| {
|
||||
const sentinel = name[2..close];
|
||||
const elem = name[close + 1 ..];
|
||||
if (std.mem.eql(u8, sentinel, "0") and std.mem.eql(u8, elem, "u8")) {
|
||||
return .string_type;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Many-pointer: [*]T
|
||||
if (name.len >= 4 and name[0] == '[' and name[1] == '*' and name[2] == ']') {
|
||||
return .{ .many_pointer_type = .{ .element_name = name[3..] } };
|
||||
@@ -116,6 +127,19 @@ pub const Type = union(enum) {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isString(self: Type) bool {
|
||||
return self == .string_type;
|
||||
}
|
||||
|
||||
/// Returns true for both `string` (null-terminated) and `[]u8` (byte slice)
|
||||
pub fn isStringLike(self: Type) bool {
|
||||
if (self == .string_type) return true;
|
||||
if (self.isSlice()) {
|
||||
return std.mem.eql(u8, self.slice_type.element_name, "u8");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn isSlice(self: Type) bool {
|
||||
return switch (self) {
|
||||
.slice_type => true,
|
||||
@@ -226,10 +250,19 @@ pub const Type = union(enum) {
|
||||
/// Everything else requires `xx`.
|
||||
pub fn isImplicitlyConvertibleTo(self: Type, target: Type) bool {
|
||||
if (std.meta.eql(self, target)) return true;
|
||||
// Struct types: compare names by content (not pointer identity)
|
||||
if (self.isStruct() and target.isStruct()) {
|
||||
return std.mem.eql(u8, self.struct_type, target.struct_type);
|
||||
}
|
||||
// Slice types: compare element names by content (not pointer)
|
||||
if (self.isSlice() and target.isSlice()) {
|
||||
return std.mem.eql(u8, self.slice_type.element_name, target.slice_type.element_name);
|
||||
}
|
||||
// string <-> []u8: same layout, bidirectional implicit conversion
|
||||
if (self == .string_type and target.isSlice() and
|
||||
std.mem.eql(u8, target.slice_type.element_name, "u8")) return true;
|
||||
if (self.isSlice() and std.mem.eql(u8, self.slice_type.element_name, "u8") and
|
||||
target == .string_type) return true;
|
||||
// Pointer types: compare pointee names by content, *void is universal (both directions)
|
||||
if (self.isPointer() and target.isPointer()) {
|
||||
if (std.mem.eql(u8, self.pointer_type.pointee_name, "void")) return true;
|
||||
|
||||
Reference in New Issue
Block a user