Introduce unstable Ipv{4,6}AddrPrefix#86992
Conversation
| /// [IPv4 address]: Ipv4Addr | ||
| #[derive(Copy, PartialEq, Eq, Clone, Hash)] | ||
| #[unstable(feature = "ip_prefix", issue = "86991")] | ||
| pub struct Ipv4AddrPrefix { |
There was a problem hiding this comment.
Python uses the name IPv6Network, but I chose the less generic name Ipv4AddrPrefix.
| #[unstable(feature = "ip_prefix", issue = "86991")] | ||
| pub struct Ipv4AddrPrefix { | ||
| address: Ipv4Addr, | ||
| len: u8, |
There was a problem hiding this comment.
len stored as an u8 to reduce size, however new and len use/return a u32 (chosen that way to have the same type as {u32, u128}::BITS).
There was a problem hiding this comment.
Well, it reduces size but not alignment. So if you put into an array there wouldn't be any savings.
| /// ``` | ||
| #[unstable(feature = "ip_prefix", issue = "86991")] | ||
| #[inline] | ||
| pub const fn new(address: Ipv4Addr, len: u32) -> Result<Ipv4AddrPrefix, InvalidPrefixError> { |
There was a problem hiding this comment.
Constructor is fallible, unlike the IpvAddr and SocketAddr constructors, because the prefix length can not be longer than there are bits.
| /// Ipv4AddrPrefix::new(Ipv4Addr::new(192, 0, 2, 0), 35).unwrap_err(); | ||
| /// ``` | ||
| #[unstable(feature = "ip_prefix", issue = "86991")] | ||
| #[inline] |
There was a problem hiding this comment.
Not sure which methods should or shouldn't be inlined.
|
As mentioned in the other PR, having a mask instead of a prefix would be more powerful. The prefix could still be passed as a convenience constructor. |
| #[derive(Copy, PartialEq, Eq, Clone, Hash)] | ||
| #[unstable(feature = "ip_prefix", issue = "86991")] | ||
| pub struct Ipv4AddrPrefix { | ||
| address_raw: u32, |
There was a problem hiding this comment.
Changed from Ipv4Addr to a u32 to avoid storing potential extra fields of c::in_addr. Same thing for Ipv6AddrPrefix. This could be reverted if #78802 ever gets merged.
|
I agree that the ability to do computations with address masks should be added, like you said comparing addresses and using a subnet mask ( An address + prefix ( |
But a mask can everything a prefix can, so why not implement the more powerful concept? |
|
☔ The latest upstream changes (presumably #85769) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Ping from triage: |
|
Triage: |
|
@JohnCSimon I'm not the PR author. |
This PR introduces types representing IP address prefixes such as
192.0.2.0/24, as suggested in #86969 (comment).Feature gate:
#![feature(ip_prefix)]Tracking issue: #86991
Overview
Ipv4AddrPrefix
Ipv6AddrPrefix
This PR doesn't make any changes yet to the
Ipv{4,6}Addrtypes themselves.Unresolved Questions
Is the naming clear? Is this the correct abstraction?