Skip to content

Commit

Permalink
feat: Add PortOffset::opposite (#136)
Browse files Browse the repository at this point in the history
I would find such a function a useful, straight-forward addition to the
`PortOffset` API.
  • Loading branch information
lmondada authored Jun 18, 2024
1 parent ab60561 commit 4831264
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ impl PortOffset {
PortOffset::Outgoing(offset) => offset as usize,
}
}

/// Returns the opposite port offset.
///
/// This maps `PortOffset::Incoming(x)` to `PortOffset::Outgoing(x)` and
/// vice versa.
#[inline(always)]
pub fn opposite(&self) -> Self {
match *self {
PortOffset::Incoming(idx) => PortOffset::Outgoing(idx),
PortOffset::Outgoing(idx) => PortOffset::Incoming(idx),
}
}
}

impl Default for PortOffset {
Expand All @@ -384,3 +396,17 @@ impl std::fmt::Debug for PortOffset {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_opposite() {
let incoming = PortOffset::Incoming(5);
let outgoing = PortOffset::Outgoing(5);

assert_eq!(incoming.opposite(), outgoing);
assert_eq!(outgoing.opposite(), incoming);
}
}

0 comments on commit 4831264

Please sign in to comment.