Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PortOffset::opposite #136

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading