|
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,20 @@ 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 | +impl Hash for Ipv4Addr { |
| 78 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 79 | + // Hashers are often more efficient at hashing a fixed-width integer |
| 80 | + // than a bytestring, so convert before hashing. |
| 81 | + u32::from_le_bytes(self.octets).hash(state); |
| 82 | + } |
| 83 | +} |
| 84 | + |
76 | 85 | /// An IPv6 address. |
77 | 86 | /// |
78 | 87 | /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291]. |
@@ -149,12 +158,20 @@ pub struct Ipv4Addr { |
149 | 158 | /// assert_eq!("::1".parse(), Ok(localhost)); |
150 | 159 | /// assert_eq!(localhost.is_loopback(), true); |
151 | 160 | /// ``` |
152 | | -#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
| 161 | +#[derive(Copy, Clone, PartialEq, Eq)] |
153 | 162 | #[stable(feature = "rust1", since = "1.0.0")] |
154 | 163 | pub struct Ipv6Addr { |
155 | 164 | octets: [u8; 16], |
156 | 165 | } |
157 | 166 |
|
| 167 | +impl Hash for Ipv6Addr { |
| 168 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 169 | + // Hashers are often more efficient at hashing a fixed-width integer |
| 170 | + // than a bytestring, so convert before hashing. |
| 171 | + u128::from_le_bytes(self.octets).hash(state); |
| 172 | + } |
| 173 | +} |
| 174 | + |
158 | 175 | /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2]. |
159 | 176 | /// |
160 | 177 | /// # Stability Guarantees |
|
0 commit comments