Skip to content

Commit 7fe17f9

Browse files
Add doc examples for UnixStream
1 parent 9976f5f commit 7fe17f9

File tree

1 file changed

+145
-6
lines changed

1 file changed

+145
-6
lines changed

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

Lines changed: 145 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'a> fmt::Display for AsciiEscaped<'a> {
218218
///
219219
/// # Examples
220220
///
221-
/// ```rust,no_run
221+
/// ```no_run
222222
/// use std::os::unix::net::UnixStream;
223223
/// use std::io::prelude::*;
224224
///
@@ -248,6 +248,20 @@ impl fmt::Debug for UnixStream {
248248

249249
impl UnixStream {
250250
/// 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+
/// ```
251265
#[stable(feature = "unix_socket", since = "1.10.0")]
252266
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
253267
fn inner(path: &Path) -> io::Result<UnixStream> {
@@ -265,6 +279,20 @@ impl UnixStream {
265279
/// Creates an unnamed pair of connected sockets.
266280
///
267281
/// 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+
/// ```
268296
#[stable(feature = "unix_socket", since = "1.10.0")]
269297
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
270298
let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
@@ -277,62 +305,161 @@ impl UnixStream {
277305
/// object references. Both handles will read and write the same stream of
278306
/// data, and options set on one stream will be propogated to the other
279307
/// 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+
/// ```
280317
#[stable(feature = "unix_socket", since = "1.10.0")]
281318
pub fn try_clone(&self) -> io::Result<UnixStream> {
282319
self.0.duplicate().map(UnixStream)
283320
}
284321

285322
/// 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+
/// ```
286332
#[stable(feature = "unix_socket", since = "1.10.0")]
287333
pub fn local_addr(&self) -> io::Result<SocketAddr> {
288334
SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
289335
}
290336

291337
/// 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+
/// ```
292347
#[stable(feature = "unix_socket", since = "1.10.0")]
293348
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
294349
SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
295350
}
296351

297352
/// Sets the read timeout for the socket.
298353
///
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
301356
/// 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+
/// ```
302371
#[stable(feature = "unix_socket", since = "1.10.0")]
303372
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
304373
self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
305374
}
306375

307376
/// Sets the write timeout for the socket.
308377
///
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
311380
/// 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+
/// ```
312395
#[stable(feature = "unix_socket", since = "1.10.0")]
313396
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
314397
self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
315398
}
316399

317400
/// 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+
/// ```
318412
#[stable(feature = "unix_socket", since = "1.10.0")]
319413
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
320414
self.0.timeout(libc::SO_RCVTIMEO)
321415
}
322416

323417
/// 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+
/// ```
324429
#[stable(feature = "unix_socket", since = "1.10.0")]
325430
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
326431
self.0.timeout(libc::SO_SNDTIMEO)
327432
}
328433

329434
/// 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+
/// ```
330445
#[stable(feature = "unix_socket", since = "1.10.0")]
331446
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
332447
self.0.set_nonblocking(nonblocking)
333448
}
334449

335450
/// 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+
/// ```
336463
#[stable(feature = "unix_socket", since = "1.10.0")]
337464
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
338465
self.0.take_error()
@@ -342,7 +469,19 @@ impl UnixStream {
342469
///
343470
/// This function will cause all pending and future I/O calls on the
344471
/// 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+
/// ```
346485
#[stable(feature = "unix_socket", since = "1.10.0")]
347486
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
348487
self.0.shutdown(how)

0 commit comments

Comments
 (0)