Is there a better way to express the bool adding / counting math I need to do here?
pub const DLStatus = packed struct {
dls_user_operational: bool,
dls_user_watchdog_ok: bool,
extended_link_detection: bool,
_reserved: u1 = 0,
port0_physical_link: bool,
port1_physical_link: bool,
port2_physical_link: bool,
port3_physical_link: bool,
port0_loop_internal: bool,
port0_rx_signal_detected: bool,
port1_loop_internal: bool,
port1_rx_signal_detected: bool,
port2_loop_internal: bool,
port2_rx_signal_detected: bool,
port3_loop_internal: bool,
port3_rx_signal_detected: bool,
pub fn topology(self: DLStatus) Topology {
const port0_active = self.port0_rx_signal_detected and !self.port0_loop_internal;
const port1_active = self.port1_rx_signal_detected and !self.port1_loop_internal;
const port2_active = self.port2_rx_signal_detected and !self.port2_loop_internal;
const port3_active = self.port3_rx_signal_detected and !self.port3_loop_internal;
const n_active_ports: u3 =
@as(u3, @intFromBool(port0_active)) +
@as(u3, @intFromBool(port1_active)) +
@as(u3, @intFromBool(port2_active)) +
@as(u3, @intFromBool(port3_active));
return switch (n_active_ports) {
0 => return .end,
1 => return .end,
2 => return .line,
3 => return .wye,
4 => return .cross,
else => unreachable,
};
}
};
pub const Topology = enum {
end,
line,
wye,
cross,
};
I would prefer comptime unreachable over runtime unreachable.