1 use { 2 crate::{size_hint, Arbitrary, Result, Unstructured}, 3 std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}, 4 }; 5 6 impl<'a> Arbitrary<'a> for Ipv4Addr { arbitrary(u: &mut Unstructured<'a>) -> Result<Self>7 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { 8 Ok(Ipv4Addr::from(u32::arbitrary(u)?)) 9 } 10 11 #[inline] size_hint(_depth: usize) -> (usize, Option<usize>)12 fn size_hint(_depth: usize) -> (usize, Option<usize>) { 13 (4, Some(4)) 14 } 15 } 16 17 impl<'a> Arbitrary<'a> for Ipv6Addr { arbitrary(u: &mut Unstructured<'a>) -> Result<Self>18 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { 19 Ok(Ipv6Addr::from(u128::arbitrary(u)?)) 20 } 21 22 #[inline] size_hint(_depth: usize) -> (usize, Option<usize>)23 fn size_hint(_depth: usize) -> (usize, Option<usize>) { 24 (16, Some(16)) 25 } 26 } 27 28 impl<'a> Arbitrary<'a> for IpAddr { arbitrary(u: &mut Unstructured<'a>) -> Result<Self>29 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { 30 if u.arbitrary()? { 31 Ok(IpAddr::V4(u.arbitrary()?)) 32 } else { 33 Ok(IpAddr::V6(u.arbitrary()?)) 34 } 35 } 36 size_hint(depth: usize) -> (usize, Option<usize>)37 fn size_hint(depth: usize) -> (usize, Option<usize>) { 38 size_hint::and( 39 bool::size_hint(depth), 40 size_hint::or(Ipv4Addr::size_hint(depth), Ipv6Addr::size_hint(depth)), 41 ) 42 } 43 } 44 45 impl<'a> Arbitrary<'a> for SocketAddrV4 { arbitrary(u: &mut Unstructured<'a>) -> Result<Self>46 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { 47 Ok(SocketAddrV4::new(u.arbitrary()?, u.arbitrary()?)) 48 } 49 50 #[inline] size_hint(depth: usize) -> (usize, Option<usize>)51 fn size_hint(depth: usize) -> (usize, Option<usize>) { 52 size_hint::and(Ipv4Addr::size_hint(depth), u16::size_hint(depth)) 53 } 54 } 55 56 impl<'a> Arbitrary<'a> for SocketAddrV6 { arbitrary(u: &mut Unstructured<'a>) -> Result<Self>57 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { 58 Ok(SocketAddrV6::new( 59 u.arbitrary()?, 60 u.arbitrary()?, 61 u.arbitrary()?, 62 u.arbitrary()?, 63 )) 64 } 65 66 #[inline] size_hint(depth: usize) -> (usize, Option<usize>)67 fn size_hint(depth: usize) -> (usize, Option<usize>) { 68 size_hint::and( 69 Ipv6Addr::size_hint(depth), 70 size_hint::and( 71 u16::size_hint(depth), 72 size_hint::and(u32::size_hint(depth), u32::size_hint(depth)), 73 ), 74 ) 75 } 76 } 77 78 impl<'a> Arbitrary<'a> for SocketAddr { arbitrary(u: &mut Unstructured<'a>) -> Result<Self>79 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { 80 if u.arbitrary()? { 81 Ok(SocketAddr::V4(u.arbitrary()?)) 82 } else { 83 Ok(SocketAddr::V6(u.arbitrary()?)) 84 } 85 } 86 size_hint(depth: usize) -> (usize, Option<usize>)87 fn size_hint(depth: usize) -> (usize, Option<usize>) { 88 size_hint::and( 89 bool::size_hint(depth), 90 size_hint::or( 91 SocketAddrV4::size_hint(depth), 92 SocketAddrV6::size_hint(depth), 93 ), 94 ) 95 } 96 } 97