@@ -44,7 +44,7 @@ use crate::fmt;
44
44
#[ derive( Copy , PartialEq , Eq , Clone , Hash ) ]
45
45
#[ unstable( feature = "ip_prefix" , issue = "86991" ) ]
46
46
pub struct Ipv4AddrPrefix {
47
- address : Ipv4Addr ,
47
+ address_raw : u32 ,
48
48
len : u8 ,
49
49
}
50
50
@@ -90,7 +90,7 @@ pub struct Ipv4AddrPrefix {
90
90
#[ derive( Copy , PartialEq , Eq , Clone , Hash ) ]
91
91
#[ unstable( feature = "ip_prefix" , issue = "86991" ) ]
92
92
pub struct Ipv6AddrPrefix {
93
- address : Ipv6Addr ,
93
+ address_raw : u128 ,
94
94
len : u8 ,
95
95
}
96
96
@@ -122,13 +122,12 @@ impl Ipv4AddrPrefix {
122
122
// Private constructor that assumes len <= 32.
123
123
// Useful because `Result::unwrap` is not yet usable in const contexts, so `new` can't be used.
124
124
pub ( crate ) const fn new_unchecked ( address : Ipv4Addr , len : u32 ) -> Ipv4AddrPrefix {
125
- let address = {
125
+ let masked = {
126
126
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
129
128
} ;
130
129
131
- Ipv4AddrPrefix { address , len : len as u8 }
130
+ Ipv4AddrPrefix { address_raw : masked , len : len as u8 }
132
131
}
133
132
134
133
/// Returns the address specifying this address prefix.
@@ -153,7 +152,7 @@ impl Ipv4AddrPrefix {
153
152
#[ unstable( feature = "ip_prefix" , issue = "86991" ) ]
154
153
#[ inline]
155
154
pub const fn address ( & self ) -> Ipv4Addr {
156
- self . address
155
+ Ipv4Addr :: from_u32 ( self . address_raw )
157
156
}
158
157
159
158
/// Returns the prefix length of this address prefix.
@@ -178,6 +177,7 @@ impl Ipv4AddrPrefix {
178
177
}
179
178
180
179
// Compute the bitmask specified by a prefix length.
180
+ #[ inline]
181
181
const fn mask ( len : u32 ) -> u32 {
182
182
if len == 0 {
183
183
0
@@ -206,14 +206,14 @@ impl Ipv4AddrPrefix {
206
206
#[ inline]
207
207
pub const fn contains ( & self , address : & Ipv4Addr ) -> bool {
208
208
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
210
210
}
211
211
}
212
212
213
213
#[ unstable( feature = "ip_prefix" , issue = "86991" ) ]
214
214
impl fmt:: Display for Ipv4AddrPrefix {
215
215
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)
217
217
}
218
218
}
219
219
@@ -228,7 +228,7 @@ impl fmt::Debug for Ipv4AddrPrefix {
228
228
impl From < Ipv4Addr > for Ipv4AddrPrefix {
229
229
/// Converts an IPv4 address `a.b.c.d` to the prefix `a.b.c.d/32`.
230
230
fn from ( address : Ipv4Addr ) -> Self {
231
- Ipv4AddrPrefix { address, len : u32:: BITS as u8 }
231
+ Ipv4AddrPrefix :: new_unchecked ( address, u32:: BITS )
232
232
}
233
233
}
234
234
@@ -260,13 +260,12 @@ impl Ipv6AddrPrefix {
260
260
// Private constructor that assumes len <= 128.
261
261
// Useful because `Result::unwrap` is not yet usable in const contexts, so `new` can't be used.
262
262
pub ( crate ) const fn new_unchecked ( address : Ipv6Addr , len : u32 ) -> Ipv6AddrPrefix {
263
- let address = {
263
+ let masked = {
264
264
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
267
266
} ;
268
267
269
- Ipv6AddrPrefix { address , len : len as u8 }
268
+ Ipv6AddrPrefix { address_raw : masked , len : len as u8 }
270
269
}
271
270
272
271
/// Returns the address specifying this address prefix.
@@ -291,7 +290,7 @@ impl Ipv6AddrPrefix {
291
290
#[ unstable( feature = "ip_prefix" , issue = "86991" ) ]
292
291
#[ inline]
293
292
pub const fn address ( & self ) -> Ipv6Addr {
294
- self . address
293
+ Ipv6Addr :: from_u128 ( self . address_raw )
295
294
}
296
295
297
296
/// Returns the prefix length of this address prefix.
@@ -343,14 +342,14 @@ impl Ipv6AddrPrefix {
343
342
#[ inline]
344
343
pub const fn contains ( & self , address : & Ipv6Addr ) -> bool {
345
344
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
347
346
}
348
347
}
349
348
350
349
#[ unstable( feature = "ip_prefix" , issue = "86991" ) ]
351
350
impl fmt:: Display for Ipv6AddrPrefix {
352
351
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)
354
353
}
355
354
}
356
355
@@ -365,7 +364,7 @@ impl fmt::Debug for Ipv6AddrPrefix {
365
364
impl From < Ipv6Addr > for Ipv6AddrPrefix {
366
365
/// Converts an IPv6 address `a:b:c:d:e:f:g:h` to the prefix `a:b:c:d:e:f:g:h/128`.
367
366
fn from ( address : Ipv6Addr ) -> Self {
368
- Ipv6AddrPrefix { address, len : u128:: BITS as u8 }
367
+ Ipv6AddrPrefix :: new_unchecked ( address, u128:: BITS )
369
368
}
370
369
}
371
370
0 commit comments