refactor(relay): use zero check for is_learned (#10209)

Simplifies the interface map we store to use a zero-check instead of
explicit bool.

Related:
https://github.com/firezone/firezone/pull/10200#discussion_r2281117072
This commit is contained in:
Jamil
2025-08-18 17:05:45 -04:00
committed by GitHub
parent 0698e0d35f
commit 618254cdfc
2 changed files with 8 additions and 16 deletions

View File

@@ -208,26 +208,22 @@ impl Default for Config {
#[cfg_attr(feature = "std", derive(Debug))]
pub struct InterfaceAddressV4 {
address: [u8; 4],
learned: bool,
}
impl InterfaceAddressV4 {
const ZERO: [u8; 4] = [0; 4];
pub fn set(&mut self, addr: Ipv4Addr) {
self.address = addr.octets();
self.learned = true;
}
pub fn get(&self) -> Option<Ipv4Addr> {
if self.learned {
if self.address != Self::ZERO {
Some(self.address.into())
} else {
None
}
}
pub fn is_learned(&self) -> bool {
self.learned
}
}
#[repr(C)]
@@ -235,26 +231,22 @@ impl InterfaceAddressV4 {
#[cfg_attr(feature = "std", derive(Debug))]
pub struct InterfaceAddressV6 {
address: [u8; 16],
learned: bool,
}
impl InterfaceAddressV6 {
const ZERO: [u8; 16] = [0; 16];
pub fn set(&mut self, addr: Ipv6Addr) {
self.address = addr.octets();
self.learned = true;
}
pub fn get(&self) -> Option<Ipv6Addr> {
if self.learned {
if self.address != Self::ZERO {
Some(self.address.into())
} else {
None
}
}
pub fn is_learned(&self) -> bool {
self.learned
}
}
#[repr(C)]

View File

@@ -405,7 +405,7 @@ fn learn_interface_ipv4_address(ipv4: &Ip4) -> Result<(), Error> {
// SAFETY: These are per-cpu maps so we don't need to worry about thread safety.
unsafe {
if !(*interface_addr).is_learned() {
if (*interface_addr).get().is_none() {
(*interface_addr).set(dst_ip);
}
}
@@ -423,7 +423,7 @@ fn learn_interface_ipv6_address(ipv6: &Ip6) -> Result<(), Error> {
// SAFETY: These are per-cpu maps so we don't need to worry about thread safety.
unsafe {
if !(*interface_addr).is_learned() {
if (*interface_addr).get().is_none() {
(*interface_addr).set(dst_ip);
}
}