From 957279f4341d05e200dc55196e2fbc7cb50c2897 Mon Sep 17 00:00:00 2001 From: Sobeston <15335529+Sobeston@users.noreply.github.com> Date: Sat, 15 Jul 2023 23:04:42 +0100 Subject: [PATCH] updated to zig master --- example/seats.zig | 2 +- src/common_core.zig | 22 ++++++------- src/scanner.zig | 62 +++++++++++++++++++------------------ src/wayland_client_core.zig | 2 +- src/wayland_server_core.zig | 24 +++++++------- src/xml.zig | 4 +-- 6 files changed, 59 insertions(+), 57 deletions(-) diff --git a/example/seats.zig b/example/seats.zig index 701a95e..e0d75ed 100644 --- a/example/seats.zig +++ b/example/seats.zig @@ -15,7 +15,7 @@ pub fn main() !void { fn listener(registry: *wl.Registry, event: wl.Registry.Event, running: *bool) void { switch (event) { .global => |global| { - if (std.cstr.cmp(global.interface, wl.Seat.getInterface().name) == 0) { + if (std.mem.orderZ(u8, global.interface, wl.Seat.getInterface().name) == .eq) { const seat = registry.bind(global.name, wl.Seat, 1) catch return; seat.setListener(*bool, seatListener, running); } diff --git a/src/common_core.zig b/src/common_core.zig index 5a3b914..6643a59 100644 --- a/src/common_core.zig +++ b/src/common_core.zig @@ -35,7 +35,7 @@ pub const Array = extern struct { pub fn slice(array: Array, comptime T: type) []T { const data = array.data orelse return &[0]T{}; // The wire protocol/libwayland only guarantee 32-bit word alignment. - const ptr = @ptrCast([*]T, @alignCast(4, data)); + const ptr = @as([*]align(4) T, @ptrCast(data)); return ptr[0..@divExact(array.size, @sizeOf(T))]; } }; @@ -45,19 +45,19 @@ pub const Fixed = enum(i32) { _, pub fn toInt(f: Fixed) i24 { - return @truncate(i24, @enumToInt(f) >> 8); + return @as(i24, @truncate(@intFromEnum(f) >> 8)); } pub fn fromInt(i: i24) Fixed { - return @intToEnum(Fixed, @as(i32, i) << 8); + return @as(Fixed, @enumFromInt(@as(i32, i) << 8)); } pub fn toDouble(f: Fixed) f64 { - return @intToFloat(f64, @enumToInt(f)) / 256; + return @as(f64, @floatFromInt(@intFromEnum(f))) / 256; } pub fn fromDouble(d: f64) Fixed { - return @intToEnum(Fixed, @floatToInt(i32, d * 256)); + return @as(Fixed, @enumFromInt(@as(i32, @intFromFloat(d * 256)))); } }; @@ -90,21 +90,21 @@ pub fn Dispatcher(comptime Obj: type, comptime Data: type) type { inline for (@typeInfo(payload_field.type).Struct.fields, 0..) |f, i| { switch (@typeInfo(f.type)) { // signed/unsigned ints, fds, new_ids, bitfield enums - .Int, .Struct => @field(payload_data, f.name) = @bitCast(f.type, args[i].u), + .Int, .Struct => @field(payload_data, f.name) = @as(f.type, @bitCast(args[i].u)), // objects, strings, arrays - .Pointer, .Optional => @field(payload_data, f.name) = @intToPtr(f.type, @ptrToInt(args[i].o)), + .Pointer, .Optional => @field(payload_data, f.name) = @as(f.type, @ptrFromInt(@intFromPtr(args[i].o))), // non-bitfield enums - .Enum => @field(payload_data, f.name) = @intToEnum(f.type, args[i].i), + .Enum => @field(payload_data, f.name) = @as(f.type, @enumFromInt(args[i].i)), else => unreachable, } } } const HandlerFn = fn (*Obj, Payload, Data) void; - @ptrCast(*const HandlerFn, @alignCast(@alignOf(HandlerFn), implementation))( - @ptrCast(*Obj, object), + @as(*const HandlerFn, @ptrCast(@alignCast(implementation)))( + @as(*Obj, @ptrCast(object)), @unionInit(Payload, payload_field.name, payload_data), - @intToPtr(Data, @ptrToInt(object.getUserData())), + @as(Data, @ptrFromInt(@intFromPtr(object.getUserData()))), ); return 0; diff --git a/src/scanner.zig b/src/scanner.zig index a1f1da3..547c2bf 100644 --- a/src/scanner.zig +++ b/src/scanner.zig @@ -589,7 +589,7 @@ const Interface = struct { \\ pub const getInterface = common.{[namespace]}.{[interface]}.getInterface; , .{ .type = titleCaseTrim(interface.name), - .version = std.math.min(interface.version, target_version), + .version = @min(interface.version, target_version), .namespace = fmtId(namespace), .interface = fmtId(trimPrefix(interface.name)), }); @@ -607,7 +607,7 @@ const Interface = struct { if (side == .client) { try writer.print( \\pub fn setQueue(_{[interface]}: *{[type]}, _queue: *client.wl.EventQueue) void {{ - \\ const _proxy = @ptrCast(*client.wl.Proxy, _{[interface]}); + \\ const _proxy: *client.wl.Proxy = @ptrCast(_{[interface]}); \\ _proxy.setQueue(_queue); \\}} , .{ @@ -634,8 +634,8 @@ const Interface = struct { \\ _listener: *const fn ({[interface]}: *{[type]}, event: Event, data: T) void, \\ _data: T, \\) void {{ - \\ const _proxy = @ptrCast(*client.wl.Proxy, _{[interface]}); - \\ const _mut_data = @intToPtr(?*anyopaque, @ptrToInt(_data)); + \\ const _proxy: *client.wl.Proxy = @ptrCast(_{[interface]}); + \\ const _mut_data: ?*anyopaque = @ptrCast(_data); \\ _proxy.addDispatcher(common.Dispatcher({[type]}, T).dispatcher, _listener, _mut_data); \\}} , .{ @@ -657,7 +657,7 @@ const Interface = struct { } else if (!has_destroy) { try writer.print( \\pub fn destroy(_{[interface]}: *{[type]}) void {{ - \\ const _proxy = @ptrCast(*client.wl.Proxy, _{[interface]}); + \\ const _proxy: *client.wl.Proxy = @ptrCast(_{[interface]}); \\ _proxy.destroy(); \\}} , .{ @@ -668,11 +668,11 @@ const Interface = struct { } else { try writer.print( \\pub fn create(_client: *server.wl.Client, _version: u32, _id: u32) !*{(tc)} {{ - \\ return @ptrCast(*{[type]}, try server.wl.Resource.create(_client, {[type]}, _version, _id)); + \\ return @as(*{[type]}, @ptrCast(try server.wl.Resource.create(_client, {[type]}, _version, _id))); \\}}pub fn destroy(_{[interface]}: *{[type]}) void {{ - \\ return @ptrCast(*server.wl.Resource, _{[interface]}).destroy(); + \\ return @as(*server.wl.Resource, @ptrCast(_{[interface]})).destroy(); \\}}pub fn fromLink(_link: *server.wl.list.Link) *{[type]} {{ - \\ return @ptrCast(*{[type]}, server.wl.Resource.fromLink(_link)); + \\ return @as(*{[type]}, @ptrCast(server.wl.Resource.fromLink(_link))); \\}} , .{ .type = titleCaseTrim(interface.name), @@ -688,7 +688,8 @@ const Interface = struct { }) |func| try writer.print( \\pub fn {[function]}(_{[interface]}: *{[type]}) {[return_type]} {{ - \\ return @ptrCast(*server.wl.Resource, _{[interface]}).{[function]}(); + \\ const resource: *server.wl.Resource = @ptrCast(_{[interface]}); + \\ return resource.{[function]}(); \\}} , .{ .function = camelCase(func[0]), @@ -703,7 +704,8 @@ const Interface = struct { if (has_error) { try writer.print( \\pub fn postError({[interface]}: *{[type]}, _err: Error, _message: [*:0]const u8) void {{ - \\ return @ptrCast(*server.wl.Resource, {[interface]}).postError(@intCast(u32, @enumToInt(_err)), _message); + \\ const resource: *server.wl.Resource = @ptrCast({[interface]}); + \\ return resource.postError(@intCast(@intFromEnum(_err)), _message); \\}} , .{ .interface = fmtId(trimPrefix(interface.name)), @@ -732,16 +734,16 @@ const Interface = struct { \\ comptime handle_destroy: ?fn (_{[interface]}: *{[type]}, data: T) void, \\ _data: T, \\) void {{ - \\ const _resource = @ptrCast(*server.wl.Resource, _{[interface]}); + \\ const _resource: *server.wl.Resource = @ptrCast(_{[interface]}); \\ _resource.setDispatcher( \\ common.Dispatcher({[type]}, T).dispatcher, \\ handle_request, - \\ @intToPtr(?*anyopaque, @ptrToInt(_data)), + \\ @as(?*anyopaque, @ptrFromInt(@intFromPtr(_data))), \\ if (handle_destroy) |_handler| struct {{ \\ fn _wrapper(__resource: *server.wl.Resource) callconv(.C) void {{ \\ @call(.always_inline, _handler, .{{ - \\ @ptrCast(*{[type]}, __resource), - \\ @intToPtr(T, @ptrToInt(__resource.getUserData())), + \\ @as(*{[type]}, @ptrCast(__resource)), + \\ @as(T, @ptrFromInt(@intFromPtr(__resource.getUserData()))), \\ }}); \\ }} \\ }}._wrapper else null, @@ -759,16 +761,16 @@ const Interface = struct { \\ comptime handle_destroy: ?fn (_{[interface]}: *{[type]}, data: T) void, \\ _data: T, \\) void {{ - \\ const _resource = @ptrCast(*server.wl.Resource, _{[interface]}); + \\ const _resource: *server.wl.Resource = @ptrCast(_{[interface]}); \\ _resource.setDispatcher( \\ null, \\ null, - \\ @intToPtr(?*anyopaque, @ptrToInt(_data)), + \\ @as(?*anyopaque, @intFromPtr(@intFromPtr(_data))), \\ if (handle_destroy) |_handler| struct {{ \\ fn _wrapper(__resource: *server.wl.Resource) callconv(.C) void {{ \\ @call(.always_inline, _handler, .{{ - \\ @ptrCast(*{[type]}, __resource), - \\ @intToPtr(T, @ptrToInt(__resource.getUserData())), + \\ @as(*{[type]}, @ptrCast(__resource)), + \\ @as(T, @ptrFromInt(@intFromPtr(__resource.getUserData))), \\ }}); \\ }} \\ }}._wrapper else null, @@ -938,13 +940,13 @@ const Message = struct { try writer.writeAll(") !*T {"); } if (side == .server) { - try writer.writeAll("const _resource = @ptrCast(*server.wl.Resource,_"); + try writer.writeAll("const _resource: *server.wl.Resource = @ptrCast(_"); } else { // wl_registry.bind for example needs special handling if (message.kind == .constructor and message.kind.constructor == null) { - try writer.writeAll("const version_to_construct = std.math.min(T.generated_version, _version);"); + try writer.writeAll("const version_to_construct = @min(T.generated_version, _version);"); } - try writer.writeAll("const _proxy = @ptrCast(*client.wl.Proxy,_"); + try writer.writeAll("const _proxy: *client.wl.Proxy = @ptrCast(_"); } try writer.print("{});", .{fmtId(trimPrefix(interface.name))}); if (message.args.len > 0) { @@ -964,8 +966,8 @@ const Message = struct { const c_type = if (arg.kind == .uint) "u32" else "i32"; try writer.print( \\ )) {{ - \\ .Enum => @intCast({[ct]s}, @enumToInt(_{[an]})), - \\ .Struct => @bitCast(u32, _{[an]}), + \\ .Enum => @as({[ct]s}, @intCast(@intFromEnum(_{[an]}))), + \\ .Struct => @as(u32, @bitCast(_{[an]})), \\ else => unreachable, \\ }} , .{ .ct = c_type, .an = fmtId(arg.name) }); @@ -977,11 +979,11 @@ const Message = struct { .object, .new_id => |new_iface| { if (arg.kind == .object or side == .server) { if (arg.allow_null) { - try writer.writeAll(".{ .o = @ptrCast(?*common.Object, _"); + try writer.writeAll(".{ .o = @as(?*common.Object, @ptrCast(_"); } else { - try writer.writeAll(".{ .o = @ptrCast(*common.Object, _"); + try writer.writeAll(".{ .o = @as(*common.Object, @ptrCast(_"); } - try writer.print("{s}) }},", .{arg.name}); + try writer.print("{s})) }},", .{arg.name}); } else { if (new_iface == null) { try writer.writeAll( @@ -1007,14 +1009,14 @@ const Message = struct { }, .constructor => |new_iface| { if (new_iface) |i| { - try writer.writeAll("return @ptrCast(*"); + try writer.writeAll("return @as(*"); try printAbsolute(side, writer, i); - try writer.print(", try _proxy.marshalConstructor({}, &_args, ", .{opcode}); + try writer.print(", @ptrCast(try _proxy.marshalConstructor({}, &_args, ", .{opcode}); try printAbsolute(side, writer, i); - try writer.writeAll(".getInterface()));"); + try writer.writeAll(".getInterface())));"); } else { try writer.print( - \\return @ptrCast(*T, try _proxy.marshalConstructorVersioned({[opcode]}, &_args, T.getInterface(), version_to_construct)); + \\return @as(*T, @ptrCast(try _proxy.marshalConstructorVersioned({[opcode]}, &_args, T.getInterface(), version_to_construct))); , .{ .opcode = opcode, }); diff --git a/src/wayland_client_core.zig b/src/wayland_client_core.zig index 9fdbc88..8479f32 100644 --- a/src/wayland_client_core.zig +++ b/src/wayland_client_core.zig @@ -119,7 +119,7 @@ pub const EglWindow = opaque { pub const CursorTheme = opaque { extern fn wl_cursor_theme_load(name: ?[*:0]const u8, size: c_int, shm: *client.wl.Shm) ?*CursorTheme; pub fn load(name: ?[*:0]const u8, size: i32, shm: *client.wl.Shm) error{LoadThemeFailed}!*CursorTheme { - return wl_cursor_theme_load(name, @intCast(c_int, size), shm) orelse error.LoadThemeFailed; + return wl_cursor_theme_load(name, @as(c_int, @intCast(size)), shm) orelse error.LoadThemeFailed; } extern fn wl_cursor_theme_destroy(wl_cursor_theme: *CursorTheme) void; diff --git a/src/wayland_server_core.zig b/src/wayland_server_core.zig index fa90645..98b3446 100644 --- a/src/wayland_server_core.zig +++ b/src/wayland_server_core.zig @@ -95,7 +95,7 @@ pub const Server = opaque { server, struct { fn wrapper(_client: *const Client, _global: *const Global, _data: ?*anyopaque) callconv(.C) bool { - filter(_client, _global, @ptrCast(T, @alignCast(@alignOf(T), _data))); + filter(_client, _global, @as(T, @ptrCast(@alignCast(_data)))); } }._wrapper, data, @@ -130,7 +130,7 @@ pub const Server = opaque { server, struct { fn _wrapper(_data: ?*anyopaque, _direction: ProtocolLogger.Type, _message: *const ProtocolLogger.LogMessage) callconv(.C) void { - func(@ptrCast(T, @alignCast(@alignOf(T), _data)), _direction, _message); + func(@as(T, @ptrCast(@alignCast(_data))), _direction, _message); } }, data, @@ -201,7 +201,7 @@ pub const Client = opaque { client, struct { fn _wrapper(_resource: *Resource, _data: ?*anyopaque) callconv(.C) IteratorResult { - return iterator(_resource, @ptrCast(T, @alignCast(@alignOf(T), _data))); + return iterator(_resource, @as(T, @ptrCast(@alignCast(_data)))); } }._wrapper, data, @@ -234,11 +234,11 @@ pub const Global = opaque { return wl_global_create( server, T.getInterface(), - @intCast(c_int, version), + @as(c_int, @intCast(version)), data, struct { fn _wrapper(_client: *Client, _data: ?*anyopaque, _version: u32, _id: u32) callconv(.C) void { - bind(_client, @ptrCast(DataT, @alignCast(@alignOf(DataT), _data)), _version, _id); + bind(_client, @as(DataT, @ptrCast(@alignCast(_data))), _version, _id); } }._wrapper, ) orelse error.GlobalCreateFailed; @@ -262,7 +262,7 @@ pub const Resource = opaque { pub inline fn create(client: *Client, comptime T: type, version: u32, id: u32) error{ResourceCreateFailed}!*Resource { // This is only a c_int because of legacy libwayland reasons. Negative versions are invalid. // Version is a u32 on the wire and for wl_global, wl_proxy, etc. - return wl_resource_create(client, T.getInterface(), @intCast(c_int, version), id) orelse error.ResourceCreateFailed; + return wl_resource_create(client, T.getInterface(), @as(c_int, @intCast(version)), id) orelse error.ResourceCreateFailed; } extern fn wl_resource_destroy(resource: *Resource) void; @@ -328,7 +328,7 @@ pub const Resource = opaque { // The fact that wl_resource.version is a int in libwayland is // a mistake. Negative versions are impossible and u32 is used // everywhere else in libwayland - return @intCast(u32, wl_resource_get_version(resource)); + return @as(u32, @intCast(wl_resource_get_version(resource))); } // TOOD: unsure if this should be bound @@ -556,7 +556,7 @@ pub fn Listener(comptime T: type) type { else struct { fn wrapper(listener: *Self, data: ?*anyopaque) callconv(.C) void { - @call(.always_inline, notify, .{ listener, @intToPtr(T, @ptrToInt(data)) }); + @call(.always_inline, notify, .{ listener, @as(T, @ptrFromInt(@intFromPtr(data))) }); } }.wrapper; } @@ -653,7 +653,7 @@ pub const EventLoop = opaque { mask, struct { fn _wrapper(_fd: c_int, _mask: u32, _data: ?*anyopaque) callconv(.C) c_int { - return func(_fd, _mask, @ptrCast(T, @alignCast(@alignOf(T), _data))); + return func(_fd, _mask, @as(T, @ptrCast(@alignCast(_data)))); } }._wrapper, data, @@ -675,7 +675,7 @@ pub const EventLoop = opaque { loop, struct { fn _wrapper(_data: ?*anyopaque) callconv(.C) c_int { - return func(@ptrCast(T, @alignCast(@alignOf(T), _data))); + return func(@as(T, @ptrCast(@alignCast(_data)))); } }._wrapper, data, @@ -700,7 +700,7 @@ pub const EventLoop = opaque { signal_number, struct { fn _wrapper(_signal_number: c_int, _data: ?*anyopaque) callconv(.C) c_int { - return func(_signal_number, @ptrCast(T, @alignCast(@alignOf(T), _data))); + return func(_signal_number, @as(T, @ptrCast(@alignCast(_data)))); } }._wrapper, data, @@ -722,7 +722,7 @@ pub const EventLoop = opaque { loop, struct { fn _wrapper(_data: ?*anyopaque) callconv(.C) void { - return func(@ptrCast(T, @alignCast(@alignOf(T), _data))); + return func(@as(T, @ptrCast(@alignCast(_data)))); } }._wrapper, data, diff --git a/src/xml.zig b/src/xml.zig index fc3b68c..fa327fc 100644 --- a/src/xml.zig +++ b/src/xml.zig @@ -197,7 +197,7 @@ pub const Parser = struct { if (parseEntity(p.document[1..], &p.char_buffer)) |semi| { const codepoint = mem.bytesToValue(u32, &p.char_buffer); - const n = std.unicode.utf8Encode(@intCast(u21, codepoint), &p.char_buffer) catch return null; + const n = std.unicode.utf8Encode(@intCast(codepoint), &p.char_buffer) catch return null; p.document = p.document[semi + 2 ..]; p.mode = .chars; return Event{ .character_data = p.char_buffer[0..n] }; @@ -294,7 +294,7 @@ pub const Attribute = struct { if (attr.raw_value[0] == '&') { if (Parser.parseEntity(attr.raw_value[1..], &attr.char_buffer)) |semi| { const codepoint = mem.bytesToValue(u32, &attr.char_buffer); - const n = std.unicode.utf8Encode(@intCast(u21, codepoint), &attr.char_buffer) catch return null; + const n = std.unicode.utf8Encode(@as(u21, @intCast(codepoint)), &attr.char_buffer) catch return null; attr.raw_value = attr.raw_value[semi + 2 ..]; return attr.char_buffer[0..n]; } else {