Make the iterators slightly better

* Implement some useful traits on them.
* Make reference an into-iterator.
This commit is contained in:
Michal 'vorner' Vaner
2019-12-05 20:15:21 +01:00
parent 43a38d4117
commit f7d9524f70
2 changed files with 18 additions and 0 deletions

View File

@ -253,6 +253,7 @@ impl From<Ipv4Addr> for Ipv4Network {
}
}
#[derive(Clone, Debug)]
pub struct Ipv4NetworkIterator {
next: u32,
end: u32,
@ -272,6 +273,14 @@ impl Iterator for Ipv4NetworkIterator {
}
}
impl IntoIterator for &'_ Ipv4Network {
type IntoIter = Ipv4NetworkIterator;
type Item = Ipv4Addr;
fn into_iter(self) -> Ipv4NetworkIterator {
self.iter()
}
}
/// Converts a `Ipv4Addr` network mask into a prefix.
///
/// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`.

View File

@ -227,6 +227,7 @@ impl From<Ipv6Addr> for Ipv6Network {
}
}
#[derive(Clone, Debug)]
pub struct Ipv6NetworkIterator {
next: u128,
end: u128,
@ -246,6 +247,14 @@ impl Iterator for Ipv6NetworkIterator {
}
}
impl IntoIterator for &'_ Ipv6Network {
type IntoIter = Ipv6NetworkIterator;
type Item = Ipv6Addr;
fn into_iter(self) -> Ipv6NetworkIterator {
self.iter()
}
}
impl fmt::Display for Ipv6Network {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{}/{}", self.ip(), self.prefix())