core_net/socket_addr.rs
1use core::cmp::Ordering;
2use core::fmt::{self, Write};
3use core::hash;
4use crate::{IpAddr, Ipv4Addr, Ipv6Addr};
5
6/// An internet socket address, either IPv4 or IPv6.
7///
8/// Internet socket addresses consist of an [IP address], a 16-bit port number, as well
9/// as possibly some version-dependent additional information. See [`SocketAddrV4`]'s and
10/// [`SocketAddrV6`]'s respective documentation for more details.
11///
12/// The size of a `SocketAddr` instance may vary depending on the target operating
13/// system.
14///
15/// [IP address]: IpAddr
16///
17/// # Examples
18///
19/// ```
20/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
21///
22/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
23///
24/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
25/// assert_eq!(socket.port(), 8080);
26/// assert_eq!(socket.is_ipv4(), true);
27/// ```
28#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
29pub enum SocketAddr {
30 /// An IPv4 socket address.
31 V4(SocketAddrV4),
32 /// An IPv6 socket address.
33 V6(SocketAddrV6),
34}
35
36/// An IPv4 socket address.
37///
38/// IPv4 socket addresses consist of an [`IPv4` address] and a 16-bit port number, as
39/// stated in [IETF RFC 793].
40///
41/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
42///
43/// The size of a `SocketAddrV4` struct may vary depending on the target operating
44/// system. Do not assume that this type has the same memory layout as the underlying
45/// system representation.
46///
47/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
48/// [`IPv4` address]: Ipv4Addr
49///
50/// # Examples
51///
52/// ```
53/// use std::net::{Ipv4Addr, SocketAddrV4};
54///
55/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
56///
57/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
58/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
59/// assert_eq!(socket.port(), 8080);
60/// ```
61#[derive(Copy, Clone, Eq, PartialEq)]
62pub struct SocketAddrV4 {
63 ip: Ipv4Addr,
64 port: u16,
65}
66
67/// An IPv6 socket address.
68///
69/// IPv6 socket addresses consist of an [`IPv6` address], a 16-bit port number, as well
70/// as fields containing the traffic class, the flow label, and a scope identifier
71/// (see [IETF RFC 2553, Section 3.3] for more details).
72///
73/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
74///
75/// The size of a `SocketAddrV6` struct may vary depending on the target operating
76/// system. Do not assume that this type has the same memory layout as the underlying
77/// system representation.
78///
79/// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
80/// [`IPv6` address]: Ipv6Addr
81///
82/// # Examples
83///
84/// ```
85/// use std::net::{Ipv6Addr, SocketAddrV6};
86///
87/// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
88///
89/// assert_eq!("[2001:db8::1]:8080".parse(), Ok(socket));
90/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
91/// assert_eq!(socket.port(), 8080);
92/// ```
93#[derive(Copy, Clone, Eq, PartialEq)]
94pub struct SocketAddrV6 {
95 ip: Ipv6Addr,
96 port: u16,
97 flowinfo: u32,
98 scope_id: u32,
99}
100
101impl SocketAddr {
102 /// Creates a new socket address from an [IP address] and a port number.
103 ///
104 /// [IP address]: IpAddr
105 ///
106 /// # Examples
107 ///
108 /// ```
109 /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
110 ///
111 /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
112 /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
113 /// assert_eq!(socket.port(), 8080);
114 /// ```
115 #[must_use]
116 pub const fn new(ip: IpAddr, port: u16) -> SocketAddr {
117 match ip {
118 IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
119 IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
120 }
121 }
122
123 /// Returns the IP address associated with this socket address.
124 ///
125 /// # Examples
126 ///
127 /// ```
128 /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
129 ///
130 /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
131 /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
132 /// ```
133 #[must_use]
134 pub const fn ip(&self) -> IpAddr {
135 match *self {
136 SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
137 SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
138 }
139 }
140
141 /// Changes the IP address associated with this socket address.
142 ///
143 /// # Examples
144 ///
145 /// ```
146 /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
147 ///
148 /// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
149 /// socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
150 /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
151 /// ```
152 pub fn set_ip(&mut self, new_ip: IpAddr) {
153 // `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
154 match (self, new_ip) {
155 (&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip),
156 (&mut SocketAddr::V6(ref mut a), IpAddr::V6(new_ip)) => a.set_ip(new_ip),
157 (self_, new_ip) => *self_ = Self::new(new_ip, self_.port()),
158 }
159 }
160
161 /// Returns the port number associated with this socket address.
162 ///
163 /// # Examples
164 ///
165 /// ```
166 /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
167 ///
168 /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
169 /// assert_eq!(socket.port(), 8080);
170 /// ```
171 #[must_use]
172 pub const fn port(&self) -> u16 {
173 match *self {
174 SocketAddr::V4(ref a) => a.port(),
175 SocketAddr::V6(ref a) => a.port(),
176 }
177 }
178
179 /// Changes the port number associated with this socket address.
180 ///
181 /// # Examples
182 ///
183 /// ```
184 /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
185 ///
186 /// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
187 /// socket.set_port(1025);
188 /// assert_eq!(socket.port(), 1025);
189 /// ```
190 pub fn set_port(&mut self, new_port: u16) {
191 match *self {
192 SocketAddr::V4(ref mut a) => a.set_port(new_port),
193 SocketAddr::V6(ref mut a) => a.set_port(new_port),
194 }
195 }
196
197 /// Returns [`true`] if the [IP address] in this `SocketAddr` is an
198 /// [`IPv4` address], and [`false`] otherwise.
199 ///
200 /// [IP address]: IpAddr
201 /// [`IPv4` address]: IpAddr::V4
202 ///
203 /// # Examples
204 ///
205 /// ```
206 /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
207 ///
208 /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
209 /// assert_eq!(socket.is_ipv4(), true);
210 /// assert_eq!(socket.is_ipv6(), false);
211 /// ```
212 #[must_use]
213 pub const fn is_ipv4(&self) -> bool {
214 matches!(*self, SocketAddr::V4(_))
215 }
216
217 /// Returns [`true`] if the [IP address] in this `SocketAddr` is an
218 /// [`IPv6` address], and [`false`] otherwise.
219 ///
220 /// [IP address]: IpAddr
221 /// [`IPv6` address]: IpAddr::V6
222 ///
223 /// # Examples
224 ///
225 /// ```
226 /// use std::net::{IpAddr, Ipv6Addr, SocketAddr};
227 ///
228 /// let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 0, 1)), 8080);
229 /// assert_eq!(socket.is_ipv4(), false);
230 /// assert_eq!(socket.is_ipv6(), true);
231 /// ```
232 #[must_use]
233 pub const fn is_ipv6(&self) -> bool {
234 matches!(*self, SocketAddr::V6(_))
235 }
236}
237
238impl SocketAddrV4 {
239 /// Creates a new socket address from an [`IPv4` address] and a port number.
240 ///
241 /// [`IPv4` address]: Ipv4Addr
242 ///
243 /// # Examples
244 ///
245 /// ```
246 /// use std::net::{SocketAddrV4, Ipv4Addr};
247 ///
248 /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
249 /// ```
250 #[must_use]
251 pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
252 SocketAddrV4 { ip, port }
253 }
254
255 /// Returns the IP address associated with this socket address.
256 ///
257 /// # Examples
258 ///
259 /// ```
260 /// use std::net::{SocketAddrV4, Ipv4Addr};
261 ///
262 /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
263 /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
264 /// ```
265 #[must_use]
266 pub const fn ip(&self) -> &Ipv4Addr {
267 &self.ip
268 }
269
270 /// Changes the IP address associated with this socket address.
271 ///
272 /// # Examples
273 ///
274 /// ```
275 /// use std::net::{SocketAddrV4, Ipv4Addr};
276 ///
277 /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
278 /// socket.set_ip(Ipv4Addr::new(192, 168, 0, 1));
279 /// assert_eq!(socket.ip(), &Ipv4Addr::new(192, 168, 0, 1));
280 /// ```
281 pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
282 self.ip = new_ip;
283 }
284
285 /// Returns the port number associated with this socket address.
286 ///
287 /// # Examples
288 ///
289 /// ```
290 /// use std::net::{SocketAddrV4, Ipv4Addr};
291 ///
292 /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
293 /// assert_eq!(socket.port(), 8080);
294 /// ```
295 #[must_use]
296 pub const fn port(&self) -> u16 {
297 self.port
298 }
299
300 /// Changes the port number associated with this socket address.
301 ///
302 /// # Examples
303 ///
304 /// ```
305 /// use std::net::{SocketAddrV4, Ipv4Addr};
306 ///
307 /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
308 /// socket.set_port(4242);
309 /// assert_eq!(socket.port(), 4242);
310 /// ```
311 pub fn set_port(&mut self, new_port: u16) {
312 self.port = new_port;
313 }
314}
315
316impl SocketAddrV6 {
317 /// Creates a new socket address from an [`IPv6` address], a 16-bit port number,
318 /// and the `flowinfo` and `scope_id` fields.
319 ///
320 /// For more information on the meaning and layout of the `flowinfo` and `scope_id`
321 /// parameters, see [IETF RFC 2553, Section 3.3].
322 ///
323 /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
324 /// [`IPv6` address]: Ipv6Addr
325 ///
326 /// # Examples
327 ///
328 /// ```
329 /// use std::net::{SocketAddrV6, Ipv6Addr};
330 ///
331 /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
332 /// ```
333 #[must_use]
334 pub const fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 {
335 SocketAddrV6 { ip, port, flowinfo, scope_id }
336 }
337
338 /// Returns the IP address associated with this socket address.
339 ///
340 /// # Examples
341 ///
342 /// ```
343 /// use std::net::{SocketAddrV6, Ipv6Addr};
344 ///
345 /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
346 /// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
347 /// ```
348 #[must_use]
349 pub const fn ip(&self) -> &Ipv6Addr {
350 &self.ip
351 }
352
353 /// Changes the IP address associated with this socket address.
354 ///
355 /// # Examples
356 ///
357 /// ```
358 /// use std::net::{SocketAddrV6, Ipv6Addr};
359 ///
360 /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
361 /// socket.set_ip(Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
362 /// assert_eq!(socket.ip(), &Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
363 /// ```
364 pub fn set_ip(&mut self, new_ip: Ipv6Addr) {
365 self.ip = new_ip;
366 }
367
368 /// Returns the port number associated with this socket address.
369 ///
370 /// # Examples
371 ///
372 /// ```
373 /// use std::net::{SocketAddrV6, Ipv6Addr};
374 ///
375 /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
376 /// assert_eq!(socket.port(), 8080);
377 /// ```
378 #[must_use]
379 pub const fn port(&self) -> u16 {
380 self.port
381 }
382
383 /// Changes the port number associated with this socket address.
384 ///
385 /// # Examples
386 ///
387 /// ```
388 /// use std::net::{SocketAddrV6, Ipv6Addr};
389 ///
390 /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
391 /// socket.set_port(4242);
392 /// assert_eq!(socket.port(), 4242);
393 /// ```
394 pub fn set_port(&mut self, new_port: u16) {
395 self.port = new_port;
396 }
397
398 /// Returns the flow information associated with this address.
399 ///
400 /// This information corresponds to the `sin6_flowinfo` field in C's `netinet/in.h`,
401 /// as specified in [IETF RFC 2553, Section 3.3].
402 /// It combines information about the flow label and the traffic class as specified
403 /// in [IETF RFC 2460], respectively [Section 6] and [Section 7].
404 ///
405 /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
406 /// [IETF RFC 2460]: https://tools.ietf.org/html/rfc2460
407 /// [Section 6]: https://tools.ietf.org/html/rfc2460#section-6
408 /// [Section 7]: https://tools.ietf.org/html/rfc2460#section-7
409 ///
410 /// # Examples
411 ///
412 /// ```
413 /// use std::net::{SocketAddrV6, Ipv6Addr};
414 ///
415 /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
416 /// assert_eq!(socket.flowinfo(), 10);
417 /// ```
418 #[must_use]
419 pub const fn flowinfo(&self) -> u32 {
420 self.flowinfo
421 }
422
423 /// Changes the flow information associated with this socket address.
424 ///
425 /// See [`SocketAddrV6::flowinfo`]'s documentation for more details.
426 ///
427 /// # Examples
428 ///
429 /// ```
430 /// use std::net::{SocketAddrV6, Ipv6Addr};
431 ///
432 /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
433 /// socket.set_flowinfo(56);
434 /// assert_eq!(socket.flowinfo(), 56);
435 /// ```
436 pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
437 self.flowinfo = new_flowinfo;
438 }
439
440 /// Returns the scope ID associated with this address.
441 ///
442 /// This information corresponds to the `sin6_scope_id` field in C's `netinet/in.h`,
443 /// as specified in [IETF RFC 2553, Section 3.3].
444 ///
445 /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
446 ///
447 /// # Examples
448 ///
449 /// ```
450 /// use std::net::{SocketAddrV6, Ipv6Addr};
451 ///
452 /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
453 /// assert_eq!(socket.scope_id(), 78);
454 /// ```
455 #[must_use]
456 pub const fn scope_id(&self) -> u32 {
457 self.scope_id
458 }
459
460 /// Changes the scope ID associated with this socket address.
461 ///
462 /// See [`SocketAddrV6::scope_id`]'s documentation for more details.
463 ///
464 /// # Examples
465 ///
466 /// ```
467 /// use std::net::{SocketAddrV6, Ipv6Addr};
468 ///
469 /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
470 /// socket.set_scope_id(42);
471 /// assert_eq!(socket.scope_id(), 42);
472 /// ```
473 pub fn set_scope_id(&mut self, new_scope_id: u32) {
474 self.scope_id = new_scope_id;
475 }
476}
477
478impl From<SocketAddrV4> for SocketAddr {
479 /// Converts a [`SocketAddrV4`] into a [`SocketAddr::V4`].
480 fn from(sock4: SocketAddrV4) -> SocketAddr {
481 SocketAddr::V4(sock4)
482 }
483}
484
485impl From<SocketAddrV6> for SocketAddr {
486 /// Converts a [`SocketAddrV6`] into a [`SocketAddr::V6`].
487 fn from(sock6: SocketAddrV6) -> SocketAddr {
488 SocketAddr::V6(sock6)
489 }
490}
491
492impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
493 /// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`].
494 ///
495 /// This conversion creates a [`SocketAddr::V4`] for an [`IpAddr::V4`]
496 /// and creates a [`SocketAddr::V6`] for an [`IpAddr::V6`].
497 ///
498 /// `u16` is treated as port of the newly created [`SocketAddr`].
499 fn from(pieces: (I, u16)) -> SocketAddr {
500 SocketAddr::new(pieces.0.into(), pieces.1)
501 }
502}
503
504impl fmt::Display for SocketAddr {
505 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
506 match *self {
507 SocketAddr::V4(ref a) => a.fmt(f),
508 SocketAddr::V6(ref a) => a.fmt(f),
509 }
510 }
511}
512
513impl fmt::Debug for SocketAddr {
514 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
515 fmt::Display::fmt(self, fmt)
516 }
517}
518
519impl fmt::Display for SocketAddrV4 {
520 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
521 // If there are no alignment requirements, write the socket address directly to `f`.
522 // Otherwise, write it to a local buffer and then use `f.pad`.
523 if f.precision().is_none() && f.width().is_none() {
524 write!(f, "{}:{}", self.ip(), self.port())
525 } else {
526 const LONGEST_IPV4_SOCKET_ADDR: &str = "255.255.255.255:65536";
527
528 let mut buf = String::with_capacity(LONGEST_IPV4_SOCKET_ADDR.len());
529 // Buffer is long enough for the longest possible IPv4 socket address, so this should never fail.
530 write!(buf, "{}:{}", self.ip(), self.port()).unwrap();
531
532 f.pad(buf.as_str())
533 }
534 }
535}
536
537impl fmt::Debug for SocketAddrV4 {
538 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
539 fmt::Display::fmt(self, fmt)
540 }
541}
542
543impl fmt::Display for SocketAddrV6 {
544 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
545 // If there are no alignment requirements, write the socket address directly to `f`.
546 // Otherwise, write it to a local buffer and then use `f.pad`.
547 if f.precision().is_none() && f.width().is_none() {
548 match self.scope_id() {
549 0 => write!(f, "[{}]:{}", self.ip(), self.port()),
550 scope_id => write!(f, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
551 }
552 } else {
553 const LONGEST_IPV6_SOCKET_ADDR: &str =
554 "[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967296]:65536";
555
556 let mut buf = String::with_capacity(LONGEST_IPV6_SOCKET_ADDR.len());
557 match self.scope_id() {
558 0 => write!(buf, "[{}]:{}", self.ip(), self.port()),
559 scope_id => write!(buf, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
560 }
561 // Buffer is long enough for the longest possible IPv6 socket address, so this should never fail.
562 .unwrap();
563
564 f.pad(buf.as_str())
565 }
566 }
567}
568
569impl fmt::Debug for SocketAddrV6 {
570 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
571 fmt::Display::fmt(self, fmt)
572 }
573}
574
575impl PartialOrd for SocketAddrV4 {
576 fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
577 Some(self.cmp(other))
578 }
579}
580
581impl PartialOrd for SocketAddrV6 {
582 fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
583 Some(self.cmp(other))
584 }
585}
586
587impl Ord for SocketAddrV4 {
588 fn cmp(&self, other: &SocketAddrV4) -> Ordering {
589 self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
590 }
591}
592
593impl Ord for SocketAddrV6 {
594 fn cmp(&self, other: &SocketAddrV6) -> Ordering {
595 self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
596 }
597}
598
599impl hash::Hash for SocketAddrV4 {
600 fn hash<H: hash::Hasher>(&self, s: &mut H) {
601 (self.port, self.ip).hash(s)
602 }
603}
604
605impl hash::Hash for SocketAddrV6 {
606 fn hash<H: hash::Hasher>(&self, s: &mut H) {
607 (self.port, &self.ip, self.flowinfo, self.scope_id).hash(s)
608 }
609}