Skip to content

Commit d361a9a

Browse files
committed
Change backing of Ipv{4,6}AddrPrefix from Ipv{4,6}Addr to u{32,128}`
1 parent 02f141c commit d361a9a

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

library/std/src/net/ip/network.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::fmt;
4444
#[derive(Copy, PartialEq, Eq, Clone, Hash)]
4545
#[unstable(feature = "ip_prefix", issue = "86991")]
4646
pub struct Ipv4AddrPrefix {
47-
address: Ipv4Addr,
47+
address_raw: u32,
4848
len: u8,
4949
}
5050

@@ -90,7 +90,7 @@ pub struct Ipv4AddrPrefix {
9090
#[derive(Copy, PartialEq, Eq, Clone, Hash)]
9191
#[unstable(feature = "ip_prefix", issue = "86991")]
9292
pub struct Ipv6AddrPrefix {
93-
address: Ipv6Addr,
93+
address_raw: u128,
9494
len: u8,
9595
}
9696

@@ -122,13 +122,12 @@ impl Ipv4AddrPrefix {
122122
// Private constructor that assumes len <= 32.
123123
// Useful because `Result::unwrap` is not yet usable in const contexts, so `new` can't be used.
124124
pub(crate) const fn new_unchecked(address: Ipv4Addr, len: u32) -> Ipv4AddrPrefix {
125-
let address = {
125+
let masked = {
126126
let mask = Ipv4AddrPrefix::mask(len);
127-
let masked = u32::from_be_bytes(address.octets()) & mask;
128-
Ipv4Addr::from_u32(masked)
127+
u32::from_be_bytes(address.octets()) & mask
129128
};
130129

131-
Ipv4AddrPrefix { address, len: len as u8 }
130+
Ipv4AddrPrefix { address_raw: masked, len: len as u8 }
132131
}
133132

134133
/// Returns the address specifying this address prefix.
@@ -153,7 +152,7 @@ impl Ipv4AddrPrefix {
153152
#[unstable(feature = "ip_prefix", issue = "86991")]
154153
#[inline]
155154
pub const fn address(&self) -> Ipv4Addr {
156-
self.address
155+
Ipv4Addr::from_u32(self.address_raw)
157156
}
158157

159158
/// Returns the prefix length of this address prefix.
@@ -178,6 +177,7 @@ impl Ipv4AddrPrefix {
178177
}
179178

180179
// Compute the bitmask specified by a prefix length.
180+
#[inline]
181181
const fn mask(len: u32) -> u32 {
182182
if len == 0 {
183183
0
@@ -206,14 +206,14 @@ impl Ipv4AddrPrefix {
206206
#[inline]
207207
pub const fn contains(&self, address: &Ipv4Addr) -> bool {
208208
let mask = Ipv4AddrPrefix::mask(self.len as u32);
209-
u32::from_be_bytes(address.octets()) & mask == u32::from_be_bytes(self.address.octets())
209+
u32::from_be_bytes(address.octets()) & mask == self.address_raw
210210
}
211211
}
212212

213213
#[unstable(feature = "ip_prefix", issue = "86991")]
214214
impl fmt::Display for Ipv4AddrPrefix {
215215
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
216-
write!(fmt, "{}/{}", self.address, self.len)
216+
write!(fmt, "{}/{}", Ipv4Addr::from_u32(self.address_raw), self.len)
217217
}
218218
}
219219

@@ -228,7 +228,7 @@ impl fmt::Debug for Ipv4AddrPrefix {
228228
impl From<Ipv4Addr> for Ipv4AddrPrefix {
229229
/// Converts an IPv4 address `a.b.c.d` to the prefix `a.b.c.d/32`.
230230
fn from(address: Ipv4Addr) -> Self {
231-
Ipv4AddrPrefix { address, len: u32::BITS as u8 }
231+
Ipv4AddrPrefix::new_unchecked(address, u32::BITS)
232232
}
233233
}
234234

@@ -260,13 +260,12 @@ impl Ipv6AddrPrefix {
260260
// Private constructor that assumes len <= 128.
261261
// Useful because `Result::unwrap` is not yet usable in const contexts, so `new` can't be used.
262262
pub(crate) const fn new_unchecked(address: Ipv6Addr, len: u32) -> Ipv6AddrPrefix {
263-
let address = {
263+
let masked = {
264264
let mask = Ipv6AddrPrefix::mask(len);
265-
let masked = u128::from_be_bytes(address.octets()) & mask;
266-
Ipv6Addr::from_u128(masked)
265+
u128::from_be_bytes(address.octets()) & mask
267266
};
268267

269-
Ipv6AddrPrefix { address, len: len as u8 }
268+
Ipv6AddrPrefix { address_raw: masked, len: len as u8 }
270269
}
271270

272271
/// Returns the address specifying this address prefix.
@@ -291,7 +290,7 @@ impl Ipv6AddrPrefix {
291290
#[unstable(feature = "ip_prefix", issue = "86991")]
292291
#[inline]
293292
pub const fn address(&self) -> Ipv6Addr {
294-
self.address
293+
Ipv6Addr::from_u128(self.address_raw)
295294
}
296295

297296
/// Returns the prefix length of this address prefix.
@@ -343,14 +342,14 @@ impl Ipv6AddrPrefix {
343342
#[inline]
344343
pub const fn contains(&self, address: &Ipv6Addr) -> bool {
345344
let mask = Ipv6AddrPrefix::mask(self.len as u32);
346-
u128::from_be_bytes(address.octets()) & mask == u128::from_be_bytes(self.address.octets())
345+
u128::from_be_bytes(address.octets()) & mask == self.address_raw
347346
}
348347
}
349348

350349
#[unstable(feature = "ip_prefix", issue = "86991")]
351350
impl fmt::Display for Ipv6AddrPrefix {
352351
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
353-
write!(fmt, "{}/{}", self.address, self.len)
352+
write!(fmt, "{}/{}", Ipv6Addr::from_u128(self.address_raw), self.len)
354353
}
355354
}
356355

@@ -365,7 +364,7 @@ impl fmt::Debug for Ipv6AddrPrefix {
365364
impl From<Ipv6Addr> for Ipv6AddrPrefix {
366365
/// Converts an IPv6 address `a:b:c:d:e:f:g:h` to the prefix `a:b:c:d:e:f:g:h/128`.
367366
fn from(address: Ipv6Addr) -> Self {
368-
Ipv6AddrPrefix { address, len: u128::BITS as u8 }
367+
Ipv6AddrPrefix::new_unchecked(address, u128::BITS)
369368
}
370369
}
371370

0 commit comments

Comments
 (0)