From c98631075748d19c1dce3768fdff9918e8f1492a Mon Sep 17 00:00:00 2001 From: jD91mZM2 Date: Wed, 27 Jun 2018 18:37:44 +0200 Subject: [PATCH] Add is_unnamed on redox --- src/libstd/sys/redox/ext/unixsocket.rs | 48 ++++++++++++++------------ src/libstd/sys/unix/ext/unixsocket.rs | 6 ++-- src/libstd/sys_common/unixsocket.rs | 28 +++++++++++++++ 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/libstd/sys/redox/ext/unixsocket.rs b/src/libstd/sys/redox/ext/unixsocket.rs index 78ca0f16af4..be37575145a 100644 --- a/src/libstd/sys/redox/ext/unixsocket.rs +++ b/src/libstd/sys/redox/ext/unixsocket.rs @@ -20,11 +20,15 @@ use sys::{cvt, fd::FileDesc, syscall}; #[stable(feature = "unix_socket", since = "1.10.0")] #[derive(Clone)] -pub(crate) struct SocketAddr(()); +pub struct SocketAddr(()); impl SocketAddr { #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn as_pathname(&self) -> Option<&Path> { + pub fn is_unnamed(&self) -> bool { + false + } + #[stable(feature = "unix_socket", since = "1.10.0")] + pub fn as_pathname(&self) -> Option<&Path> { None } } @@ -36,7 +40,7 @@ impl fmt::Debug for SocketAddr { } #[stable(feature = "unix_socket", since = "1.10.0")] -pub(crate) struct UnixStream(FileDesc); +pub struct UnixStream(FileDesc); #[stable(feature = "unix_socket", since = "1.10.0")] impl fmt::Debug for UnixStream { @@ -55,7 +59,7 @@ impl fmt::Debug for UnixStream { impl UnixStream { #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn connect(path: &Path) -> io::Result { + pub fn connect(path: &Path) -> io::Result { if let Some(s) = path.to_str() { cvt(syscall::open(format!("chan:{}", s), syscall::O_CLOEXEC)) .map(FileDesc::new) @@ -69,7 +73,7 @@ impl UnixStream { } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn pair() -> io::Result<(UnixStream, UnixStream)> { + pub fn pair() -> io::Result<(UnixStream, UnixStream)> { let server = cvt(syscall::open("chan:", syscall::O_CREAT | syscall::O_CLOEXEC)) .map(FileDesc::new)?; let client = server.duplicate_path(b"connect")?; @@ -78,52 +82,52 @@ impl UnixStream { } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn try_clone(&self) -> io::Result { + pub fn try_clone(&self) -> io::Result { self.0.duplicate().map(UnixStream) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn local_addr(&self) -> io::Result { + pub fn local_addr(&self) -> io::Result { Err(Error::new(ErrorKind::Other, "UnixStream::local_addr unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn peer_addr(&self) -> io::Result { + pub fn peer_addr(&self) -> io::Result { Err(Error::new(ErrorKind::Other, "UnixStream::peer_addr unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn set_read_timeout(&self, _timeout: Option) -> io::Result<()> { + pub fn set_read_timeout(&self, _timeout: Option) -> io::Result<()> { Err(Error::new(ErrorKind::Other, "UnixStream::set_read_timeout unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn set_write_timeout(&self, _timeout: Option) -> io::Result<()> { + pub fn set_write_timeout(&self, _timeout: Option) -> io::Result<()> { Err(Error::new(ErrorKind::Other, "UnixStream::set_write_timeout unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn read_timeout(&self) -> io::Result> { + pub fn read_timeout(&self) -> io::Result> { Err(Error::new(ErrorKind::Other, "UnixStream::read_timeout unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn write_timeout(&self) -> io::Result> { + pub fn write_timeout(&self) -> io::Result> { Err(Error::new(ErrorKind::Other, "UnixStream::write_timeout unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { self.0.set_nonblocking(nonblocking) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn take_error(&self) -> io::Result> { + pub fn take_error(&self) -> io::Result> { Ok(None) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn shutdown(&self, _how: Shutdown) -> io::Result<()> { + pub fn shutdown(&self, _how: Shutdown) -> io::Result<()> { Err(Error::new(ErrorKind::Other, "UnixStream::shutdown unimplemented on redox")) } } @@ -173,7 +177,7 @@ impl IntoRawFd for UnixStream { } #[stable(feature = "unix_socket", since = "1.10.0")] -pub(crate) struct UnixListener(FileDesc); +pub struct UnixListener(FileDesc); #[stable(feature = "unix_socket", since = "1.10.0")] impl fmt::Debug for UnixListener { @@ -189,7 +193,7 @@ impl fmt::Debug for UnixListener { impl UnixListener { #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn bind(path: &Path) -> io::Result { + pub fn bind(path: &Path) -> io::Result { if let Some(s) = path.to_str() { cvt(syscall::open(format!("chan:{}", s), syscall::O_CREAT | syscall::O_CLOEXEC)) .map(FileDesc::new) @@ -203,27 +207,27 @@ impl UnixListener { } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> { + pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> { self.0.duplicate_path(b"listen").map(|fd| (UnixStream(fd), SocketAddr(()))) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn try_clone(&self) -> io::Result { + pub fn try_clone(&self) -> io::Result { self.0.duplicate().map(UnixListener) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn local_addr(&self) -> io::Result { + pub fn local_addr(&self) -> io::Result { Err(Error::new(ErrorKind::Other, "UnixListener::local_addr unimplemented on redox")) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { self.0.set_nonblocking(nonblocking) } #[stable(feature = "unix_socket", since = "1.10.0")] - pub(crate) fn take_error(&self) -> io::Result> { + pub fn take_error(&self) -> io::Result> { Ok(None) } } diff --git a/src/libstd/sys/unix/ext/unixsocket.rs b/src/libstd/sys/unix/ext/unixsocket.rs index 7b52a5fcf0d..124555141a3 100644 --- a/src/libstd/sys/unix/ext/unixsocket.rs +++ b/src/libstd/sys/unix/ext/unixsocket.rs @@ -54,7 +54,8 @@ pub struct SocketAddr { } impl SocketAddr { - pub(crate) fn new(f: F) -> io::Result + #[stable(feature = "unix_socket", since = "1.10.0")] + pub fn new(f: F) -> io::Result where F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int { unsafe { @@ -65,7 +66,8 @@ impl SocketAddr { } } - pub(crate) fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) + #[stable(feature = "unix_socket", since = "1.10.0")] + pub fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result { if len == 0 { diff --git a/src/libstd/sys_common/unixsocket.rs b/src/libstd/sys_common/unixsocket.rs index 286e0e9f37f..c7d71ae6790 100644 --- a/src/libstd/sys_common/unixsocket.rs +++ b/src/libstd/sys_common/unixsocket.rs @@ -113,6 +113,34 @@ impl<'a> io::Write for &'a UnixStream { } impl SocketAddr { + /// Returns true if and only if the address is unnamed. + /// + /// # Examples + /// + /// A named address: + /// + /// ```no_run + /// use std::os::unix::net::UnixListener; + /// + /// let socket = UnixListener::bind("/tmp/sock").unwrap(); + /// let addr = socket.local_addr().expect("Couldn't get local address"); + /// assert_eq!(addr.is_unnamed(), false); + /// ``` + /// + /// An unnamed address: + /// + /// ``` + /// use std::os::unix::net::UnixDatagram; + /// + /// let socket = UnixDatagram::unbound().unwrap(); + /// let addr = socket.local_addr().expect("Couldn't get local address"); + /// assert_eq!(addr.is_unnamed(), true); + /// ``` + #[stable(feature = "unix_socket", since = "1.10.0")] + pub fn is_unnamed(&self) -> bool { + self.0.is_unnamed() + } + /// Returns the contents of this address if it is a `pathname` address. /// /// # Examples