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 fdf0d33
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 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,20 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
}
}

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

fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(4, Some(16))
}
}

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

0 comments on commit fdf0d33

Please sign in to comment.