Switching on POSIX signals

I’m trying to do a “full-coverage” of signals by setting the ones I don’t care to ignore and registering the rest to a self-pipe for later processing in an event-loop.

To make sure I’m indeed covering all possible signals, I considered using a switch on the std.posix.SIG/std.c.SIG enums:

switch (std.posix.SIG) {
    .INT => {},
    //...
}

Unfortunately, this doesn’t seem to be working. Any ideas?

If you want to switch on a value, you need an actual value to switch on. Right now, you’re switching on the type std.postix.SIG. What you would do:

const signal: std.posix.SIG = functionThatReturnsASignal();
//                ^ The type             ^ Getting a value          
switch (signal) {
    .INT => { ... },
    .TRAP => { ... },
    else => { ... },
}

Does it need to be a value? I actually want to switch on the enum itself, which I believe is also possible.

Well, when switching on the type what do you want to happen. You’re defining a bunch of clauses with no way to choose one of them.

If my intuition is correct you want to do something for every signal. In that case you can do:

const values: []const std.posix.SIG = std.enums.values(std.posix.SIG); // Get all the values
for (values) |value| {
    switch (value) {
        .INT => { ... },
        else => { ... },
    }
}

If my intuition is incorrect, it may help to explain what you’re trying to do.

1 Like

That’s it! I confused myself :person_facepalming:. Thanks.