Skip to content

Commit

Permalink
impl Arbitrary for IpAddr
Browse files Browse the repository at this point in the history
This complements the existing impls for Ipv4Addr and Ipv6Addr.
  • Loading branch information
jyn514 committed Jun 29, 2023
1 parent 0bdbec8 commit 7d3364e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use std::borrow::{Cow, ToOwned};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::ffi::{CString, OsString};
use std::hash::BuildHasher;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ops::Bound;
use std::path::PathBuf;
use std::rc::Rc;
Expand Down Expand Up @@ -1140,6 +1140,23 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
}
}

impl<'a> Arbitrary<'a> for IpAddr {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
if u.arbitrary()? {
Ok(IpAddr::V4(u.arbitrary()?))
} else {
Ok(IpAddr::V6(u.arbitrary()?))
}
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
size_hint::and(
bool::size_hint(depth),
size_hint::or(Ipv4Addr::size_hint(depth), Ipv6Addr::size_hint(depth)),
)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 7d3364e

Please sign in to comment.