Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for latest Zig master #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions extensions/glib2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub inline fn new(comptime T: type, value: T) *T {
/// Destroys a value created using `create`.
pub fn destroy(ptr: anytype) void {
const type_info = @typeInfo(@TypeOf(ptr));
if (type_info != .Pointer or type_info.Pointer.size != .One) @compileError("must be a single-item pointer");
glib.freeSized(@ptrCast(ptr), @sizeOf(type_info.Pointer.child));
if (type_info != .pointer or type_info.pointer.size != .One) @compileError("must be a single-item pointer");
glib.freeSized(@ptrCast(ptr), @sizeOf(type_info.pointer.child));
}

/// Heap allocates a slice of `n` values of type `T` using `glib.mallocN`. `T`
Expand All @@ -36,8 +36,8 @@ pub fn alloc(comptime T: type, n: usize) []T {
/// Frees a slice created using `alloc`.
pub fn free(ptr: anytype) void {
const type_info = @typeInfo(@TypeOf(ptr));
if (type_info != .Pointer or type_info.Pointer.size != .Slice) @compileError("must be a slice");
glib.freeSized(@ptrCast(ptr.ptr), @sizeOf(type_info.Pointer.child) * ptr.len);
if (type_info != .pointer or type_info.pointer.size != .Slice) @compileError("must be a slice");
glib.freeSized(@ptrCast(ptr.ptr), @sizeOf(type_info.pointer.child) * ptr.len);
}

pub const Bytes = struct {
Expand Down Expand Up @@ -91,27 +91,27 @@ pub const Variant = struct {
child.* = newFrom(item);
}
return glib.Variant.newArray(child_type, &children, children.len);
} else if (type_info == .Pointer and type_info.Pointer.size == .Slice) {
const child_type = glib.ext.VariantType.newFor(type_info.Pointer.child);
} else if (type_info == .pointer and type_info.pointer.size == .Slice) {
const child_type = glib.ext.VariantType.newFor(type_info.pointer.child);
defer child_type.free();
const children = alloc(*glib.Variant, contents.len);
defer free(children);
for (contents, children) |item, *child| {
child.* = newFrom(item);
}
return glib.Variant.newArray(child_type, children.ptr, children.len);
} else if (type_info == .Optional) {
const child_type = glib.ext.VariantType.newFor(type_info.Optional.child);
} else if (type_info == .optional) {
const child_type = glib.ext.VariantType.newFor(type_info.optional.child);
defer child_type.free();
if (contents) |value| {
const child = newFrom(value);
return glib.Variant.newMaybe(child_type, child);
} else {
return glib.Variant.newMaybe(child_type, null);
}
} else if (type_info == .Struct and type_info.Struct.is_tuple) {
var children: [type_info.Struct.fields.len]*glib.Variant = undefined;
inline for (type_info.Struct.fields, &children) |field, *child| {
} else if (type_info == .@"struct" and type_info.@"struct".is_tuple) {
var children: [type_info.@"struct".fields.len]*glib.Variant = undefined;
inline for (type_info.@"struct".fields, &children) |field, *child| {
child.* = newFrom(@field(contents, field.name));
}
return glib.Variant.newTuple(&children, children.len);
Expand Down Expand Up @@ -153,14 +153,14 @@ pub const VariantType = struct {
} else if (T == *glib.Variant) {
return "v";
} else if (type_info == .Array) {
return "a" ++ stringFor(type_info.Array.child);
} else if (type_info == .Pointer and type_info.Pointer.size == .Slice) {
return "a" ++ stringFor(type_info.Pointer.child);
} else if (type_info == .Optional) {
return "m" ++ stringFor(type_info.Optional.child);
} else if (type_info == .Struct and type_info.Struct.is_tuple) {
return "a" ++ stringFor(type_info.array.child);
} else if (type_info == .pointer and type_info.pointer.size == .Slice) {
return "a" ++ stringFor(type_info.pointer.child);
} else if (type_info == .optional) {
return "m" ++ stringFor(type_info.optional.child);
} else if (type_info == .@"struct" and type_info.@"struct".is_tuple) {
comptime var str: [:0]const u8 = "(";
inline for (type_info.Struct.fields) |field| {
inline for (type_info.@"struct".fields) |field| {
str = str ++ comptime stringFor(field.type);
}
return str ++ ")";
Expand All @@ -172,12 +172,12 @@ pub const VariantType = struct {

fn isCString(comptime T: type) bool {
return switch (@typeInfo(T)) {
.Pointer => |info| switch (info.size) {
.pointer => |info| switch (info.size) {
.One => switch (@typeInfo(info.child)) {
.Array => |child| child.child == u8 and std.meta.sentinel(info.child) == @as(u8, 0),
.array => |child| child.child == u8 and std.meta.sentinel(info.child) == @as(u8, 0),
else => false,
},
.Many, .Slice => info.child == u8 and std.meta.sentinel(T) == @as(u8, 0),
.many, .slice => info.child == u8 and std.meta.sentinel(T) == @as(u8, 0),
else => false,
},
else => false,
Expand Down
80 changes: 40 additions & 40 deletions extensions/gobject2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -173,32 +173,32 @@ pub fn defineClass(
comptime options: DefineClassOptions(Instance),
) fn () callconv(.C) gobject.Type {
const instance_info = @typeInfo(Instance);
if (instance_info != .Struct or instance_info.Struct.layout != .@"extern") {
if (instance_info != .@"struct" or instance_info.@"struct".layout != .@"extern") {
@compileError("an instance type must be an extern struct");
}

if (!@hasDecl(Instance, "Parent")) {
@compileError("a class type must have a declaration named Parent pointing to the parent type");
}
const parent_info = @typeInfo(Instance.Parent);
if (parent_info != .Struct or parent_info.Struct.layout != .@"extern" or !@hasDecl(Instance.Parent, "getGObjectType")) {
if (parent_info != .@"struct" or parent_info.@"struct".layout != .@"extern" or !@hasDecl(Instance.Parent, "getGObjectType")) {
@compileError("the defined parent type " ++ @typeName(Instance.Parent) ++ " does not appear to be a GObject class type");
}
if (instance_info.Struct.fields.len == 0 or instance_info.Struct.fields[0].type != Instance.Parent) {
if (instance_info.@"struct".fields.len == 0 or instance_info.@"struct".fields[0].type != Instance.Parent) {
@compileError("the first field of the instance struct must have type " ++ @typeName(Instance.Parent));
}

if (!@hasDecl(Instance, "Class")) {
@compileError("a class type must have a member named Class pointing to the class record");
}
const class_info = @typeInfo(Instance.Class);
if (class_info != .Struct or class_info.Struct.layout != .@"extern") {
if (class_info != .@"struct" or class_info.@"struct".layout != .@"extern") {
@compileError("a class type must be an extern struct");
}
if (!@hasDecl(Instance.Class, "Instance") or Instance.Class.Instance != Instance) {
@compileError("a class type must have a declaration named Instance pointing to the instance type");
}
if (class_info.Struct.fields.len == 0 or class_info.Struct.fields[0].type != Instance.Parent.Class) {
if (class_info.@"struct".fields.len == 0 or class_info.@"struct".fields[0].type != Instance.Parent.Class) {
@compileError("the first field of the class struct must have type " ++ @typeName(Instance.Parent.Class));
}

Expand Down Expand Up @@ -338,16 +338,16 @@ pub fn defineEnum(
comptime options: DefineEnumOptions,
) fn () callconv(.C) gobject.Type {
const enum_info = @typeInfo(Enum);
if (enum_info != .Enum or enum_info.Enum.tag_type != c_int) {
if (enum_info != .@"enum" or enum_info.@"enum".tag_type != c_int) {
@compileError("an enum type must have a tag type of c_int");
}
if (!enum_info.Enum.is_exhaustive) {
if (!enum_info.@"enum".is_exhaustive) {
@compileError("an enum type must be exhaustive");
}

const n_values = enum_info.Enum.fields.len;
var enum_values: [n_values + 1]gobject.EnumValue = undefined;
for (enum_info.Enum.fields, enum_values[0..n_values]) |field, *value| {
const n_values = enum_info.@"enum".fields.len;
var enum_values: [n_values + 1]gobject.enum_value = undefined;
for (enum_info.@"enum".fields, enum_values[0..n_values]) |field, *value| {
value.* = .{
.value = field.value,
.value_name = field.name,
Expand Down Expand Up @@ -394,12 +394,12 @@ pub fn defineFlags(
comptime options: DefineFlagsOptions,
) fn () callconv(.C) gobject.Type {
const flags_info = @typeInfo(Flags);
if (flags_info != .Struct or flags_info.Struct.layout != .@"packed" or flags_info.Struct.backing_integer != c_uint) {
if (flags_info != .@"struct" or flags_info.@"struct".layout != .@"packed" or flags_info.@"struct".backing_integer != c_uint) {
@compileError("a flags type must have a backing integer type of c_uint");
}

comptime var n_values = 0;
for (flags_info.Struct.fields) |field| {
for (flags_info.@"struct".fields) |field| {
if (!std.mem.startsWith(u8, field.name, "_")) {
if (@bitSizeOf(field.type) != 1) {
@compileError("non-padding flags field " ++ field.name ++ " must be 1 bit");
Expand All @@ -409,7 +409,7 @@ pub fn defineFlags(
}
comptime var flags_values: [n_values + 1]gobject.FlagsValue = undefined;
var current_value = 0;
for (flags_info.Struct.fields) |field| {
for (flags_info.@"struct".fields) |field| {
if (!std.mem.startsWith(u8, field.name, "_")) {
flags_values[current_value] = .{
.value = 1 << @bitOffsetOf(Flags, field.name),
Expand Down Expand Up @@ -458,7 +458,7 @@ pub fn Accessor(comptime Owner: type, comptime Data: type) type {
}

fn FieldType(comptime T: type, comptime name: []const u8) type {
return for (@typeInfo(T).Struct.fields) |field| {
return for (@typeInfo(T).@"struct".fields) |field| {
if (std.mem.eql(u8, field.name, name)) break field.type;
} else @compileError("no field named " ++ name ++ " in " ++ @typeName(T));
}
Expand Down Expand Up @@ -698,15 +698,15 @@ pub fn defineProperty(
);
} else if (std.meta.hasFn(Data, "getGObjectType")) {
return switch (@typeInfo(Data)) {
.Enum => gobject.paramSpecEnum(
.@"enum" => gobject.paramSpecEnum(
name,
options.nick orelse null,
options.blurb orelse null,
Data.getGObjectType(),
@intFromEnum(options.default),
flags,
),
.Struct => gobject.paramSpecFlags(
.@"struct" => gobject.paramSpecFlags(
name,
options.nick orelse null,
options.blurb orelse null,
Expand Down Expand Up @@ -752,7 +752,7 @@ pub fn defineProperty(
/// The properties passed in `properties` should be the structs returned by
/// `defineProperty`.
pub fn registerProperties(class: anytype, properties: []const type) void {
const Instance = @typeInfo(@TypeOf(class)).Pointer.child.Instance;
const Instance = @typeInfo(@TypeOf(class)).pointer.child.Instance;
gobject.Object.virtual_methods.get_property.implement(class, struct {
fn getProperty(object: *Instance, id: c_uint, value: *gobject.Value, _: *gobject.ParamSpec) callconv(.C) void {
inline for (properties, 1..) |property, i| {
Expand Down Expand Up @@ -810,10 +810,10 @@ pub fn defineSignal(
comptime param_types: []const type,
comptime ReturnType: type,
) type {
const EmitParams = @Type(.{ .Struct = .{
const EmitParams = @Type(.{ .@"struct" = .{
.layout = .auto,
.fields = fields: {
var fields: [param_types.len]std.builtin.Type.StructField = undefined;
var fields: [param_types.len]std.builtin.Type.struct_field = undefined;
for (param_types, &fields, 0..) |ParamType, *field, i| {
field.* = .{
.name = std.fmt.comptimePrint("{}", .{i}),
Expand Down Expand Up @@ -878,7 +878,7 @@ pub fn defineSignal(
pub fn connect(
target: anytype,
comptime T: type,
callback: SignalHandler(@typeInfo(@TypeOf(target)).Pointer.child, param_types, T, ReturnType),
callback: SignalHandler(@typeInfo(@TypeOf(target)).pointer.child, param_types, T, ReturnType),
data: T,
options: struct {
after: bool = false,
Expand Down Expand Up @@ -917,10 +917,10 @@ pub const impl_helpers = struct {
/// guaranteed.
pub inline fn as(comptime T: type, self: anytype) *T {
const self_info = @typeInfo(@TypeOf(self));
if (self_info != .Pointer or self_info.Pointer.size != .One) {
if (self_info != .pointer or self_info.pointer.size != .One) {
@compileError("cannot cast a non-pointer type");
}
const Self = self_info.Pointer.child;
const Self = self_info.pointer.child;

if (isAssignableFrom(T, Self)) {
return @ptrCast(@alignCast(self));
Expand Down Expand Up @@ -979,7 +979,7 @@ pub fn isA(self: anytype, comptime T: type) bool {

/// Creates a new instance of an object type with the given properties.
pub fn newInstance(comptime T: type, properties: anytype) *T {
const typeInfo = @typeInfo(@TypeOf(properties)).Struct;
const typeInfo = @typeInfo(@TypeOf(properties)).@"struct";
const n_props = typeInfo.fields.len;
var names: [n_props][*:0]const u8 = undefined;
var values: [n_props]gobject.Value = undefined;
Expand Down Expand Up @@ -1085,10 +1085,10 @@ pub const Value = struct {
} else if (T == f64) {
return value.getDouble();
} else if (isCString(T)) {
if (@typeInfo(T) != .Optional) {
if (@typeInfo(T) != .optional) {
@compileError("cannot guarantee value is non-null");
}
const Pointer = @typeInfo(@typeInfo(T).Optional.child).Pointer;
const Pointer = @typeInfo(@typeInfo(T).optional.child).pointer;
if (!Pointer.is_const) {
@compileError("get does not take ownership; can only return const strings");
}
Expand All @@ -1099,12 +1099,12 @@ pub const Value = struct {
};
} else if (std.meta.hasFn(T, "getGObjectType")) {
return switch (@typeInfo(T)) {
.Enum => @enumFromInt(value.getEnum()),
.Struct => @bitCast(value.getFlags()),
.@"enum" => @enumFromInt(value.getEnum()),
.@"struct" => @bitCast(value.getFlags()),
else => @compileError("cannot extract " ++ @typeName(T) ++ " from Value"),
};
} else if (singlePointerChild(T)) |Child| {
if (@typeInfo(T) != .Optional) {
if (@typeInfo(T) != .optional) {
@compileError("cannot guarantee value is non-null");
}
if (Child == gobject.ParamSpec) {
Expand Down Expand Up @@ -1156,14 +1156,14 @@ pub const Value = struct {
} else if (comptime isCString(T)) {
// orelse null as temporary workaround for https://github.com/ziglang/zig/issues/12523
switch (@typeInfo(T)) {
.Pointer => value.setString(contents),
.Optional => value.setString(contents orelse null),
.pointer => value.setString(contents),
.optional => value.setString(contents orelse null),
else => unreachable,
}
} else if (std.meta.hasFn(T, "getGObjectType")) {
switch (@typeInfo(T)) {
.Enum => value.setEnum(@intFromEnum(contents)),
.Struct => value.setFlags(@bitCast(contents)),
.@"enum" => value.setEnum(@intFromEnum(contents)),
.@"struct" => value.setFlags(@bitCast(contents)),
else => @compileError("cannot construct Value from " ++ @typeName(T)),
}
} else if (singlePointerChild(T)) |Child| {
Expand Down Expand Up @@ -1192,18 +1192,18 @@ inline fn isObject(comptime T: type) bool {

inline fn isCString(comptime T: type) bool {
return switch (@typeInfo(T)) {
.Pointer => |pointer| switch (pointer.size) {
.pointer => |pointer| switch (pointer.size) {
.One => switch (@typeInfo(pointer.child)) {
.Array => |child| child.child == u8 and std.meta.sentinel(pointer.child) == @as(u8, 0),
.array => |child| child.child == u8 and std.meta.sentinel(pointer.child) == @as(u8, 0),
else => false,
},
.Many, .Slice => pointer.child == u8 and std.meta.sentinel(T) == @as(u8, 0),
.C => pointer.child == u8,
},
.Optional => |optional| switch (@typeInfo(optional.child)) {
.Pointer => |pointer| switch (pointer.size) {
.optional => |optional| switch (@typeInfo(optional.child)) {
.pointer => |pointer| switch (pointer.size) {
.One => switch (@typeInfo(pointer.child)) {
.Array => |child| child.child == u8 and std.meta.sentinel(pointer.child) == @as(u8, 0),
.array => |child| child.child == u8 and std.meta.sentinel(pointer.child) == @as(u8, 0),
else => false,
},
.Many, .Slice => pointer.child == u8 and std.meta.sentinel(optional.child) == @as(u8, 0),
Expand All @@ -1217,12 +1217,12 @@ inline fn isCString(comptime T: type) bool {

inline fn singlePointerChild(comptime T: type) ?type {
return switch (@typeInfo(T)) {
.Pointer => |pointer| switch (pointer.size) {
.pointer => |pointer| switch (pointer.size) {
.One, .C => pointer.child,
else => null,
},
.Optional => |optional| switch (@typeInfo(optional.child)) {
.Pointer => |pointer| switch (pointer.size) {
.optional => |optional| switch (@typeInfo(optional.child)) {
.pointer => |pointer| switch (pointer.size) {
.One => pointer.child,
else => null,
},
Expand Down
8 changes: 4 additions & 4 deletions extensions/gtk4.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ pub const impl_helpers = struct {
}

fn ensureWidgetType(comptime Container: type, comptime field_name: []const u8) void {
inline for (@typeInfo(Container).Struct.fields) |field| {
inline for (@typeInfo(Container).@"struct".fields) |field| {
if (comptime std.mem.eql(u8, field.name, field_name)) {
const WidgetType = switch (@typeInfo(field.type)) {
.Pointer => |pointer| widget_type: {
.pointer => |pointer| widget_type: {
if (pointer.size != .One) {
@compileError("bound child type must be a single pointer");
}
break :widget_type pointer.child;
},
.Optional => |optional| switch (@typeInfo(optional.child)) {
.Pointer => |pointer| widget_type: {
.optional => |optional| switch (@typeInfo(optional.child)) {
.pointer => |pointer| widget_type: {
if (pointer.size != .One) {
@compileError("bound child type must be a single pointer");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub const std_options: std.Options = .{

pub fn logImpl(
comptime level: log.Level,
comptime scope: @Type(.EnumLiteral),
comptime scope: @Type(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
Expand Down
Loading