@@ -218,7 +218,7 @@ impl<'a> fmt::Display for AsciiEscaped<'a> {
218
218
///
219
219
/// # Examples
220
220
///
221
- /// ```rust, no_run
221
+ /// ```no_run
222
222
/// use std::os::unix::net::UnixStream;
223
223
/// use std::io::prelude::*;
224
224
///
@@ -248,6 +248,20 @@ impl fmt::Debug for UnixStream {
248
248
249
249
impl UnixStream {
250
250
/// Connects to the socket named by `path`.
251
+ ///
252
+ /// # Examples
253
+ ///
254
+ /// ```no_run
255
+ /// use std::os::unix::net::UnixStream;
256
+ ///
257
+ /// let socket = match UnixStream::connect("/tmp/sock") {
258
+ /// Ok(sock) => sock,
259
+ /// Err(e) => {
260
+ /// println!("Couldn't connect: {:?}", e);
261
+ /// return
262
+ /// }
263
+ /// };
264
+ /// ```
251
265
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
252
266
pub fn connect < P : AsRef < Path > > ( path : P ) -> io:: Result < UnixStream > {
253
267
fn inner ( path : & Path ) -> io:: Result < UnixStream > {
@@ -265,6 +279,20 @@ impl UnixStream {
265
279
/// Creates an unnamed pair of connected sockets.
266
280
///
267
281
/// Returns two `UnixStream`s which are connected to each other.
282
+ ///
283
+ /// # Examples
284
+ ///
285
+ /// ```no_run
286
+ /// use std::os::unix::net::UnixStream;
287
+ ///
288
+ /// let (sock1, sock2) = match UnixStream::pair() {
289
+ /// Ok((sock1, sock2)) => (sock1, sock2),
290
+ /// Err(e) => {
291
+ /// println!("Couldn't create a pair of sockets: {:?}", e);
292
+ /// return
293
+ /// }
294
+ /// };
295
+ /// ```
268
296
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
269
297
pub fn pair ( ) -> io:: Result < ( UnixStream , UnixStream ) > {
270
298
let ( i1, i2) = Socket :: new_pair ( libc:: AF_UNIX , libc:: SOCK_STREAM ) ?;
@@ -277,62 +305,161 @@ impl UnixStream {
277
305
/// object references. Both handles will read and write the same stream of
278
306
/// data, and options set on one stream will be propogated to the other
279
307
/// stream.
308
+ ///
309
+ /// # Examples
310
+ ///
311
+ /// ```no_run
312
+ /// use std::os::unix::net::UnixStream;
313
+ ///
314
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
315
+ /// let sock_copy = socket.try_clone().expect("Couldn't clone socket...");
316
+ /// ```
280
317
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
281
318
pub fn try_clone ( & self ) -> io:: Result < UnixStream > {
282
319
self . 0 . duplicate ( ) . map ( UnixStream )
283
320
}
284
321
285
322
/// Returns the socket address of the local half of this connection.
323
+ ///
324
+ /// # Examples
325
+ ///
326
+ /// ```no_run
327
+ /// use std::os::unix::net::UnixStream;
328
+ ///
329
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
330
+ /// let addr = socket.local_addr().expect("Couldn't get local address");
331
+ /// ```
286
332
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
287
333
pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
288
334
SocketAddr :: new ( |addr, len| unsafe { libc:: getsockname ( * self . 0 . as_inner ( ) , addr, len) } )
289
335
}
290
336
291
337
/// Returns the socket address of the remote half of this connection.
338
+ ///
339
+ /// # Examples
340
+ ///
341
+ /// ```no_run
342
+ /// use std::os::unix::net::UnixStream;
343
+ ///
344
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
345
+ /// let addr = socket.peer_addr().expect("Couldn't get peer address");
346
+ /// ```
292
347
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
293
348
pub fn peer_addr ( & self ) -> io:: Result < SocketAddr > {
294
349
SocketAddr :: new ( |addr, len| unsafe { libc:: getpeername ( * self . 0 . as_inner ( ) , addr, len) } )
295
350
}
296
351
297
352
/// Sets the read timeout for the socket.
298
353
///
299
- /// If the provided value is `None`, then `read` calls will block
300
- /// indefinitely. It is an error to pass the zero `Duration` to this
354
+ /// If the provided value is [ `None`] , then [ `read()`] calls will block
355
+ /// indefinitely. It is an error to pass the zero [ `Duration`] to this
301
356
/// method.
357
+ ///
358
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
359
+ /// [`read()`]: ../../std/io/trait.Read.html#tymethod.read
360
+ /// [`Duration`]: ../../std/time/struct.Duration.html
361
+ ///
362
+ /// # Examples
363
+ ///
364
+ /// ```no_run
365
+ /// use std::os::unix::net::UnixStream;
366
+ /// use std::time::Duration;
367
+ ///
368
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
369
+ /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
370
+ /// ```
302
371
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
303
372
pub fn set_read_timeout ( & self , timeout : Option < Duration > ) -> io:: Result < ( ) > {
304
373
self . 0 . set_timeout ( timeout, libc:: SO_RCVTIMEO )
305
374
}
306
375
307
376
/// Sets the write timeout for the socket.
308
377
///
309
- /// If the provided value is `None`, then `write` calls will block
310
- /// indefinitely. It is an error to pass the zero `Duration` to this
378
+ /// If the provided value is [ `None`] , then [ `write()`] calls will block
379
+ /// indefinitely. It is an error to pass the zero [ `Duration`] to this
311
380
/// method.
381
+ ///
382
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
383
+ /// [`read()`]: ../../std/io/trait.Write.html#tymethod.write
384
+ /// [`Duration`]: ../../std/time/struct.Duration.html
385
+ ///
386
+ /// # Examples
387
+ ///
388
+ /// ```no_run
389
+ /// use std::os::unix::net::UnixStream;
390
+ /// use std::time::Duration;
391
+ ///
392
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
393
+ /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
394
+ /// ```
312
395
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
313
396
pub fn set_write_timeout ( & self , timeout : Option < Duration > ) -> io:: Result < ( ) > {
314
397
self . 0 . set_timeout ( timeout, libc:: SO_SNDTIMEO )
315
398
}
316
399
317
400
/// Returns the read timeout of this socket.
401
+ ///
402
+ /// # Examples
403
+ ///
404
+ /// ```no_run
405
+ /// use std::os::unix::net::UnixStream;
406
+ /// use std::time::Duration;
407
+ ///
408
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
409
+ /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
410
+ /// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
411
+ /// ```
318
412
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
319
413
pub fn read_timeout ( & self ) -> io:: Result < Option < Duration > > {
320
414
self . 0 . timeout ( libc:: SO_RCVTIMEO )
321
415
}
322
416
323
417
/// Returns the write timeout of this socket.
418
+ ///
419
+ /// # Examples
420
+ ///
421
+ /// ```no_run
422
+ /// use std::os::unix::net::UnixStream;
423
+ /// use std::time::Duration;
424
+ ///
425
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
426
+ /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
427
+ /// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
428
+ /// ```
324
429
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
325
430
pub fn write_timeout ( & self ) -> io:: Result < Option < Duration > > {
326
431
self . 0 . timeout ( libc:: SO_SNDTIMEO )
327
432
}
328
433
329
434
/// Moves the socket into or out of nonblocking mode.
435
+ ///
436
+ /// # Examples
437
+ ///
438
+ /// ```no_run
439
+ /// use std::os::unix::net::UnixStream;
440
+ /// use std::time::Duration;
441
+ ///
442
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
443
+ /// socket.set_nonblocking(true).expect("Couldn't set non blocking");
444
+ /// ```
330
445
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
331
446
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
332
447
self . 0 . set_nonblocking ( nonblocking)
333
448
}
334
449
335
450
/// Returns the value of the `SO_ERROR` option.
451
+ ///
452
+ /// # Examples
453
+ ///
454
+ /// ```no_run
455
+ /// use std::os::unix::net::UnixStream;
456
+ /// use std::time::Duration;
457
+ ///
458
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
459
+ /// if let Ok(Some(err)) = socket.take_error() {
460
+ /// println!("Got error: {:?}", err);
461
+ /// }
462
+ /// ```
336
463
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
337
464
pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
338
465
self . 0 . take_error ( )
@@ -342,7 +469,19 @@ impl UnixStream {
342
469
///
343
470
/// This function will cause all pending and future I/O calls on the
344
471
/// specified portions to immediately return with an appropriate value
345
- /// (see the documentation of `Shutdown`).
472
+ /// (see the documentation of [`Shutdown`]).
473
+ ///
474
+ /// [`Shutdown`]: ../../std/net/enum.Shutdown.html
475
+ ///
476
+ /// # Examples
477
+ ///
478
+ /// ```no_run
479
+ /// use std::os::unix::net::UnixStream;
480
+ /// use std::time::Duration;
481
+ ///
482
+ /// let socket = match UnixStream::connect("/tmp/sock").unwrap();
483
+ /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
484
+ /// ```
346
485
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
347
486
pub fn shutdown ( & self , how : Shutdown ) -> io:: Result < ( ) > {
348
487
self . 0 . shutdown ( how)
0 commit comments