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

updated to zig master #50

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion example/seats.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 11 additions & 11 deletions src/common_core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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))];
}
};
Expand All @@ -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))));
}
};

Expand Down Expand Up @@ -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;
Expand Down
62 changes: 32 additions & 30 deletions src/scanner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
});
Expand All @@ -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);
\\}}
, .{
Expand All @@ -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);
\\}}
, .{
Expand All @@ -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();
\\}}
, .{
Expand All @@ -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),
Expand All @@ -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]),
Expand All @@ -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)),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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) });
Expand All @@ -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(
Expand All @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion src/wayland_client_core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions src/wayland_server_core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading