Skip to content

Commit

Permalink
impl AsIpv4Addrs for reference types (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 authored May 17, 2022
1 parent 411e9bc commit 7c53e87
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,12 @@ pub trait AsIpv4Addrs {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>>;
}

impl<T: AsIpv4Addrs> AsIpv4Addrs for &T {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>> {
(*self).as_ipv4_addrs()
}
}

impl AsIpv4Addrs for &str {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>> {
let mut addrs = HashSet::new();
Expand Down
15 changes: 12 additions & 3 deletions tests/addr_parse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashSet;
use std::iter::FromIterator;

use mdns_sd::AsIpv4Addrs;
use nix::sys::socket::Ipv4Addr;
use std::collections::HashSet;

#[test]
fn test_addr_str() {
Expand All @@ -27,6 +25,17 @@ fn test_addr_str() {
})
);

// verify that `&String` also works.
assert_eq!(
(&addr).as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));

set
})
);

assert_eq!(
"127.0.0.1,127.0.0.2".as_ipv4_addrs(),
Ok({
Expand Down

0 comments on commit 7c53e87

Please sign in to comment.