@@ -474,12 +474,30 @@ impl TcpListener {
474474 ///
475475 /// The address type can be any implementor of `ToSocketAddrs` trait. See
476476 /// its documentation for concrete examples.
477+ ///
478+ /// # Examples
479+ ///
480+ /// ```no_run
481+ /// use std::net::TcpListener;
482+ ///
483+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
484+ /// ```
477485 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
478486 pub fn bind < A : ToSocketAddrs > ( addr : A ) -> io:: Result < TcpListener > {
479487 super :: each_addr ( addr, net_imp:: TcpListener :: bind) . map ( TcpListener )
480488 }
481489
482490 /// Returns the local socket address of this listener.
491+ ///
492+ /// # Examples
493+ ///
494+ /// ```no_run
495+ /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
496+ ///
497+ /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
498+ /// assert_eq!(listener.local_addr().unwrap(),
499+ /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
500+ /// ```
483501 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
484502 pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
485503 self . 0 . socket_addr ( )
@@ -490,6 +508,15 @@ impl TcpListener {
490508 /// The returned `TcpListener` is a reference to the same socket that this
491509 /// object references. Both handles can be used to accept incoming
492510 /// connections and options set on one listener will affect the other.
511+ ///
512+ /// # Examples
513+ ///
514+ /// ```no_run
515+ /// use std::net::TcpListener;
516+ ///
517+ /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
518+ /// let listener_clone = listener.try_clone().unwrap();
519+ /// ```
493520 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
494521 pub fn try_clone ( & self ) -> io:: Result < TcpListener > {
495522 self . 0 . duplicate ( ) . map ( TcpListener )
@@ -500,6 +527,18 @@ impl TcpListener {
500527 /// This function will block the calling thread until a new TCP connection
501528 /// is established. When established, the corresponding `TcpStream` and the
502529 /// remote peer's address will be returned.
530+ ///
531+ /// # Examples
532+ ///
533+ /// ```no_run
534+ /// use std::net::TcpListener;
535+ ///
536+ /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
537+ /// match listener.accept() {
538+ /// Ok((_socket, addr)) => println!("new client: {:?}", addr),
539+ /// Err(e) => println!("couldn't get client: {:?}", e),
540+ /// }
541+ /// ```
503542 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
504543 pub fn accept ( & self ) -> io:: Result < ( TcpStream , SocketAddr ) > {
505544 self . 0 . accept ( ) . map ( |( a, b) | ( TcpStream ( a) , b) )
@@ -508,8 +547,28 @@ impl TcpListener {
508547 /// Returns an iterator over the connections being received on this
509548 /// listener.
510549 ///
511- /// The returned iterator will never return `None` and will also not yield
512- /// the peer's `SocketAddr` structure.
550+ /// The returned iterator will never return [`None`] and will also not yield
551+ /// the peer's [`SocketAddr`] structure.
552+ ///
553+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
554+ /// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
555+ ///
556+ /// # Examples
557+ ///
558+ /// ```no_run
559+ /// use std::net::TcpListener;
560+ ///
561+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
562+ ///
563+ /// for stream in listener.incoming() {
564+ /// match stream {
565+ /// Ok(stream) => {
566+ /// println!("new client!");
567+ /// }
568+ /// Err(e) => { /* connection failed */ }
569+ /// }
570+ /// }
571+ /// ```
513572 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
514573 pub fn incoming ( & self ) -> Incoming {
515574 Incoming { listener : self }
@@ -519,16 +578,35 @@ impl TcpListener {
519578 ///
520579 /// This value sets the time-to-live field that is used in every packet sent
521580 /// from this socket.
581+ ///
582+ /// # Examples
583+ ///
584+ /// ```no_run
585+ /// use std::net::TcpListener;
586+ ///
587+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
588+ /// listener.set_ttl(100).expect("could not set TTL");
589+ /// ```
522590 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
523591 pub fn set_ttl ( & self , ttl : u32 ) -> io:: Result < ( ) > {
524592 self . 0 . set_ttl ( ttl)
525593 }
526594
527595 /// Gets the value of the `IP_TTL` option for this socket.
528596 ///
529- /// For more information about this option, see [`set_ttl`][link].
597+ /// For more information about this option, see [`set_ttl() `][link].
530598 ///
531599 /// [link]: #method.set_ttl
600+ ///
601+ /// # Examples
602+ ///
603+ /// ```no_run
604+ /// use std::net::TcpListener;
605+ ///
606+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
607+ /// listener.set_ttl(100).expect("could not set TTL");
608+ /// assert_eq!(listener.ttl().unwrap_or(0), 100);
609+ /// ```
532610 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
533611 pub fn ttl ( & self ) -> io:: Result < u32 > {
534612 self . 0 . ttl ( )
@@ -542,6 +620,15 @@ impl TcpListener {
542620 ///
543621 /// If this is set to `false` then the socket can be used to send and
544622 /// receive packets from an IPv4-mapped IPv6 address.
623+ ///
624+ /// # Examples
625+ ///
626+ /// ```no_run
627+ /// use std::net::TcpListener;
628+ ///
629+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
630+ /// listener.set_only_v6(true).expect("Cannot set to IPv6");
631+ /// ```
545632 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
546633 pub fn set_only_v6 ( & self , only_v6 : bool ) -> io:: Result < ( ) > {
547634 self . 0 . set_only_v6 ( only_v6)
@@ -552,6 +639,16 @@ impl TcpListener {
552639 /// For more information about this option, see [`set_only_v6`][link].
553640 ///
554641 /// [link]: #method.set_only_v6
642+ ///
643+ /// # Examples
644+ ///
645+ /// ```no_run
646+ /// use std::net::TcpListener;
647+ ///
648+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
649+ /// listener.set_only_v6(true).expect("Cannot set to IPv6");
650+ /// assert_eq!(listener.only_v6().unwrap_or(false), true);
651+ /// ```
555652 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
556653 pub fn only_v6 ( & self ) -> io:: Result < bool > {
557654 self . 0 . only_v6 ( )
@@ -562,6 +659,15 @@ impl TcpListener {
562659 /// This will retrieve the stored error in the underlying socket, clearing
563660 /// the field in the process. This can be useful for checking errors between
564661 /// calls.
662+ ///
663+ /// # Examples
664+ ///
665+ /// ```no_run
666+ /// use std::net::TcpListener;
667+ ///
668+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
669+ /// listener.take_error().expect("No error was expected");
670+ /// ```
565671 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
566672 pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
567673 self . 0 . take_error ( )
@@ -571,6 +677,15 @@ impl TcpListener {
571677 ///
572678 /// On Unix this corresponds to calling fcntl, and on Windows this
573679 /// corresponds to calling ioctlsocket.
680+ ///
681+ /// # Examples
682+ ///
683+ /// ```no_run
684+ /// use std::net::TcpListener;
685+ ///
686+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
687+ /// listener.set_nonblocking(true).expect("Cannot set non-blocking");
688+ /// ```
574689 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
575690 pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
576691 self . 0 . set_nonblocking ( nonblocking)
0 commit comments