|
1 | 1 | use super::display_buffer::DisplayBuffer;
|
2 | 2 | use crate::cmp::Ordering;
|
3 | 3 | use crate::fmt::{self, Write};
|
| 4 | +use crate::hash::{Hash, Hasher}; |
4 | 5 | use crate::iter;
|
5 | 6 | use crate::mem::transmute;
|
6 | 7 | use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
|
@@ -67,12 +68,22 @@ pub enum IpAddr {
|
67 | 68 | /// assert!("0000000.0.0.0".parse::<Ipv4Addr>().is_err()); // first octet is a zero in octal
|
68 | 69 | /// assert!("0xcb.0x0.0x71.0x00".parse::<Ipv4Addr>().is_err()); // all octets are in hex
|
69 | 70 | /// ```
|
70 |
| -#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
| 71 | +#[derive(Copy, Clone, PartialEq, Eq)] |
71 | 72 | #[stable(feature = "rust1", since = "1.0.0")]
|
72 | 73 | pub struct Ipv4Addr {
|
73 | 74 | octets: [u8; 4],
|
74 | 75 | }
|
75 | 76 |
|
| 77 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 78 | +impl Hash for Ipv4Addr { |
| 79 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 80 | + // Hashers are often more efficient at hashing a fixed-width integer |
| 81 | + // than a bytestring, so convert before hashing. We don't use to_bits() |
| 82 | + // here as that may involve a byteswap which is unnecessary. |
| 83 | + u32::from_ne_bytes(self.octets).hash(state); |
| 84 | + } |
| 85 | +} |
| 86 | + |
76 | 87 | /// An IPv6 address.
|
77 | 88 | ///
|
78 | 89 | /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291].
|
@@ -149,12 +160,22 @@ pub struct Ipv4Addr {
|
149 | 160 | /// assert_eq!("::1".parse(), Ok(localhost));
|
150 | 161 | /// assert_eq!(localhost.is_loopback(), true);
|
151 | 162 | /// ```
|
152 |
| -#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
| 163 | +#[derive(Copy, Clone, PartialEq, Eq)] |
153 | 164 | #[stable(feature = "rust1", since = "1.0.0")]
|
154 | 165 | pub struct Ipv6Addr {
|
155 | 166 | octets: [u8; 16],
|
156 | 167 | }
|
157 | 168 |
|
| 169 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 170 | +impl Hash for Ipv6Addr { |
| 171 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 172 | + // Hashers are often more efficient at hashing a fixed-width integer |
| 173 | + // than a bytestring, so convert before hashing. We don't use to_bits() |
| 174 | + // here as that may involve unnecessary byteswaps. |
| 175 | + u128::from_ne_bytes(self.octets).hash(state); |
| 176 | + } |
| 177 | +} |
| 178 | + |
158 | 179 | /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2].
|
159 | 180 | ///
|
160 | 181 | /// # Stability Guarantees
|
|
0 commit comments