pub fn sub_networks(cidr: &Ipv6Cidr, bits: u8) -> Option<Vec<Ipv6Cidr>> {
        let cidr_bits = cidr.get_bits();

        if cidr_bits > bits {
            return None;
        } else if cidr_bits == bits {
            return Some(vec![cidr.clone()]);
        }

        let n = 2usize.pow(u32::from(bits - cidr_bits));

        let n_u128 = n as u128;

        let mut output = Vec::with_capacity(n);

        let size = cidr.size();

        let d = if size.1 {
            u128::max_value() / n_u128 + 1
        } else {
            size.0 / n_u128
        };

        let mut iter = cidr.iter();

        let usize_max_u128 = usize::max_value() as u128;

        if d <= usize_max_u128 {
            for ip in iter.step_by(d as usize) {
                output.push(Ipv6Cidr::from_prefix_and_bits(ip, bits).unwrap());
            }
        } else {
            let nth = d - 1;

            if let Some(ip) = iter.next() {
                output.push(Ipv6Cidr::from_prefix_and_bits(ip, bits).unwrap());

                while let Some(ip) = iter.nth_u128((nth, false)) {
                    output.push(Ipv6Cidr::from_prefix_and_bits(ip, bits).unwrap());
                }
            }
        }

        Some(output)
    }
/// Divide an IPv6 CIDR into subnetworks with a specific bits.
Divide an IPv6 CIDR into subnetworks with a specific bits.
Divide an IPv6 CIDR into subnetworks with a specific bits .
pub fn sub_networks(cidr: &Ipv6Cidr, bits: u8) -> Option<Vec<Ipv6Cidr>> {
        let cidr_bits = cidr.get_bits();
        if cidr_bits > bits {
            return None;
        } else if cidr_bits == bits {
            return Some(vec![cidr.clone()]);
        }
        let n = 2usize.pow(u32::from(bits - cidr_bits));
        let n_u128 = n as u128;
        let mut output = Vec::with_capacity(n);
        let size = cidr.size();
        let d = if size.1 {
            u128::max_value() / n_u128 + 1
        } else {
            size.0 / n_u128
        };
        let mut iter = cidr.iter();
        let usize_max_u128 = usize::max_value() as u128;
        if d <= usize_max_u128 {
            for ip in iter.step_by(d as usize) {
                output.push(Ipv6Cidr::from_prefix_and_bits(ip, bits).unwrap());
            }
        } else {
            let nth = d - 1;
            if let Some(ip) = iter.next() {
                output.push(Ipv6Cidr::from_prefix_and_bits(ip, bits).unwrap());
                while let Some(ip) = iter.nth_u128((nth, false)) {
                    output.push(Ipv6Cidr::from_prefix_and_bits(ip, bits).unwrap());
                }
            }
        }
        Some(output)
    }
pub fn sub_networks ( cidr : & Ipv6Cidr , bits : u8 ) -> Option < Vec < Ipv6Cidr > > { let cidr_bits = cidr . get_bits ( ) ; if cidr_bits > bits { return None ; } else if cidr_bits == bits { return Some ( vec ! [ cidr clone ( ) ] ) ; } let n = 2usize . pow ( u32 :: from ( bits - cidr_bits ) ) ; let n_u128 = n as u128 ; let mut output = Vec :: with_capacity ( n ) ; let size = cidr . size ( ) ; let d = if size . 1 { u128 :: max_value ( ) / n_u128 + 1 } else { size . 0 / n_u128 } ; let mut iter = cidr . iter ( ) ; let usize_max_u128 = usize :: max_value ( ) as u128 ; if d <= usize_max_u128 { for ip in iter . step_by ( d as usize ) { output . push ( Ipv6Cidr :: from_prefix_and_bits ( ip , bits ) . unwrap ( ) ) ; } } else { let nth = d - 1 ; if let Some ( ip ) = iter . next ( ) { output . push ( Ipv6Cidr :: from_prefix_and_bits ( ip , bits ) . unwrap ( ) ) ; while let Some ( ip ) = iter . nth_u128 ( ( nth , false ) ) { output . push ( Ipv6Cidr :: from_prefix_and_bits ( ip , bits ) . unwrap ( ) ) ; } } } Some ( output ) }
Divide an IPv6 CIDR into subnetworks with a specific bits.
Divide an IPv6 CIDR into subnetworks with a specific bits .
cidr
&Ipv6Cidr
bits
u8
cidr
&Ipv6Cidr
bits
u8