-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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}Addr
types themselves.Unresolved Questions
Is the naming clear? Is this the correct abstraction?