Repository: rust-lang/socket2 (Rust)

Issue:
Rename SocketAddr::vsock_address to as_vsock_address
All other conversion have the `as_` prefix, this an exception so let's fix that in v0.5.


Discussion:

Patch:
diff --git a/src/sockaddr.rs b/src/sockaddr.rs
index 5a25adcc..5cb0fb99 100644
--- a/src/sockaddr.rs
+++ b/src/sockaddr.rs
@@ -7,9 +7,10 @@ use std::{fmt, io};
 use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;
 
 use crate::sys::{
-    sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET,
+    c_int, sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET,
     AF_INET6,
 };
+use crate::Domain;
 
 /// The address of a socket.
 ///
@@ -23,6 +24,16 @@ pub struct SockAddr {
 
 #[allow(clippy::len_without_is_empty)]
 impl SockAddr {
+    /// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path.
+    ///
+    /// Returns an error if the path is longer than `SUN_LEN`.
+    pub fn unix<P>(path: P) -> io::Result<SockAddr>
+    where
+        P: AsRef<Path>,
+    {
+        crate::sys::unix_sockaddr(path.as_ref())
+    }
+
     /// Create a `SockAddr` from the underlying storage and its length.
     ///
     /// # Safety
@@ -146,6 +157,11 @@ impl SockAddr {
         self.storage.ss_family
     }
 
+    /// Returns this address's `Domain`.
+    pub const fn domain(&self) -> Domain {
+        Domain(self.storage.ss_family as c_int)
+    }
+
     /// Returns the size of this address in bytes.
     pub const fn len(&self) -> socklen_t {
         self.len
@@ -156,6 +172,17 @@ impl SockAddr {
         &self.storage as *const _ as *const _
     }
 
+    /// Returns true if this address is in the `AF_INET` (IPv4) family, false otherwise.
+    pub const fn is_ipv4(&self) -> bool {
+        self.storage.ss_family == AF_INET as sa_family_t
+    }
+
+    /// Returns true if this address is in the `AF_INET6` (IPv6) family, false
+    /// otherwise.
+    pub const fn is_ipv6(&self) -> bool {
+        self.storage.ss_family == AF_INET6 as sa_family_t
+    }
+
     /// Returns a raw pointer to the address storage.
     #[cfg(all(unix, not(target_os = "redox")))]
     pub(crate) const fn as_storage_ptr(&self) -> *const sockaddr_storage {
@@ -211,16 +238,6 @@ impl SockAddr {
             _ => None,
         }
     }
-
-    /// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path.
-    ///
-    /// Returns an error if the path is longer than `SUN_LEN`.
-    pub fn unix<P>(path: P) -> io::Result<SockAddr>
-    where
-        P: AsRef<Path>,
-    {
-        crate::sys::unix_sockaddr(path.as_ref())
-    }
 }
 
 impl From<SocketAddr> for SockAddr {
@@ -322,7 +339,9 @@ fn ipv4() {
     use std::net::Ipv4Addr;
     let std = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
     let addr = SockAddr::from(std);
+    assert_eq!(addr.is_ipv4(), true);
     assert_eq!(addr.family(), AF_INET as sa_family_t);
+    assert_eq!(addr.domain(), Domain::IPV4);
     assert_eq!(addr.len(), size_of::<sockaddr_in>() as socklen_t);
     assert_eq!(addr.as_socket(), Some(SocketAddr::V4(std)));
     assert_eq!(addr.as_socket_ipv4(), Some(std));
@@ -341,7 +360,9 @@ fn ipv6() {
     use std::net::Ipv6Addr;
     let std = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);
     let addr = SockAddr::from(std);
+    assert_eq!(addr.is_ipv6(), true);
     assert_eq!(addr.family(), AF_INET6 as sa_family_t);
+    assert_eq!(addr.domain(), Domain::IPV6);
     assert_eq!(addr.len(), size_of::<sockaddr_in6>() as socklen_t);
     assert_eq!(addr.as_socket(), Some(SocketAddr::V6(std)));
     assert!(addr.as_socket_ipv4().is_none());
diff --git a/src/sys/unix.rs b/src/sys/unix.rs
index 8d83b1fc..51045132 100644
--- a/src/sys/unix.rs
+++ b/src/sys/unix.rs
@@ -535,7 +535,7 @@ impl SockAddr {
         docsrs,
         doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
     )]
-    pub fn vsock_address(&self) -> Option<(u32, u32)> {
+    pub fn as_vsock_address(&self) -> Option<(u32, u32)> {
         if self.family() == libc::AF_VSOCK as sa_family_t {
             // Safety: if the ss_family field is AF_VSOCK then storage must be a sockaddr_vm.
             let addr = unsafe { &*(self.as_ptr() as *const libc::sockaddr_vm) };


Test patch:
diff --git a/tests/socket.rs b/tests/socket.rs
index ecbe9e58..7540b5d6 100644
--- a/tests/socket.rs
+++ b/tests/socket.rs
@@ -151,7 +151,7 @@ fn socket_address_vsock() {
     let addr = SockAddr::vsock(1, 9999);
     assert!(addr.as_socket_ipv4().is_none());
     assert!(addr.as_socket_ipv6().is_none());
-    assert_eq!(addr.vsock_address().unwrap(), (1, 9999));
+    assert_eq!(addr.as_vsock_address().unwrap(), (1, 9999));
 }
 
 #[test]
@@ -481,7 +481,7 @@ fn vsock() {
     listener.bind(&addr).unwrap();
     listener.listen(10).unwrap();
 
-    let (_, port) = listener.local_addr().unwrap().vsock_address().unwrap();
+    let (_, port) = listener.local_addr().unwrap().as_vsock_address().unwrap();
     let addr = SockAddr::vsock(libc::VMADDR_CID_LOCAL, port);
     let mut a = Socket::new(Domain::VSOCK, Type::STREAM, None).unwrap();
     a.connect(&addr).unwrap();