Skip to content

Commit bc82685

Browse files
author
luozijun
committed
impl Debug for all sockaddr types
1 parent dad7770 commit bc82685

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

src/ifaddrs.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! of interfaces and their associated addresses.
55
66
use std::ffi;
7-
use std::fmt;
87
use std::iter::Iterator;
98
use std::mem;
109
use std::option::Option;
@@ -16,7 +15,7 @@ use sys::socket::SockAddr;
1615
use net::if_::*;
1716

1817
/// Describes a single address for an interface as returned by `getifaddrs`.
19-
#[derive(Clone, Eq, Hash, PartialEq)]
18+
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
2019
pub struct InterfaceAddress {
2120
/// Name of the network interface
2221
pub interface_name: String,
@@ -32,12 +31,6 @@ pub struct InterfaceAddress {
3231
pub destination: Option<SockAddr>,
3332
}
3433

35-
impl fmt::Debug for InterfaceAddress {
36-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37-
write!(f, "InterfaceAddress ({:?})", self.interface_name)
38-
}
39-
}
40-
4134
cfg_if! {
4235
if #[cfg(any(target_os = "emscripten", target_os = "fuchsia", target_os = "linux"))] {
4336
fn get_ifu_from_sockaddr(info: &libc::ifaddrs) -> *const libc::sockaddr {
@@ -150,4 +143,4 @@ mod tests {
150143
fn test_getifaddrs() {
151144
let _ = getifaddrs();
152145
}
153-
}
146+
}

src/sys/socket/addr.rs

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl AddressFamily {
222222
}
223223
}
224224

225-
#[derive(Clone, Copy)]
225+
#[derive(Copy)]
226226
pub enum InetAddr {
227227
V4(libc::sockaddr_in),
228228
V6(libc::sockaddr_in6),
@@ -348,6 +348,12 @@ impl hash::Hash for InetAddr {
348348
}
349349
}
350350

351+
impl Clone for InetAddr {
352+
fn clone(&self) -> InetAddr {
353+
*self
354+
}
355+
}
356+
351357
impl fmt::Display for InetAddr {
352358
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
353359
match *self {
@@ -357,6 +363,12 @@ impl fmt::Display for InetAddr {
357363
}
358364
}
359365

366+
impl fmt::Debug for InetAddr {
367+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
368+
fmt::Display::fmt(self, f)
369+
}
370+
}
371+
360372
/*
361373
*
362374
* ===== IpAddr =====
@@ -390,7 +402,6 @@ impl IpAddr {
390402
net::IpAddr::V6(ref std) => IpAddr::V6(Ipv6Addr::from_std(std)),
391403
}
392404
}
393-
394405
pub fn to_std(&self) -> net::IpAddr {
395406
match *self {
396407
IpAddr::V4(ref ip) => net::IpAddr::V4(ip.to_std()),
@@ -409,13 +420,19 @@ impl fmt::Display for IpAddr {
409420
}
410421
}
411422

423+
impl fmt::Debug for IpAddr {
424+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
425+
fmt::Display::fmt(self, f)
426+
}
427+
}
428+
412429
/*
413430
*
414431
* ===== Ipv4Addr =====
415432
*
416433
*/
417434

418-
#[derive(Clone, Copy)]
435+
#[derive(Copy)]
419436
pub struct Ipv4Addr(pub libc::in_addr);
420437

421438
impl Ipv4Addr {
@@ -463,13 +480,25 @@ impl hash::Hash for Ipv4Addr {
463480
}
464481
}
465482

483+
impl Clone for Ipv4Addr {
484+
fn clone(&self) -> Ipv4Addr {
485+
*self
486+
}
487+
}
488+
466489
impl fmt::Display for Ipv4Addr {
467490
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
468491
let octets = self.octets();
469492
write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
470493
}
471494
}
472495

496+
impl fmt::Debug for Ipv4Addr {
497+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
498+
fmt::Display::fmt(self, f)
499+
}
500+
}
501+
473502
/*
474503
*
475504
* ===== Ipv6Addr =====
@@ -524,6 +553,12 @@ impl fmt::Display for Ipv6Addr {
524553
}
525554
}
526555

556+
impl fmt::Debug for Ipv6Addr {
557+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
558+
fmt::Display::fmt(self, f)
559+
}
560+
}
561+
527562
/*
528563
*
529564
* ===== UnixAddr =====
@@ -538,7 +573,7 @@ impl fmt::Display for Ipv6Addr {
538573
/// does not require that `sun_len` include the terminating null even for normal
539574
/// sockets. Note that the actual sockaddr length is greater by
540575
/// `offset_of!(libc::sockaddr_un, sun_path)`
541-
#[derive(Clone, Copy)]
576+
#[derive(Copy)]
542577
pub struct UnixAddr(pub libc::sockaddr_un, pub usize);
543578

544579
impl UnixAddr {
@@ -645,6 +680,12 @@ impl hash::Hash for UnixAddr {
645680
}
646681
}
647682

683+
impl Clone for UnixAddr {
684+
fn clone(&self) -> UnixAddr {
685+
*self
686+
}
687+
}
688+
648689
impl fmt::Display for UnixAddr {
649690
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
650691
if self.1 == 0 {
@@ -658,14 +699,20 @@ impl fmt::Display for UnixAddr {
658699
}
659700
}
660701

702+
impl fmt::Debug for UnixAddr {
703+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
704+
fmt::Display::fmt(self, f)
705+
}
706+
}
707+
661708
/*
662709
*
663710
* ===== Sock addr =====
664711
*
665712
*/
666713

667714
/// Represents a socket address
668-
#[derive(Clone, Copy)]
715+
#[derive(Copy, Debug)]
669716
pub enum SockAddr {
670717
Inet(InetAddr),
671718
Unix(UnixAddr),
@@ -732,7 +779,8 @@ impl SockAddr {
732779
SysControlAddr(*(addr as *const sys_control::sockaddr_ctl)))),
733780
// Other address families are currently not supported and simply yield a None
734781
// entry instead of a proper conversion to a `SockAddr`.
735-
Some(_) | None => None,
782+
Some(_) => None,
783+
None => None,
736784
}
737785
}
738786
}
@@ -784,6 +832,12 @@ impl hash::Hash for SockAddr {
784832
}
785833
}
786834

835+
impl Clone for SockAddr {
836+
fn clone(&self) -> SockAddr {
837+
*self
838+
}
839+
}
840+
787841
impl fmt::Display for SockAddr {
788842
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
789843
match *self {
@@ -850,6 +904,12 @@ pub mod netlink {
850904
write!(f, "pid: {} groups: {}", self.pid(), self.groups())
851905
}
852906
}
907+
908+
impl fmt::Debug for NetlinkAddr {
909+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
910+
fmt::Display::fmt(self, f)
911+
}
912+
}
853913
}
854914

855915
#[cfg(any(target_os = "ios", target_os = "macos"))]
@@ -948,4 +1008,10 @@ pub mod sys_control {
9481008
write!(f, "id: {} unit: {}", self.id(), self.unit())
9491009
}
9501010
}
951-
}
1011+
1012+
impl fmt::Debug for SysControlAddr {
1013+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1014+
fmt::Display::fmt(self, f)
1015+
}
1016+
}
1017+
}

0 commit comments

Comments
 (0)