Skip to content

Commit 9a9aa78

Browse files
deastoecathay4t
authored andcommitted
address_family: impl From<IpAddr> for AddressFamily
When writing AF-agnostic code it's convenient to get the appropriate AddressFamily variant for a given IpAddr. Since all platform variations of AddressFamily have Inet and Inet6 variants, we add the impl in a platform-agnostic manner. Signed-off-by: Duncan Eastoe <[email protected]>
1 parent a83f6ae commit 9a9aa78

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/address_family.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
use std::net::IpAddr;
4+
5+
use crate::AddressFamily;
6+
7+
impl From<IpAddr> for AddressFamily {
8+
fn from(v: IpAddr) -> Self {
9+
match v {
10+
IpAddr::V4(_) => AddressFamily::Inet,
11+
IpAddr::V6(_) => AddressFamily::Inet6,
12+
}
13+
}
14+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//! [rtnetlink_url]: https://docs.rs/rtnetlink
2424
2525
pub mod address;
26+
mod address_family;
2627
pub mod link;
2728
pub mod neighbour;
2829
pub mod neighbour_table;

0 commit comments

Comments
 (0)