Replace .unwrap() with ? in std::os::unix::net

This commit is contained in:
Emmanuel Gil Peyrot 2019-11-12 22:07:39 +01:00
parent 7d761fe046
commit 8f158bc62b
2 changed files with 236 additions and 148 deletions

View file

@ -16,7 +16,7 @@
//! use std::os::unix::prelude::*;
//!
//! fn main() {
//! let f = File::create("foo.txt").unwrap();
//! let f = File::create("foo.txt")?;
//! let fd = f.as_raw_fd();
//!
//! // use fd with native unix bindings

View file

@ -142,9 +142,11 @@ impl SocketAddr {
/// ```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);
/// fn main() -> std::io::Result<()> {
/// let socket = UnixListener::bind("/tmp/sock")?;
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// assert_eq!(addr.is_unnamed(), false);
/// }
/// ```
///
/// An unnamed address:
@ -152,9 +154,11 @@ impl SocketAddr {
/// ```
/// 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);
/// fn main() -> std::io::Result<()> {
/// let socket = UnixDatagram::unbound()?;
/// 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 {
@ -175,9 +179,11 @@ impl SocketAddr {
/// use std::os::unix::net::UnixListener;
/// use std::path::Path;
///
/// let socket = UnixListener::bind("/tmp/sock").unwrap();
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
/// fn main() -> std::io::Result<()> {
/// let socket = UnixListener::bind("/tmp/sock")?;
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
/// }
/// ```
///
/// Without a pathname:
@ -185,9 +191,11 @@ impl SocketAddr {
/// ```
/// 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.as_pathname(), None);
/// fn main() -> std::io::Result<()> {
/// let socket = UnixDatagram::unbound()?;
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// assert_eq!(addr.as_pathname(), None);
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn as_pathname(&self) -> Option<&Path> {
@ -247,11 +255,13 @@ impl<'a> fmt::Display for AsciiEscaped<'a> {
/// use std::os::unix::net::UnixStream;
/// use std::io::prelude::*;
///
/// let mut stream = UnixStream::connect("/path/to/my/socket").unwrap();
/// stream.write_all(b"hello world").unwrap();
/// let mut response = String::new();
/// stream.read_to_string(&mut response).unwrap();
/// println!("{}", response);
/// fn main() -> std::io::Result<()> {
/// let mut stream = UnixStream::connect("/path/to/my/socket")?;
/// stream.write_all(b"hello world")?;
/// let mut response = String::new();
/// stream.read_to_string(&mut response)?;
/// println!("{}", response);
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub struct UnixStream(Socket);
@ -336,8 +346,10 @@ impl UnixStream {
/// ```no_run
/// use std::os::unix::net::UnixStream;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn try_clone(&self) -> io::Result<UnixStream> {
@ -351,8 +363,10 @@ impl UnixStream {
/// ```no_run
/// use std::os::unix::net::UnixStream;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
@ -366,8 +380,10 @@ impl UnixStream {
/// ```no_run
/// use std::os::unix::net::UnixStream;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let addr = socket.peer_addr().expect("Couldn't get peer address");
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// let addr = socket.peer_addr().expect("Couldn't get peer address");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
@ -391,7 +407,7 @@ impl UnixStream {
/// use std::os::unix::net::UnixStream;
/// use std::time::Duration;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let socket = UnixStream::connect("/tmp/sock")?;
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
/// ```
///
@ -403,10 +419,12 @@ impl UnixStream {
/// use std::os::unix::net::UnixStream;
/// use std::time::Duration;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@ -430,8 +448,11 @@ impl UnixStream {
/// use std::os::unix::net::UnixStream;
/// use std::time::Duration;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// socket.set_write_timeout(Some(Duration::new(1, 0)))
/// .expect("Couldn't set write timeout");
/// }
/// ```
///
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@ -442,10 +463,12 @@ impl UnixStream {
/// use std::net::UdpSocket;
/// use std::time::Duration;
///
/// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// fn main() -> std::io::Result<()> {
/// let socket = UdpSocket::bind("127.0.0.1:34254")?;
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@ -460,9 +483,11 @@ impl UnixStream {
/// use std::os::unix::net::UnixStream;
/// use std::time::Duration;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
/// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
/// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
@ -477,9 +502,12 @@ impl UnixStream {
/// use std::os::unix::net::UnixStream;
/// use std::time::Duration;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
/// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// socket.set_write_timeout(Some(Duration::new(1, 0)))
/// .expect("Couldn't set write timeout");
/// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
@ -493,8 +521,10 @@ impl UnixStream {
/// ```no_run
/// use std::os::unix::net::UnixStream;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@ -508,9 +538,11 @@ impl UnixStream {
/// ```no_run
/// use std::os::unix::net::UnixStream;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// if let Ok(Some(err)) = socket.take_error() {
/// println!("Got error: {:?}", err);
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?;
/// if let Ok(Some(err)) = socket.take_error() {
/// println!("Got error: {:?}", err);
/// }
/// }
/// ```
///
@ -535,8 +567,10 @@ impl UnixStream {
/// use std::os::unix::net::UnixStream;
/// use std::net::Shutdown;
///
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
/// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock");
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
@ -697,18 +731,20 @@ impl IntoRawFd for net::UdpSocket {
/// // ...
/// }
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// // accept connections and process them, spawning a new thread for each one
/// for stream in listener.incoming() {
/// match stream {
/// Ok(stream) => {
/// /* connection succeeded */
/// thread::spawn(|| handle_client(stream));
/// }
/// Err(err) => {
/// /* connection failed */
/// break;
/// // accept connections and process them, spawning a new thread for each one
/// for stream in listener.incoming() {
/// match stream {
/// Ok(stream) => {
/// /* connection succeeded */
/// thread::spawn(|| handle_client(stream));
/// }
/// Err(err) => {
/// /* connection failed */
/// break;
/// }
/// }
/// }
/// }
@ -773,11 +809,13 @@ impl UnixListener {
/// ```no_run
/// use std::os::unix::net::UnixListener;
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// match listener.accept() {
/// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
/// Err(e) => println!("accept function failed: {:?}", e),
/// match listener.accept() {
/// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
/// Err(e) => println!("accept function failed: {:?}", e),
/// }
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
@ -800,9 +838,11 @@ impl UnixListener {
/// ```no_run
/// use std::os::unix::net::UnixListener;
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// let listener_copy = listener.try_clone().expect("try_clone failed");
/// let listener_copy = listener.try_clone().expect("try_clone failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn try_clone(&self) -> io::Result<UnixListener> {
@ -816,9 +856,11 @@ impl UnixListener {
/// ```no_run
/// use std::os::unix::net::UnixListener;
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// let addr = listener.local_addr().expect("Couldn't get local address");
/// let addr = listener.local_addr().expect("Couldn't get local address");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
@ -832,9 +874,11 @@ impl UnixListener {
/// ```no_run
/// use std::os::unix::net::UnixListener;
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// listener.set_nonblocking(true).expect("Couldn't set non blocking");
/// listener.set_nonblocking(true).expect("Couldn't set non blocking");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@ -848,10 +892,12 @@ impl UnixListener {
/// ```no_run
/// use std::os::unix::net::UnixListener;
///
/// let listener = UnixListener::bind("/tmp/sock").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/tmp/sock")?;
///
/// if let Ok(Some(err)) = listener.take_error() {
/// println!("Got error: {:?}", err);
/// if let Ok(Some(err)) = listener.take_error() {
/// println!("Got error: {:?}", err);
/// }
/// }
/// ```
///
@ -880,15 +926,17 @@ impl UnixListener {
/// // ...
/// }
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// for stream in listener.incoming() {
/// match stream {
/// Ok(stream) => {
/// thread::spawn(|| handle_client(stream));
/// }
/// Err(err) => {
/// break;
/// for stream in listener.incoming() {
/// match stream {
/// Ok(stream) => {
/// thread::spawn(|| handle_client(stream));
/// }
/// Err(err) => {
/// break;
/// }
/// }
/// }
/// }
@ -947,15 +995,17 @@ impl<'a> IntoIterator for &'a UnixListener {
/// // ...
/// }
///
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
///
/// for stream in listener.incoming() {
/// match stream {
/// Ok(stream) => {
/// thread::spawn(|| handle_client(stream));
/// }
/// Err(err) => {
/// break;
/// for stream in listener.incoming() {
/// match stream {
/// Ok(stream) => {
/// thread::spawn(|| handle_client(stream));
/// }
/// Err(err) => {
/// break;
/// }
/// }
/// }
/// }
@ -986,11 +1036,13 @@ impl<'a> Iterator for Incoming<'a> {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let socket = UnixDatagram::bind("/path/to/my/socket").unwrap();
/// socket.send_to(b"hello world", "/path/to/other/socket").unwrap();
/// let mut buf = [0; 100];
/// let (count, address) = socket.recv_from(&mut buf).unwrap();
/// println!("socket {:?} sent {:?}", address, &buf[..count]);
/// fn main() -> std::io::Result<()> {
/// let socket = UnixDatagram::bind("/path/to/my/socket")?;
/// socket.send_to(b"hello world", "/path/to/other/socket")?;
/// let mut buf = [0; 100];
/// let (count, address) = socket.recv_from(&mut buf)?;
/// println!("socket {:?} sent {:?}", address, &buf[..count]);
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub struct UnixDatagram(Socket);
@ -1099,14 +1151,16 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// match sock.connect("/path/to/the/socket") {
/// Ok(sock) => sock,
/// Err(e) => {
/// println!("Couldn't connect: {:?}", e);
/// return
/// }
/// };
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// match sock.connect("/path/to/the/socket") {
/// Ok(sock) => sock,
/// Err(e) => {
/// println!("Couldn't connect: {:?}", e);
/// return
/// }
/// };
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
@ -1133,9 +1187,11 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
///
/// let sock_copy = sock.try_clone().expect("try_clone failed");
/// let sock_copy = sock.try_clone().expect("try_clone failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn try_clone(&self) -> io::Result<UnixDatagram> {
@ -1149,9 +1205,11 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
///
/// let addr = sock.local_addr().expect("Couldn't get local address");
/// let addr = sock.local_addr().expect("Couldn't get local address");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
@ -1169,10 +1227,12 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.connect("/path/to/the/socket").unwrap();
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.connect("/path/to/the/socket")?;
///
/// let addr = sock.peer_addr().expect("Couldn't get peer address");
/// let addr = sock.peer_addr().expect("Couldn't get peer address");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
@ -1189,11 +1249,13 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// let mut buf = vec![0; 10];
/// match sock.recv_from(buf.as_mut_slice()) {
/// Ok((size, sender)) => println!("received {} bytes from {:?}", size, sender),
/// Err(e) => println!("recv_from function failed: {:?}", e),
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// let mut buf = vec![0; 10];
/// match sock.recv_from(buf.as_mut_slice()) {
/// Ok((size, sender)) => println!("received {} bytes from {:?}", size, sender),
/// Err(e) => println!("recv_from function failed: {:?}", e),
/// }
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
@ -1229,9 +1291,11 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
/// let mut buf = vec![0; 10];
/// sock.recv(buf.as_mut_slice()).expect("recv function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
/// let mut buf = vec![0; 10];
/// sock.recv(buf.as_mut_slice()).expect("recv function failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
@ -1247,8 +1311,10 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
@ -1280,9 +1346,11 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.connect("/some/sock").expect("Couldn't connect");
/// sock.send(b"omelette au fromage").expect("send_to function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.connect("/some/sock").expect("Couldn't connect");
/// sock.send(b"omelette au fromage").expect("send_to function failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
@ -1307,8 +1375,11 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::time::Duration;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_read_timeout(Some(Duration::new(1, 0)))
/// .expect("set_read_timeout function failed");
/// }
/// ```
///
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@ -1319,10 +1390,12 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::time::Duration;
///
/// let socket = UnixDatagram::unbound().unwrap();
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// fn main() -> std::io::Result<()> {
/// let socket = UnixDatagram::unbound()?;
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@ -1346,9 +1419,11 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::time::Duration;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
/// .expect("set_write_timeout function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
/// .expect("set_write_timeout function failed");
/// }
/// ```
///
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@ -1359,10 +1434,12 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::time::Duration;
///
/// let socket = UnixDatagram::unbound().unwrap();
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// fn main() -> std::io::Result<()> {
/// let socket = UnixDatagram::unbound()?;
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@ -1377,9 +1454,12 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::time::Duration;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
/// assert_eq!(sock.read_timeout().unwrap(), Some(Duration::new(1, 0)));
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_read_timeout(Some(Duration::new(1, 0)))
/// .expect("set_read_timeout function failed");
/// assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
@ -1394,10 +1474,12 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::time::Duration;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
/// .expect("set_write_timeout function failed");
/// assert_eq!(sock.write_timeout().unwrap(), Some(Duration::new(1, 0)));
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
/// .expect("set_write_timeout function failed");
/// assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
@ -1411,8 +1493,10 @@ impl UnixDatagram {
/// ```
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.set_nonblocking(true).expect("set_nonblocking function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_nonblocking(true).expect("set_nonblocking function failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@ -1426,9 +1510,11 @@ impl UnixDatagram {
/// ```no_run
/// use std::os::unix::net::UnixDatagram;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// if let Ok(Some(err)) = sock.take_error() {
/// println!("Got error: {:?}", err);
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// if let Ok(Some(err)) = sock.take_error() {
/// println!("Got error: {:?}", err);
/// }
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
@ -1448,8 +1534,10 @@ impl UnixDatagram {
/// use std::os::unix::net::UnixDatagram;
/// use std::net::Shutdown;
///
/// let sock = UnixDatagram::unbound().unwrap();
/// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
/// }
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {