Skip to content

Commit 80d6bfe

Browse files
authored
Merge pull request #157 from jyn514/ip-addr
impl Arbitrary for IpAddr
2 parents 0bdbec8 + 7d3364e commit 80d6bfe

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::borrow::{Cow, ToOwned};
4747
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
4848
use std::ffi::{CString, OsString};
4949
use std::hash::BuildHasher;
50-
use std::net::{Ipv4Addr, Ipv6Addr};
50+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
5151
use std::ops::Bound;
5252
use std::path::PathBuf;
5353
use std::rc::Rc;
@@ -1140,6 +1140,23 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
11401140
}
11411141
}
11421142

1143+
impl<'a> Arbitrary<'a> for IpAddr {
1144+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
1145+
if u.arbitrary()? {
1146+
Ok(IpAddr::V4(u.arbitrary()?))
1147+
} else {
1148+
Ok(IpAddr::V6(u.arbitrary()?))
1149+
}
1150+
}
1151+
1152+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
1153+
size_hint::and(
1154+
bool::size_hint(depth),
1155+
size_hint::or(Ipv4Addr::size_hint(depth), Ipv6Addr::size_hint(depth)),
1156+
)
1157+
}
1158+
}
1159+
11431160
#[cfg(test)]
11441161
mod test {
11451162
use super::*;

0 commit comments

Comments
 (0)