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

View File

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