Skip to content

Commit a279ebb

Browse files
authoredDec 2, 2019
Rollup merge of #66346 - linkmauve:try-in-docstring, r=Dylan-DPC
Replace .unwrap() with ? in std::os::unix::net As people like to copy examples, this gives them good habits.
2 parents 4007d4e + be18a22 commit a279ebb

File tree

2 files changed

+284
-156
lines changed

2 files changed

+284
-156
lines changed
 

‎src/libstd/sys/unix/ext/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
//! use std::fs::File;
1616
//! use std::os::unix::prelude::*;
1717
//!
18-
//! fn main() {
19-
//! let f = File::create("foo.txt").unwrap();
18+
//! fn main() -> std::io::Result<()> {
19+
//! let f = File::create("foo.txt")?;
2020
//! let fd = f.as_raw_fd();
2121
//!
2222
//! // use fd with native unix bindings
23+
//!
24+
//! Ok(())
2325
//! }
2426
//! ```
2527

‎src/libstd/sys/unix/ext/net.rs

Lines changed: 280 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,25 @@ impl SocketAddr {
142142
/// ```no_run
143143
/// use std::os::unix::net::UnixListener;
144144
///
145-
/// let socket = UnixListener::bind("/tmp/sock").unwrap();
146-
/// let addr = socket.local_addr().expect("Couldn't get local address");
147-
/// assert_eq!(addr.is_unnamed(), false);
145+
/// fn main() -> std::io::Result<()> {
146+
/// let socket = UnixListener::bind("/tmp/sock")?;
147+
/// let addr = socket.local_addr().expect("Couldn't get local address");
148+
/// assert_eq!(addr.is_unnamed(), false);
149+
/// Ok(())
150+
/// }
148151
/// ```
149152
///
150153
/// An unnamed address:
151154
///
152155
/// ```
153156
/// use std::os::unix::net::UnixDatagram;
154157
///
155-
/// let socket = UnixDatagram::unbound().unwrap();
156-
/// let addr = socket.local_addr().expect("Couldn't get local address");
157-
/// assert_eq!(addr.is_unnamed(), true);
158+
/// fn main() -> std::io::Result<()> {
159+
/// let socket = UnixDatagram::unbound()?;
160+
/// let addr = socket.local_addr().expect("Couldn't get local address");
161+
/// assert_eq!(addr.is_unnamed(), true);
162+
/// Ok(())
163+
/// }
158164
/// ```
159165
#[stable(feature = "unix_socket", since = "1.10.0")]
160166
pub fn is_unnamed(&self) -> bool {
@@ -175,19 +181,25 @@ impl SocketAddr {
175181
/// use std::os::unix::net::UnixListener;
176182
/// use std::path::Path;
177183
///
178-
/// let socket = UnixListener::bind("/tmp/sock").unwrap();
179-
/// let addr = socket.local_addr().expect("Couldn't get local address");
180-
/// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
184+
/// fn main() -> std::io::Result<()> {
185+
/// let socket = UnixListener::bind("/tmp/sock")?;
186+
/// let addr = socket.local_addr().expect("Couldn't get local address");
187+
/// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
188+
/// Ok(())
189+
/// }
181190
/// ```
182191
///
183192
/// Without a pathname:
184193
///
185194
/// ```
186195
/// use std::os::unix::net::UnixDatagram;
187196
///
188-
/// let socket = UnixDatagram::unbound().unwrap();
189-
/// let addr = socket.local_addr().expect("Couldn't get local address");
190-
/// assert_eq!(addr.as_pathname(), None);
197+
/// fn main() -> std::io::Result<()> {
198+
/// let socket = UnixDatagram::unbound()?;
199+
/// let addr = socket.local_addr().expect("Couldn't get local address");
200+
/// assert_eq!(addr.as_pathname(), None);
201+
/// Ok(())
202+
/// }
191203
/// ```
192204
#[stable(feature = "unix_socket", since = "1.10.0")]
193205
pub fn as_pathname(&self) -> Option<&Path> {
@@ -247,11 +259,14 @@ impl<'a> fmt::Display for AsciiEscaped<'a> {
247259
/// use std::os::unix::net::UnixStream;
248260
/// use std::io::prelude::*;
249261
///
250-
/// let mut stream = UnixStream::connect("/path/to/my/socket").unwrap();
251-
/// stream.write_all(b"hello world").unwrap();
252-
/// let mut response = String::new();
253-
/// stream.read_to_string(&mut response).unwrap();
254-
/// println!("{}", response);
262+
/// fn main() -> std::io::Result<()> {
263+
/// let mut stream = UnixStream::connect("/path/to/my/socket")?;
264+
/// stream.write_all(b"hello world")?;
265+
/// let mut response = String::new();
266+
/// stream.read_to_string(&mut response)?;
267+
/// println!("{}", response);
268+
/// Ok(())
269+
/// }
255270
/// ```
256271
#[stable(feature = "unix_socket", since = "1.10.0")]
257272
pub struct UnixStream(Socket);
@@ -336,8 +351,11 @@ impl UnixStream {
336351
/// ```no_run
337352
/// use std::os::unix::net::UnixStream;
338353
///
339-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
340-
/// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
354+
/// fn main() -> std::io::Result<()> {
355+
/// let socket = UnixStream::connect("/tmp/sock")?;
356+
/// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
357+
/// Ok(())
358+
/// }
341359
/// ```
342360
#[stable(feature = "unix_socket", since = "1.10.0")]
343361
pub fn try_clone(&self) -> io::Result<UnixStream> {
@@ -351,8 +369,11 @@ impl UnixStream {
351369
/// ```no_run
352370
/// use std::os::unix::net::UnixStream;
353371
///
354-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
355-
/// let addr = socket.local_addr().expect("Couldn't get local address");
372+
/// fn main() -> std::io::Result<()> {
373+
/// let socket = UnixStream::connect("/tmp/sock")?;
374+
/// let addr = socket.local_addr().expect("Couldn't get local address");
375+
/// Ok(())
376+
/// }
356377
/// ```
357378
#[stable(feature = "unix_socket", since = "1.10.0")]
358379
pub fn local_addr(&self) -> io::Result<SocketAddr> {
@@ -366,8 +387,11 @@ impl UnixStream {
366387
/// ```no_run
367388
/// use std::os::unix::net::UnixStream;
368389
///
369-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
370-
/// let addr = socket.peer_addr().expect("Couldn't get peer address");
390+
/// fn main() -> std::io::Result<()> {
391+
/// let socket = UnixStream::connect("/tmp/sock")?;
392+
/// let addr = socket.peer_addr().expect("Couldn't get peer address");
393+
/// Ok(())
394+
/// }
371395
/// ```
372396
#[stable(feature = "unix_socket", since = "1.10.0")]
373397
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
@@ -391,8 +415,11 @@ impl UnixStream {
391415
/// use std::os::unix::net::UnixStream;
392416
/// use std::time::Duration;
393417
///
394-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
395-
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
418+
/// fn main() -> std::io::Result<()> {
419+
/// let socket = UnixStream::connect("/tmp/sock")?;
420+
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
421+
/// Ok(())
422+
/// }
396423
/// ```
397424
///
398425
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@@ -403,10 +430,13 @@ impl UnixStream {
403430
/// use std::os::unix::net::UnixStream;
404431
/// use std::time::Duration;
405432
///
406-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
407-
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
408-
/// let err = result.unwrap_err();
409-
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
433+
/// fn main() -> std::io::Result<()> {
434+
/// let socket = UnixStream::connect("/tmp/sock")?;
435+
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
436+
/// let err = result.unwrap_err();
437+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
438+
/// Ok(())
439+
/// }
410440
/// ```
411441
#[stable(feature = "unix_socket", since = "1.10.0")]
412442
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@@ -430,8 +460,12 @@ impl UnixStream {
430460
/// use std::os::unix::net::UnixStream;
431461
/// use std::time::Duration;
432462
///
433-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
434-
/// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
463+
/// fn main() -> std::io::Result<()> {
464+
/// let socket = UnixStream::connect("/tmp/sock")?;
465+
/// socket.set_write_timeout(Some(Duration::new(1, 0)))
466+
/// .expect("Couldn't set write timeout");
467+
/// Ok(())
468+
/// }
435469
/// ```
436470
///
437471
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@@ -442,10 +476,13 @@ impl UnixStream {
442476
/// use std::net::UdpSocket;
443477
/// use std::time::Duration;
444478
///
445-
/// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
446-
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
447-
/// let err = result.unwrap_err();
448-
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
479+
/// fn main() -> std::io::Result<()> {
480+
/// let socket = UdpSocket::bind("127.0.0.1:34254")?;
481+
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
482+
/// let err = result.unwrap_err();
483+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
484+
/// Ok(())
485+
/// }
449486
/// ```
450487
#[stable(feature = "unix_socket", since = "1.10.0")]
451488
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@@ -460,9 +497,12 @@ impl UnixStream {
460497
/// use std::os::unix::net::UnixStream;
461498
/// use std::time::Duration;
462499
///
463-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
464-
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
465-
/// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
500+
/// fn main() -> std::io::Result<()> {
501+
/// let socket = UnixStream::connect("/tmp/sock")?;
502+
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
503+
/// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
504+
/// Ok(())
505+
/// }
466506
/// ```
467507
#[stable(feature = "unix_socket", since = "1.10.0")]
468508
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
@@ -477,9 +517,13 @@ impl UnixStream {
477517
/// use std::os::unix::net::UnixStream;
478518
/// use std::time::Duration;
479519
///
480-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
481-
/// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
482-
/// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
520+
/// fn main() -> std::io::Result<()> {
521+
/// let socket = UnixStream::connect("/tmp/sock")?;
522+
/// socket.set_write_timeout(Some(Duration::new(1, 0)))
523+
/// .expect("Couldn't set write timeout");
524+
/// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
525+
/// Ok(())
526+
/// }
483527
/// ```
484528
#[stable(feature = "unix_socket", since = "1.10.0")]
485529
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
@@ -493,8 +537,11 @@ impl UnixStream {
493537
/// ```no_run
494538
/// use std::os::unix::net::UnixStream;
495539
///
496-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
497-
/// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
540+
/// fn main() -> std::io::Result<()> {
541+
/// let socket = UnixStream::connect("/tmp/sock")?;
542+
/// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
543+
/// Ok(())
544+
/// }
498545
/// ```
499546
#[stable(feature = "unix_socket", since = "1.10.0")]
500547
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@@ -508,9 +555,12 @@ impl UnixStream {
508555
/// ```no_run
509556
/// use std::os::unix::net::UnixStream;
510557
///
511-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
512-
/// if let Ok(Some(err)) = socket.take_error() {
513-
/// println!("Got error: {:?}", err);
558+
/// fn main() -> std::io::Result<()> {
559+
/// let socket = UnixStream::connect("/tmp/sock")?;
560+
/// if let Ok(Some(err)) = socket.take_error() {
561+
/// println!("Got error: {:?}", err);
562+
/// }
563+
/// Ok(())
514564
/// }
515565
/// ```
516566
///
@@ -535,8 +585,11 @@ impl UnixStream {
535585
/// use std::os::unix::net::UnixStream;
536586
/// use std::net::Shutdown;
537587
///
538-
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
539-
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
588+
/// fn main() -> std::io::Result<()> {
589+
/// let socket = UnixStream::connect("/tmp/sock")?;
590+
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
591+
/// Ok(())
592+
/// }
540593
/// ```
541594
#[stable(feature = "unix_socket", since = "1.10.0")]
542595
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
@@ -697,20 +750,23 @@ impl IntoRawFd for net::UdpSocket {
697750
/// // ...
698751
/// }
699752
///
700-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
753+
/// fn main() -> std::io::Result<()> {
754+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
701755
///
702-
/// // accept connections and process them, spawning a new thread for each one
703-
/// for stream in listener.incoming() {
704-
/// match stream {
705-
/// Ok(stream) => {
706-
/// /* connection succeeded */
707-
/// thread::spawn(|| handle_client(stream));
708-
/// }
709-
/// Err(err) => {
710-
/// /* connection failed */
711-
/// break;
756+
/// // accept connections and process them, spawning a new thread for each one
757+
/// for stream in listener.incoming() {
758+
/// match stream {
759+
/// Ok(stream) => {
760+
/// /* connection succeeded */
761+
/// thread::spawn(|| handle_client(stream));
762+
/// }
763+
/// Err(err) => {
764+
/// /* connection failed */
765+
/// break;
766+
/// }
712767
/// }
713768
/// }
769+
/// Ok(())
714770
/// }
715771
/// ```
716772
#[stable(feature = "unix_socket", since = "1.10.0")]
@@ -773,11 +829,14 @@ impl UnixListener {
773829
/// ```no_run
774830
/// use std::os::unix::net::UnixListener;
775831
///
776-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
832+
/// fn main() -> std::io::Result<()> {
833+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
777834
///
778-
/// match listener.accept() {
779-
/// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
780-
/// Err(e) => println!("accept function failed: {:?}", e),
835+
/// match listener.accept() {
836+
/// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
837+
/// Err(e) => println!("accept function failed: {:?}", e),
838+
/// }
839+
/// Ok(())
781840
/// }
782841
/// ```
783842
#[stable(feature = "unix_socket", since = "1.10.0")]
@@ -800,9 +859,11 @@ impl UnixListener {
800859
/// ```no_run
801860
/// use std::os::unix::net::UnixListener;
802861
///
803-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
804-
///
805-
/// let listener_copy = listener.try_clone().expect("try_clone failed");
862+
/// fn main() -> std::io::Result<()> {
863+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
864+
/// let listener_copy = listener.try_clone().expect("try_clone failed");
865+
/// Ok(())
866+
/// }
806867
/// ```
807868
#[stable(feature = "unix_socket", since = "1.10.0")]
808869
pub fn try_clone(&self) -> io::Result<UnixListener> {
@@ -816,9 +877,11 @@ impl UnixListener {
816877
/// ```no_run
817878
/// use std::os::unix::net::UnixListener;
818879
///
819-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
820-
///
821-
/// let addr = listener.local_addr().expect("Couldn't get local address");
880+
/// fn main() -> std::io::Result<()> {
881+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
882+
/// let addr = listener.local_addr().expect("Couldn't get local address");
883+
/// Ok(())
884+
/// }
822885
/// ```
823886
#[stable(feature = "unix_socket", since = "1.10.0")]
824887
pub fn local_addr(&self) -> io::Result<SocketAddr> {
@@ -832,9 +895,11 @@ impl UnixListener {
832895
/// ```no_run
833896
/// use std::os::unix::net::UnixListener;
834897
///
835-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
836-
///
837-
/// listener.set_nonblocking(true).expect("Couldn't set non blocking");
898+
/// fn main() -> std::io::Result<()> {
899+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
900+
/// listener.set_nonblocking(true).expect("Couldn't set non blocking");
901+
/// Ok(())
902+
/// }
838903
/// ```
839904
#[stable(feature = "unix_socket", since = "1.10.0")]
840905
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@@ -848,10 +913,13 @@ impl UnixListener {
848913
/// ```no_run
849914
/// use std::os::unix::net::UnixListener;
850915
///
851-
/// let listener = UnixListener::bind("/tmp/sock").unwrap();
916+
/// fn main() -> std::io::Result<()> {
917+
/// let listener = UnixListener::bind("/tmp/sock")?;
852918
///
853-
/// if let Ok(Some(err)) = listener.take_error() {
854-
/// println!("Got error: {:?}", err);
919+
/// if let Ok(Some(err)) = listener.take_error() {
920+
/// println!("Got error: {:?}", err);
921+
/// }
922+
/// Ok(())
855923
/// }
856924
/// ```
857925
///
@@ -880,17 +948,20 @@ impl UnixListener {
880948
/// // ...
881949
/// }
882950
///
883-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
884-
///
885-
/// for stream in listener.incoming() {
886-
/// match stream {
887-
/// Ok(stream) => {
888-
/// thread::spawn(|| handle_client(stream));
889-
/// }
890-
/// Err(err) => {
891-
/// break;
951+
/// fn main() -> std::io::Result<()> {
952+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
953+
///
954+
/// for stream in listener.incoming() {
955+
/// match stream {
956+
/// Ok(stream) => {
957+
/// thread::spawn(|| handle_client(stream));
958+
/// }
959+
/// Err(err) => {
960+
/// break;
961+
/// }
892962
/// }
893963
/// }
964+
/// Ok(())
894965
/// }
895966
/// ```
896967
#[stable(feature = "unix_socket", since = "1.10.0")]
@@ -947,17 +1018,20 @@ impl<'a> IntoIterator for &'a UnixListener {
9471018
/// // ...
9481019
/// }
9491020
///
950-
/// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
1021+
/// fn main() -> std::io::Result<()> {
1022+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
9511023
///
952-
/// for stream in listener.incoming() {
953-
/// match stream {
954-
/// Ok(stream) => {
955-
/// thread::spawn(|| handle_client(stream));
956-
/// }
957-
/// Err(err) => {
958-
/// break;
1024+
/// for stream in listener.incoming() {
1025+
/// match stream {
1026+
/// Ok(stream) => {
1027+
/// thread::spawn(|| handle_client(stream));
1028+
/// }
1029+
/// Err(err) => {
1030+
/// break;
1031+
/// }
9591032
/// }
9601033
/// }
1034+
/// Ok(())
9611035
/// }
9621036
/// ```
9631037
#[derive(Debug)]
@@ -986,11 +1060,14 @@ impl<'a> Iterator for Incoming<'a> {
9861060
/// ```no_run
9871061
/// use std::os::unix::net::UnixDatagram;
9881062
///
989-
/// let socket = UnixDatagram::bind("/path/to/my/socket").unwrap();
990-
/// socket.send_to(b"hello world", "/path/to/other/socket").unwrap();
991-
/// let mut buf = [0; 100];
992-
/// let (count, address) = socket.recv_from(&mut buf).unwrap();
993-
/// println!("socket {:?} sent {:?}", address, &buf[..count]);
1063+
/// fn main() -> std::io::Result<()> {
1064+
/// let socket = UnixDatagram::bind("/path/to/my/socket")?;
1065+
/// socket.send_to(b"hello world", "/path/to/other/socket")?;
1066+
/// let mut buf = [0; 100];
1067+
/// let (count, address) = socket.recv_from(&mut buf)?;
1068+
/// println!("socket {:?} sent {:?}", address, &buf[..count]);
1069+
/// Ok(())
1070+
/// }
9941071
/// ```
9951072
#[stable(feature = "unix_socket", since = "1.10.0")]
9961073
pub struct UnixDatagram(Socket);
@@ -1099,14 +1176,17 @@ impl UnixDatagram {
10991176
/// ```no_run
11001177
/// use std::os::unix::net::UnixDatagram;
11011178
///
1102-
/// let sock = UnixDatagram::unbound().unwrap();
1103-
/// match sock.connect("/path/to/the/socket") {
1104-
/// Ok(sock) => sock,
1105-
/// Err(e) => {
1106-
/// println!("Couldn't connect: {:?}", e);
1107-
/// return
1108-
/// }
1109-
/// };
1179+
/// fn main() -> std::io::Result<()> {
1180+
/// let sock = UnixDatagram::unbound()?;
1181+
/// match sock.connect("/path/to/the/socket") {
1182+
/// Ok(sock) => sock,
1183+
/// Err(e) => {
1184+
/// println!("Couldn't connect: {:?}", e);
1185+
/// return Err(e)
1186+
/// }
1187+
/// };
1188+
/// Ok(())
1189+
/// }
11101190
/// ```
11111191
#[stable(feature = "unix_socket", since = "1.10.0")]
11121192
pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
@@ -1133,9 +1213,11 @@ impl UnixDatagram {
11331213
/// ```no_run
11341214
/// use std::os::unix::net::UnixDatagram;
11351215
///
1136-
/// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1137-
///
1138-
/// let sock_copy = sock.try_clone().expect("try_clone failed");
1216+
/// fn main() -> std::io::Result<()> {
1217+
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1218+
/// let sock_copy = sock.try_clone().expect("try_clone failed");
1219+
/// Ok(())
1220+
/// }
11391221
/// ```
11401222
#[stable(feature = "unix_socket", since = "1.10.0")]
11411223
pub fn try_clone(&self) -> io::Result<UnixDatagram> {
@@ -1149,9 +1231,11 @@ impl UnixDatagram {
11491231
/// ```no_run
11501232
/// use std::os::unix::net::UnixDatagram;
11511233
///
1152-
/// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1153-
///
1154-
/// let addr = sock.local_addr().expect("Couldn't get local address");
1234+
/// fn main() -> std::io::Result<()> {
1235+
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1236+
/// let addr = sock.local_addr().expect("Couldn't get local address");
1237+
/// Ok(())
1238+
/// }
11551239
/// ```
11561240
#[stable(feature = "unix_socket", since = "1.10.0")]
11571241
pub fn local_addr(&self) -> io::Result<SocketAddr> {
@@ -1169,10 +1253,13 @@ impl UnixDatagram {
11691253
/// ```no_run
11701254
/// use std::os::unix::net::UnixDatagram;
11711255
///
1172-
/// let sock = UnixDatagram::unbound().unwrap();
1173-
/// sock.connect("/path/to/the/socket").unwrap();
1256+
/// fn main() -> std::io::Result<()> {
1257+
/// let sock = UnixDatagram::unbound()?;
1258+
/// sock.connect("/path/to/the/socket")?;
11741259
///
1175-
/// let addr = sock.peer_addr().expect("Couldn't get peer address");
1260+
/// let addr = sock.peer_addr().expect("Couldn't get peer address");
1261+
/// Ok(())
1262+
/// }
11761263
/// ```
11771264
#[stable(feature = "unix_socket", since = "1.10.0")]
11781265
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
@@ -1189,11 +1276,12 @@ impl UnixDatagram {
11891276
/// ```no_run
11901277
/// use std::os::unix::net::UnixDatagram;
11911278
///
1192-
/// let sock = UnixDatagram::unbound().unwrap();
1193-
/// let mut buf = vec![0; 10];
1194-
/// match sock.recv_from(buf.as_mut_slice()) {
1195-
/// Ok((size, sender)) => println!("received {} bytes from {:?}", size, sender),
1196-
/// Err(e) => println!("recv_from function failed: {:?}", e),
1279+
/// fn main() -> std::io::Result<()> {
1280+
/// let sock = UnixDatagram::unbound()?;
1281+
/// let mut buf = vec![0; 10];
1282+
/// let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
1283+
/// println!("received {} bytes from {:?}", size, sender);
1284+
/// Ok(())
11971285
/// }
11981286
/// ```
11991287
#[stable(feature = "unix_socket", since = "1.10.0")]
@@ -1229,9 +1317,12 @@ impl UnixDatagram {
12291317
/// ```no_run
12301318
/// use std::os::unix::net::UnixDatagram;
12311319
///
1232-
/// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1233-
/// let mut buf = vec![0; 10];
1234-
/// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1320+
/// fn main() -> std::io::Result<()> {
1321+
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1322+
/// let mut buf = vec![0; 10];
1323+
/// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1324+
/// Ok(())
1325+
/// }
12351326
/// ```
12361327
#[stable(feature = "unix_socket", since = "1.10.0")]
12371328
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
@@ -1247,8 +1338,11 @@ impl UnixDatagram {
12471338
/// ```no_run
12481339
/// use std::os::unix::net::UnixDatagram;
12491340
///
1250-
/// let sock = UnixDatagram::unbound().unwrap();
1251-
/// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1341+
/// fn main() -> std::io::Result<()> {
1342+
/// let sock = UnixDatagram::unbound()?;
1343+
/// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1344+
/// Ok(())
1345+
/// }
12521346
/// ```
12531347
#[stable(feature = "unix_socket", since = "1.10.0")]
12541348
pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
@@ -1280,9 +1374,12 @@ impl UnixDatagram {
12801374
/// ```no_run
12811375
/// use std::os::unix::net::UnixDatagram;
12821376
///
1283-
/// let sock = UnixDatagram::unbound().unwrap();
1284-
/// sock.connect("/some/sock").expect("Couldn't connect");
1285-
/// sock.send(b"omelette au fromage").expect("send_to function failed");
1377+
/// fn main() -> std::io::Result<()> {
1378+
/// let sock = UnixDatagram::unbound()?;
1379+
/// sock.connect("/some/sock").expect("Couldn't connect");
1380+
/// sock.send(b"omelette au fromage").expect("send_to function failed");
1381+
/// Ok(())
1382+
/// }
12861383
/// ```
12871384
#[stable(feature = "unix_socket", since = "1.10.0")]
12881385
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
@@ -1307,8 +1404,12 @@ impl UnixDatagram {
13071404
/// use std::os::unix::net::UnixDatagram;
13081405
/// use std::time::Duration;
13091406
///
1310-
/// let sock = UnixDatagram::unbound().unwrap();
1311-
/// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1407+
/// fn main() -> std::io::Result<()> {
1408+
/// let sock = UnixDatagram::unbound()?;
1409+
/// sock.set_read_timeout(Some(Duration::new(1, 0)))
1410+
/// .expect("set_read_timeout function failed");
1411+
/// Ok(())
1412+
/// }
13121413
/// ```
13131414
///
13141415
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@@ -1319,10 +1420,13 @@ impl UnixDatagram {
13191420
/// use std::os::unix::net::UnixDatagram;
13201421
/// use std::time::Duration;
13211422
///
1322-
/// let socket = UnixDatagram::unbound().unwrap();
1323-
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1324-
/// let err = result.unwrap_err();
1325-
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1423+
/// fn main() -> std::io::Result<()> {
1424+
/// let socket = UnixDatagram::unbound()?;
1425+
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1426+
/// let err = result.unwrap_err();
1427+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1428+
/// Ok(())
1429+
/// }
13261430
/// ```
13271431
#[stable(feature = "unix_socket", since = "1.10.0")]
13281432
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@@ -1346,9 +1450,12 @@ impl UnixDatagram {
13461450
/// use std::os::unix::net::UnixDatagram;
13471451
/// use std::time::Duration;
13481452
///
1349-
/// let sock = UnixDatagram::unbound().unwrap();
1350-
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
1351-
/// .expect("set_write_timeout function failed");
1453+
/// fn main() -> std::io::Result<()> {
1454+
/// let sock = UnixDatagram::unbound()?;
1455+
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
1456+
/// .expect("set_write_timeout function failed");
1457+
/// Ok(())
1458+
/// }
13521459
/// ```
13531460
///
13541461
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
@@ -1359,10 +1466,13 @@ impl UnixDatagram {
13591466
/// use std::os::unix::net::UnixDatagram;
13601467
/// use std::time::Duration;
13611468
///
1362-
/// let socket = UnixDatagram::unbound().unwrap();
1363-
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1364-
/// let err = result.unwrap_err();
1365-
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1469+
/// fn main() -> std::io::Result<()> {
1470+
/// let socket = UnixDatagram::unbound()?;
1471+
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1472+
/// let err = result.unwrap_err();
1473+
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1474+
/// Ok(())
1475+
/// }
13661476
/// ```
13671477
#[stable(feature = "unix_socket", since = "1.10.0")]
13681478
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
@@ -1377,9 +1487,13 @@ impl UnixDatagram {
13771487
/// use std::os::unix::net::UnixDatagram;
13781488
/// use std::time::Duration;
13791489
///
1380-
/// let sock = UnixDatagram::unbound().unwrap();
1381-
/// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1382-
/// assert_eq!(sock.read_timeout().unwrap(), Some(Duration::new(1, 0)));
1490+
/// fn main() -> std::io::Result<()> {
1491+
/// let sock = UnixDatagram::unbound()?;
1492+
/// sock.set_read_timeout(Some(Duration::new(1, 0)))
1493+
/// .expect("set_read_timeout function failed");
1494+
/// assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
1495+
/// Ok(())
1496+
/// }
13831497
/// ```
13841498
#[stable(feature = "unix_socket", since = "1.10.0")]
13851499
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
@@ -1394,10 +1508,13 @@ impl UnixDatagram {
13941508
/// use std::os::unix::net::UnixDatagram;
13951509
/// use std::time::Duration;
13961510
///
1397-
/// let sock = UnixDatagram::unbound().unwrap();
1398-
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
1399-
/// .expect("set_write_timeout function failed");
1400-
/// assert_eq!(sock.write_timeout().unwrap(), Some(Duration::new(1, 0)));
1511+
/// fn main() -> std::io::Result<()> {
1512+
/// let sock = UnixDatagram::unbound()?;
1513+
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
1514+
/// .expect("set_write_timeout function failed");
1515+
/// assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
1516+
/// Ok(())
1517+
/// }
14011518
/// ```
14021519
#[stable(feature = "unix_socket", since = "1.10.0")]
14031520
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
@@ -1411,8 +1528,11 @@ impl UnixDatagram {
14111528
/// ```
14121529
/// use std::os::unix::net::UnixDatagram;
14131530
///
1414-
/// let sock = UnixDatagram::unbound().unwrap();
1415-
/// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1531+
/// fn main() -> std::io::Result<()> {
1532+
/// let sock = UnixDatagram::unbound()?;
1533+
/// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1534+
/// Ok(())
1535+
/// }
14161536
/// ```
14171537
#[stable(feature = "unix_socket", since = "1.10.0")]
14181538
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@@ -1426,9 +1546,12 @@ impl UnixDatagram {
14261546
/// ```no_run
14271547
/// use std::os::unix::net::UnixDatagram;
14281548
///
1429-
/// let sock = UnixDatagram::unbound().unwrap();
1430-
/// if let Ok(Some(err)) = sock.take_error() {
1431-
/// println!("Got error: {:?}", err);
1549+
/// fn main() -> std::io::Result<()> {
1550+
/// let sock = UnixDatagram::unbound()?;
1551+
/// if let Ok(Some(err)) = sock.take_error() {
1552+
/// println!("Got error: {:?}", err);
1553+
/// }
1554+
/// Ok(())
14321555
/// }
14331556
/// ```
14341557
#[stable(feature = "unix_socket", since = "1.10.0")]
@@ -1448,8 +1571,11 @@ impl UnixDatagram {
14481571
/// use std::os::unix::net::UnixDatagram;
14491572
/// use std::net::Shutdown;
14501573
///
1451-
/// let sock = UnixDatagram::unbound().unwrap();
1452-
/// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1574+
/// fn main() -> std::io::Result<()> {
1575+
/// let sock = UnixDatagram::unbound()?;
1576+
/// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1577+
/// Ok(())
1578+
/// }
14531579
/// ```
14541580
#[stable(feature = "unix_socket", since = "1.10.0")]
14551581
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {

0 commit comments

Comments
 (0)
Please sign in to comment.