Skip to content

Commit 7ea9c20

Browse files
committed
add From<Ip*Addr> implementations for cidr and inet types
1 parent 3e71f82 commit 7ea9c20

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

src/cidr/any.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use core::{
22
fmt,
33
str::FromStr,
44
};
5-
use std::net::IpAddr;
5+
use std::net::{
6+
IpAddr,
7+
Ipv4Addr,
8+
Ipv6Addr,
9+
};
610

711
use crate::{
812
errors::*,
@@ -266,12 +270,30 @@ impl From<Ipv4Cidr> for AnyIpCidr {
266270
}
267271
}
268272

273+
impl From<Ipv4Addr> for AnyIpCidr {
274+
fn from(address: Ipv4Addr) -> Self {
275+
Self::V4(address.into())
276+
}
277+
}
278+
269279
impl From<Ipv6Cidr> for AnyIpCidr {
270280
fn from(c: Ipv6Cidr) -> Self {
271281
Self::V6(c)
272282
}
273283
}
274284

285+
impl From<Ipv6Addr> for AnyIpCidr {
286+
fn from(address: Ipv6Addr) -> Self {
287+
Self::V6(address.into())
288+
}
289+
}
290+
291+
impl From<IpAddr> for AnyIpCidr {
292+
fn from(address: IpAddr) -> Self {
293+
Self::new_host(address)
294+
}
295+
}
296+
275297
#[cfg(feature = "bitstring")]
276298
#[cfg_attr(doc_cfg, doc(cfg(feature = "bitstring")))]
277299
impl bitstring::BitString for AnyIpCidr {

src/cidr/combined.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use core::{
22
fmt,
33
str::FromStr,
44
};
5-
use std::net::IpAddr;
5+
use std::net::{
6+
IpAddr,
7+
Ipv4Addr,
8+
Ipv6Addr,
9+
};
610

711
use super::from_str::cidr_from_str;
812
use crate::{
@@ -237,12 +241,30 @@ impl From<Ipv4Cidr> for IpCidr {
237241
}
238242
}
239243

244+
impl From<Ipv4Addr> for IpCidr {
245+
fn from(address: Ipv4Addr) -> Self {
246+
Self::V4(address.into())
247+
}
248+
}
249+
240250
impl From<Ipv6Cidr> for IpCidr {
241251
fn from(c: Ipv6Cidr) -> Self {
242252
Self::V6(c)
243253
}
244254
}
245255

256+
impl From<Ipv6Addr> for IpCidr {
257+
fn from(address: Ipv6Addr) -> Self {
258+
Self::V6(address.into())
259+
}
260+
}
261+
262+
impl From<IpAddr> for IpCidr {
263+
fn from(address: IpAddr) -> Self {
264+
Self::new_host(address)
265+
}
266+
}
267+
246268
impl IntoIterator for IpCidr {
247269
type IntoIter = InetIterator<IpAddr>;
248270
type Item = IpInet;

src/cidr/direct.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ macro_rules! impl_cidr_for {
278278
}
279279
}
280280

281+
impl From<$addr> for $n {
282+
fn from(address: $addr) -> Self {
283+
Self::new_host(address)
284+
}
285+
}
286+
281287
/// Iterate over all the addresses in the CIDR.
282288
impl IntoIterator for $n {
283289
type IntoIter = $crate::InetIterator<$addr>;

src/inet/combined.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use core::{
22
fmt,
33
str::FromStr,
44
};
5-
use std::net::IpAddr;
5+
use std::net::{
6+
IpAddr,
7+
Ipv4Addr,
8+
Ipv6Addr,
9+
};
610

711
use super::from_str::inet_from_str;
812
use crate::{
@@ -342,12 +346,30 @@ impl From<Ipv4Inet> for IpInet {
342346
}
343347
}
344348

349+
impl From<Ipv4Addr> for IpInet {
350+
fn from(adress: Ipv4Addr) -> Self {
351+
Self::V4(adress.into())
352+
}
353+
}
354+
345355
impl From<Ipv6Inet> for IpInet {
346356
fn from(c: Ipv6Inet) -> Self {
347357
Self::V6(c)
348358
}
349359
}
350360

361+
impl From<Ipv6Addr> for IpInet {
362+
fn from(address: Ipv6Addr) -> Self {
363+
Self::V6(address.into())
364+
}
365+
}
366+
367+
impl From<IpAddr> for IpInet {
368+
fn from(address: IpAddr) -> Self {
369+
Self::new_host(address)
370+
}
371+
}
372+
351373
impl core::ops::Add<u128> for IpInet {
352374
type Output = IpInet;
353375

src/inet/direct.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ macro_rules! impl_inet_for {
375375
}
376376
}
377377

378+
impl From<$addr> for $n {
379+
fn from(address: $addr) -> Self {
380+
Self::new_host(address)
381+
}
382+
}
383+
378384
impl core::ops::Add<u128> for $n {
379385
type Output = $n;
380386

0 commit comments

Comments
 (0)