Thanks @AndrewCodeDev
Maybe I’m too new to Zig, still can’t figure out the way using const declaration to implement optional public method like this zbpf/src/bpf/args.zig at d2a0d46526670132fa93fe665364720739935650 · tw4452852/zbpf · GitHub
Hope you could help me here, thanks in advance!
Moved from another topic.
dimdin
3
Using the recipe from proposal use case “Conditional Inclusion”.
pub usingnamespace if (f.@"fn".params.len < 1) struct {} else struct {
const RET = f.@"fn".params[0].type.?;
pub fn arg0(self: *Self) RET {
if (!in_bpf_program) {
return cast(RET, self.get_regs().arg0_ptr().*);
} else {
var ret: RET = undefined;
const err = helpers.probe_read_kernel(@ptrCast(&ret), @sizeOf(RET), self.get_regs().arg0_ptr());
if (err != 0) printErr(@src(), err);
return ret;
}
}
};
becomes:
pub const arg0 = if (f.@"fn".params.len < 1) @compileError("arg0 not supported") else struct {
const RET = f.@"fn".params[0].type.?;
pub fn arg0(self: *Self) RET {
if (!in_bpf_program) {
return cast(RET, self.get_regs().arg0_ptr().*);
} else {
var ret: RET = undefined;
const err = helpers.probe_read_kernel(@ptrCast(&ret), @sizeOf(RET), self.get_regs().arg0_ptr());
if (err != 0) printErr(@src(), err);
return ret;
}
}
}.arg0;
1 Like
Ah, I got the point, thanks @dimdin