add(zig-interface): dependency to check interface contracts at comptime
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 53s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 53s
The described interfaces for `Widget` and `Layout` are now defined and correspondingly checked at comptime.
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
//! Dynamic dispatch for widget implementations.
|
||||
//! Each widget should at least implement these functions:
|
||||
//! - handle(this: *@This(), event: Event) ?Event {}
|
||||
//! - render(this: *@This(), renderer: *Renderer) !void {}
|
||||
//! - deinit(this: *@This()) void {}
|
||||
//! Each `Widget` has to implement the `WidgetInterface`
|
||||
//!
|
||||
//! Create a `Widget` using `createFrom(object: anytype)` and use them through
|
||||
//! the defined interface. The widget will take care of calling the correct
|
||||
@@ -21,9 +18,14 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
|
||||
if (!isTaggedUnion(Event)) {
|
||||
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
||||
}
|
||||
return struct {
|
||||
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,
|
||||
.deinit = fn (anytype) void,
|
||||
}, .{});
|
||||
|
||||
const VTable = struct {
|
||||
handle: *const fn (this: *WidgetType, event: Event) ?Event,
|
||||
@@ -88,8 +90,13 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: import and export of `Widget` implementations (with corresponding initialization using `Event`)
|
||||
// import and export of `Widget` implementations
|
||||
pub const RawText = @import("widget/RawText.zig").Widget(Event, Renderer);
|
||||
pub const Spacer = @import("widget/Spacer.zig").Widget(Event, Renderer);
|
||||
};
|
||||
// test widget implementation satisfies the interface
|
||||
comptime Type.Interface.satisfiedBy(Type);
|
||||
comptime Type.Interface.satisfiedBy(Type.RawText);
|
||||
comptime Type.Interface.satisfiedBy(Type.Spacer);
|
||||
return Type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user