mod(dispatch): use anyopague instead of usize for pointer handling
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Has been cancelled

This commit is contained in:
2024-11-16 19:08:22 +01:00
parent 7c9038fbda
commit 4ef9e077cb
2 changed files with 10 additions and 12 deletions

View File

@@ -25,7 +25,6 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
layout: LayoutType,
widget: @import("widget.zig").Widget(Event, Renderer),
};
const Ptr = usize;
pub const Interface = @import("interface").Interface(.{
.handle = fn (anytype, Event) anyerror!*Events,
.render = fn (anytype, *Renderer) anyerror!void,
@@ -38,7 +37,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
deinit: *const fn (this: *LayoutType) void,
};
object: Ptr = undefined,
object: *anyopaque = undefined,
vtable: *const VTable = undefined,
// Handle the provided `Event` for this `Layout`.
@@ -58,25 +57,25 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
pub fn createFrom(object: anytype) LayoutType {
return LayoutType{
.object = @intFromPtr(object),
.object = @ptrCast(@alignCast(object)),
.vtable = &.{
.handle = struct {
// Handle the provided `Event` for this `Layout`.
fn handle(this: *LayoutType, event: Event) !*Events {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
const layout: @TypeOf(object) = @ptrCast(@alignCast(this.object));
return try layout.handle(event);
}
}.handle,
.render = struct {
// Render the contents of this `Layout`.
fn render(this: *LayoutType, renderer: *Renderer) !void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
const layout: @TypeOf(object) = @ptrCast(@alignCast(this.object));
try layout.render(renderer);
}
}.render,
.deinit = struct {
fn deinit(this: *LayoutType) void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
const layout: @TypeOf(object) = @ptrCast(@alignCast(this.object));
layout.deinit();
}
}.deinit,

View File

@@ -20,7 +20,6 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
const Type = struct {
const WidgetType = @This();
const Ptr = usize;
pub const Interface = @import("interface").Interface(.{
.handle = fn (anytype, Event) ?Event,
.render = fn (anytype, *Renderer) anyerror!void,
@@ -33,7 +32,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
deinit: *const fn (this: *WidgetType) void,
};
object: Ptr = undefined,
object: *anyopaque = undefined,
vtable: *const VTable = undefined,
// Handle the provided `Event` for this `Widget`.
@@ -64,25 +63,25 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
pub fn createFrom(object: anytype) WidgetType {
return WidgetType{
.object = @intFromPtr(object),
.object = @ptrCast(@alignCast(object)),
.vtable = &.{
.handle = struct {
// Handle the provided `Event` for this `Widget`.
fn handle(this: *WidgetType, event: Event) ?Event {
const widget: @TypeOf(object) = @ptrFromInt(this.object);
const widget: @TypeOf(object) = @ptrCast(@alignCast(this.object));
return widget.handle(event);
}
}.handle,
.render = struct {
// Return the entire content of this `Widget`.
fn render(this: *WidgetType, renderer: *Renderer) !void {
const widget: @TypeOf(object) = @ptrFromInt(this.object);
const widget: @TypeOf(object) = @ptrCast(@alignCast(this.object));
try widget.render(renderer);
}
}.render,
.deinit = struct {
fn deinit(this: *WidgetType) void {
const widget: @TypeOf(object) = @ptrFromInt(this.object);
const widget: @TypeOf(object) = @ptrCast(@alignCast(this.object));
widget.deinit();
}
}.deinit,