Skip to content

Commit

Permalink
Merge pull request #157 from jyn514/ip-addr
Browse files Browse the repository at this point in the history
impl Arbitrary for IpAddr
  • Loading branch information
Manishearth authored Jun 29, 2023
2 parents 0bdbec8 + 7d3364e commit 80d6bfe
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 80d6bfe

Please sign in to comment.