@@ -437,7 +437,6 @@ impl UnixStream {
437
437
///
438
438
/// ```no_run
439
439
/// use std::os::unix::net::UnixStream;
440
- /// use std::time::Duration;
441
440
///
442
441
/// let socket = match UnixStream::connect("/tmp/sock").unwrap();
443
442
/// socket.set_nonblocking(true).expect("Couldn't set non blocking");
@@ -453,7 +452,6 @@ impl UnixStream {
453
452
///
454
453
/// ```no_run
455
454
/// use std::os::unix::net::UnixStream;
456
- /// use std::time::Duration;
457
455
///
458
456
/// let socket = match UnixStream::connect("/tmp/sock").unwrap();
459
457
/// if let Ok(Some(err)) = socket.take_error() {
@@ -477,7 +475,6 @@ impl UnixStream {
477
475
///
478
476
/// ```no_run
479
477
/// use std::os::unix::net::UnixStream;
480
- /// use std::time::Duration;
481
478
///
482
479
/// let socket = match UnixStream::connect("/tmp/sock").unwrap();
483
480
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
@@ -557,7 +554,7 @@ impl IntoRawFd for UnixStream {
557
554
///
558
555
/// # Examples
559
556
///
560
- /// ```rust, no_run
557
+ /// ```no_run
561
558
/// use std::thread;
562
559
/// use std::os::unix::net::{UnixStream, UnixListener};
563
560
///
@@ -580,9 +577,6 @@ impl IntoRawFd for UnixStream {
580
577
/// }
581
578
/// }
582
579
/// }
583
- ///
584
- /// // close the listener socket
585
- /// drop(listener);
586
580
/// ```
587
581
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
588
582
pub struct UnixListener ( Socket ) ;
@@ -601,6 +595,20 @@ impl fmt::Debug for UnixListener {
601
595
602
596
impl UnixListener {
603
597
/// Creates a new `UnixListener` bound to the specified socket.
598
+ ///
599
+ /// # Examples
600
+ ///
601
+ /// ```
602
+ /// use std::os::unix::net::UnixListener;
603
+ ///
604
+ /// let listener = match UnixListener::bind("/path/to/the/socket") {
605
+ /// Ok(sock) => sock,
606
+ /// Err(e) => {
607
+ /// println!("Couldn't connect: {:?}", e);
608
+ /// return
609
+ /// }
610
+ /// };
611
+ /// ```
604
612
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
605
613
pub fn bind < P : AsRef < Path > > ( path : P ) -> io:: Result < UnixListener > {
606
614
fn inner ( path : & Path ) -> io:: Result < UnixListener > {
@@ -620,8 +628,23 @@ impl UnixListener {
620
628
/// Accepts a new incoming connection to this listener.
621
629
///
622
630
/// This function will block the calling thread until a new Unix connection
623
- /// is established. When established, the corersponding `UnixStream` and
631
+ /// is established. When established, the corersponding [ `UnixStream`] and
624
632
/// the remote peer's address will be returned.
633
+ ///
634
+ /// [`UnixStream`]: ../../std/os/unix/net/struct.UnixStream.html
635
+ ///
636
+ /// # Examples
637
+ ///
638
+ /// ```no_run
639
+ /// use std::os::unix::net::UnixListener;
640
+ ///
641
+ /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
642
+ ///
643
+ /// match listener.accept() {
644
+ /// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
645
+ /// Err(e) => println!("accept function failed: {:?}", e),
646
+ /// }
647
+ /// ```
625
648
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
626
649
pub fn accept ( & self ) -> io:: Result < ( UnixStream , SocketAddr ) > {
627
650
let mut storage: libc:: sockaddr_un = unsafe { mem:: zeroed ( ) } ;
@@ -636,33 +659,101 @@ impl UnixListener {
636
659
/// The returned `UnixListener` is a reference to the same socket that this
637
660
/// object references. Both handles can be used to accept incoming
638
661
/// connections and options set on one listener will affect the other.
662
+ ///
663
+ /// # Examples
664
+ ///
665
+ /// ```no_run
666
+ /// use std::os::unix::net::UnixListener;
667
+ ///
668
+ /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
669
+ ///
670
+ /// let listener_copy = listener.try_clone().expect("try_clone failed");
671
+ /// ```
639
672
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
640
673
pub fn try_clone ( & self ) -> io:: Result < UnixListener > {
641
674
self . 0 . duplicate ( ) . map ( UnixListener )
642
675
}
643
676
644
677
/// Returns the local socket address of this listener.
678
+ ///
679
+ /// # Examples
680
+ ///
681
+ /// ```no_run
682
+ /// use std::os::unix::net::UnixListener;
683
+ ///
684
+ /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
685
+ ///
686
+ /// let addr = socket.local_addr().expect("Couldn't get local address");
687
+ /// ```
645
688
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
646
689
pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
647
690
SocketAddr :: new ( |addr, len| unsafe { libc:: getsockname ( * self . 0 . as_inner ( ) , addr, len) } )
648
691
}
649
692
650
693
/// Moves the socket into or out of nonblocking mode.
694
+ ///
695
+ /// # Examples
696
+ ///
697
+ /// ```no_run
698
+ /// use std::os::unix::net::UnixListener;
699
+ ///
700
+ /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
701
+ ///
702
+ /// socket.set_nonblocking(true).expect("Couldn't set non blocking");
703
+ /// ```
651
704
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
652
705
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
653
706
self . 0 . set_nonblocking ( nonblocking)
654
707
}
655
708
656
709
/// Returns the value of the `SO_ERROR` option.
710
+ ///
711
+ /// # Examples
712
+ ///
713
+ /// ```no_run
714
+ /// use std::os::unix::net::UnixListener;
715
+ ///
716
+ /// let socket = match UnixListener::bind("/tmp/sock").unwrap();
717
+ /// if let Ok(Some(err)) = socket.take_error() {
718
+ /// println!("Got error: {:?}", err);
719
+ /// }
720
+ /// ```
657
721
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
658
722
pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
659
723
self . 0 . take_error ( )
660
724
}
661
725
662
726
/// Returns an iterator over incoming connections.
663
727
///
664
- /// The iterator will never return `None` and will also not yield the
665
- /// peer's `SocketAddr` structure.
728
+ /// The iterator will never return [`None`] and will also not yield the
729
+ /// peer's [`SocketAddr`] structure.
730
+ ///
731
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
732
+ /// [`SocketAddr`]: struct.SocketAddr.html
733
+ ///
734
+ /// # Examples
735
+ ///
736
+ /// ```no_run
737
+ /// use std::thread;
738
+ /// use std::os::unix::net::{UnixStream, UnixListener};
739
+ ///
740
+ /// fn handle_client(stream: UnixStream) {
741
+ /// // ...
742
+ /// }
743
+ ///
744
+ /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
745
+ ///
746
+ /// for stream in listener.incoming() {
747
+ /// match stream {
748
+ /// Ok(stream) => {
749
+ /// thread::spawn(|| handle_client(stream));
750
+ /// }
751
+ /// Err(err) => {
752
+ /// break;
753
+ /// }
754
+ /// }
755
+ /// }
756
+ /// ```
666
757
#[ stable( feature = "unix_socket" , since = "1.10.0" ) ]
667
758
pub fn incoming < ' a > ( & ' a self ) -> Incoming < ' a > {
668
759
Incoming { listener : self }
0 commit comments