[pve-devel] [PATCH proxmox v4 1/5] network-types: initial commit

Stefan Hanreich s.hanreich at proxmox.com
Thu Jul 3 15:46:03 CEST 2025


On 7/3/25 15:11, Wolfgang Bumiller wrote:
>> +    /// checks whether this CIDR contains an IPv4 address.
>> +    pub fn contains_address(&self, other: &Ipv4Addr) -> bool {
>> +        let bits = u32::from_be_bytes(self.addr.octets());
>> +        let other_bits = u32::from_be_bytes(other.octets());
>> +
>> +        let shift_amount: u32 = IPV4_LENGTH.saturating_sub(self.mask).into();
>> +
>> +        bits.checked_shr(shift_amount).unwrap_or(0)
>> +            == other_bits.checked_shr(shift_amount).unwrap_or(0)
> 
> ^ Could IMO just use `>>` since `IPV4_LENGTH.saturating_sub(self.mask)`
> is always <= the number of bits or an u32.

shift_amount can be 32 and >> needs the shift amount to be strictly
smaller than the width of the integer, see [1].

We could short-circuit when mask == width instead?

I'll fix the rest in a new version!

[1]
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=036301d385d610e632210655e44d1e38




More information about the pve-devel mailing list