Skip to content

Commit c35b9f6

Browse files
Add UnixListener doc examples
1 parent 7fe17f9 commit c35b9f6

File tree

1 file changed

+101
-10
lines changed

1 file changed

+101
-10
lines changed

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

+101-10
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ impl UnixStream {
437437
///
438438
/// ```no_run
439439
/// use std::os::unix::net::UnixStream;
440-
/// use std::time::Duration;
441440
///
442441
/// let socket = match UnixStream::connect("/tmp/sock").unwrap();
443442
/// socket.set_nonblocking(true).expect("Couldn't set non blocking");
@@ -453,7 +452,6 @@ impl UnixStream {
453452
///
454453
/// ```no_run
455454
/// use std::os::unix::net::UnixStream;
456-
/// use std::time::Duration;
457455
///
458456
/// let socket = match UnixStream::connect("/tmp/sock").unwrap();
459457
/// if let Ok(Some(err)) = socket.take_error() {
@@ -477,7 +475,6 @@ impl UnixStream {
477475
///
478476
/// ```no_run
479477
/// use std::os::unix::net::UnixStream;
480-
/// use std::time::Duration;
481478
///
482479
/// let socket = match UnixStream::connect("/tmp/sock").unwrap();
483480
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
@@ -557,7 +554,7 @@ impl IntoRawFd for UnixStream {
557554
///
558555
/// # Examples
559556
///
560-
/// ```rust,no_run
557+
/// ```no_run
561558
/// use std::thread;
562559
/// use std::os::unix::net::{UnixStream, UnixListener};
563560
///
@@ -580,9 +577,6 @@ impl IntoRawFd for UnixStream {
580577
/// }
581578
/// }
582579
/// }
583-
///
584-
/// // close the listener socket
585-
/// drop(listener);
586580
/// ```
587581
#[stable(feature = "unix_socket", since = "1.10.0")]
588582
pub struct UnixListener(Socket);
@@ -601,6 +595,20 @@ impl fmt::Debug for UnixListener {
601595

602596
impl UnixListener {
603597
/// 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+
/// ```
604612
#[stable(feature = "unix_socket", since = "1.10.0")]
605613
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
606614
fn inner(path: &Path) -> io::Result<UnixListener> {
@@ -620,8 +628,23 @@ impl UnixListener {
620628
/// Accepts a new incoming connection to this listener.
621629
///
622630
/// 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
624632
/// 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+
/// ```
625648
#[stable(feature = "unix_socket", since = "1.10.0")]
626649
pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
627650
let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
@@ -636,33 +659,101 @@ impl UnixListener {
636659
/// The returned `UnixListener` is a reference to the same socket that this
637660
/// object references. Both handles can be used to accept incoming
638661
/// 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+
/// ```
639672
#[stable(feature = "unix_socket", since = "1.10.0")]
640673
pub fn try_clone(&self) -> io::Result<UnixListener> {
641674
self.0.duplicate().map(UnixListener)
642675
}
643676

644677
/// 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+
/// ```
645688
#[stable(feature = "unix_socket", since = "1.10.0")]
646689
pub fn local_addr(&self) -> io::Result<SocketAddr> {
647690
SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
648691
}
649692

650693
/// 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+
/// ```
651704
#[stable(feature = "unix_socket", since = "1.10.0")]
652705
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
653706
self.0.set_nonblocking(nonblocking)
654707
}
655708

656709
/// 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+
/// ```
657721
#[stable(feature = "unix_socket", since = "1.10.0")]
658722
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
659723
self.0.take_error()
660724
}
661725

662726
/// Returns an iterator over incoming connections.
663727
///
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+
/// ```
666757
#[stable(feature = "unix_socket", since = "1.10.0")]
667758
pub fn incoming<'a>(&'a self) -> Incoming<'a> {
668759
Incoming { listener: self }

0 commit comments

Comments
 (0)