Skip to content

Commit

Permalink
Add ip_mask_to_prefix_checked
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Jan 10, 2025
1 parent 2f9e0ac commit 887d62e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,17 @@ pub fn ip_mask_to_prefix(mask: IpAddr) -> Result<u8, IpNetworkError> {
}
}

/// Converts a `IpAddr` network mask into a prefix.
///
/// If the mask is invalid this will return `None`. This is useful in const contexts where
/// [`Option::unwrap`] may be called to trigger a compile-time error if the prefix is invalid.
pub const fn ip_mask_to_prefix_checked(mask: IpAddr) -> Option<u8> {
match mask {
IpAddr::V4(mask) => ipv4_mask_to_prefix_checked(mask),
IpAddr::V6(mask) => ipv6_mask_to_prefix_checked(mask),
}
}

#[cfg(test)]
mod test {
#[test]
Expand Down

0 comments on commit 887d62e

Please sign in to comment.