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

Add IpNetwork::with_netmask_checked & Ipv{4,6}Network::with_netmask_checked #216

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions src/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ impl Ipv4Network {
Ok(net)
}

/// Constructs a new `Ipv4Network` from a network address and a network mask.
///
/// If the netmask is not valid this will return a `None`.
pub const fn with_netmask_checked(netaddr: Ipv4Addr, netmask: Ipv4Addr) -> Option<Ipv4Network> {
let Some(prefix) = ipv4_mask_to_prefix_checked(netmask) else {
return None;
};
let net = Self {
addr: netaddr,
prefix,
};
Some(net)
}

/// Returns an iterator over `Ipv4Network`. Each call to `next` will return the next
/// `Ipv4Addr` in the given network. `None` will be returned when there are no more
/// addresses.
Expand Down
14 changes: 14 additions & 0 deletions src/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ impl Ipv6Network {
Ok(net)
}

/// Constructs a new `Ipv6Network` from a network address and a network mask.
///
/// If the netmask is not valid this will return a `None`.
pub const fn with_netmask_checked(netaddr: Ipv6Addr, netmask: Ipv6Addr) -> Option<Self> {
let Some(prefix) = ipv6_mask_to_prefix_checked(netmask) else {
return None;
};
let net = Self {
addr: netaddr,
prefix,
};
Some(net)
}

/// Returns an iterator over `Ipv6Network`. Each call to `next` will return the next
/// `Ipv6Addr` in the given network. `None` will be returned when there are no more
/// addresses.
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ impl IpNetwork {
Self::new(netaddr, prefix)
}

/// Constructs a new `IpNetwork` from a network address and a network mask.
///
/// If the netmask is not valid this will return an `None`.
pub const fn with_netmask_checked(netaddr: IpAddr, netmask: IpAddr) -> Option<Self> {
let Some(prefix) = ip_mask_to_prefix_checked(netmask) else {
return None;
};

match netaddr {
IpAddr::V4(address) => {
let Some(network) = Ipv4Network::new_checked(address, prefix) else {
return None;
};
Some(IpNetwork::V4(network))
}
IpAddr::V6(address) => {
let Some(network) = Ipv6Network::new_checked(address, prefix) else {
return None;
};
Some(IpNetwork::V6(network))
}
}
}

/// Returns the IP part of a given `IpNetwork`
pub const fn ip(&self) -> IpAddr {
match *self {
Expand Down
Loading