Skip to content

Commit 1877cd5

Browse files
add link_device
1 parent e3f2551 commit 1877cd5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/HttpClient_device.zig

+33
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,39 @@ pub const HttpDevice = struct {
448448
}
449449
self.runner_loop.store_tx_data(TXPkg.convert_type(.HTTP, Package{ .SMALL = req }), runner_inst) catch unreachable;
450450
}
451+
452+
pub fn link_device(self: *HttpDevice, runner: anytype) void {
453+
const info = @typeInfo(@TypeOf(runner));
454+
switch (info) {
455+
.pointer => |ptr| {
456+
const child_type = ptr.child;
457+
if (@hasField(child_type, "HTTP_device")) {
458+
const HTTP_device = &runner.HTTP_device;
459+
if (@TypeOf(HTTP_device.*) == ?*Device) {
460+
self.device.device_instance = @ptrCast(self);
461+
HTTP_device.* = &self.device;
462+
} else {
463+
@compileError("HTTP_device need to be a Device pointer");
464+
}
465+
} else {
466+
@compileError(std.fmt.comptimePrint("type {s} does not have field \"HTTP_device\"", .{@typeName(runner)}));
467+
}
468+
469+
if (@hasField(child_type, "runner_dev")) {
470+
if (@TypeOf(runner.runner_dev) == Runner) {
471+
self.runner_loop = &(runner.*.runner_dev);
472+
} else {
473+
@compileError("runner_dev need to be a Runner type");
474+
}
475+
} else {
476+
@compileError(std.fmt.comptimePrint("type {s} does not have field \"runner_dev\"", .{@typeName(runner)}));
477+
}
478+
},
479+
else => {
480+
@compileError("\"runner\" need to be a Pointer");
481+
},
482+
}
483+
}
451484
};
452485

453486
fn check_request(req: *const Request) HTTPError!void {

0 commit comments

Comments
 (0)