Skip to content

Commit 9976f5f

Browse files
Add missing doc examples for SocketAddr struct
1 parent 535b6d3 commit 9976f5f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ enum AddressKind<'a> {
8585
}
8686

8787
/// An address associated with a Unix socket.
88+
///
89+
/// # Examples
90+
///
91+
/// ```
92+
/// use std::os::unix::net::UnixListener;
93+
///
94+
/// let socket = match UnixListener::bind("/tmp/sock") {
95+
/// Ok(sock) => sock,
96+
/// Err(e) => {
97+
/// println!("Couldn't bind: {:?}", e);
98+
/// return
99+
/// }
100+
/// };
101+
/// let addr = socket.local_addr().expect("Couldn't get local address");
102+
/// ```
88103
#[derive(Clone)]
89104
#[stable(feature = "unix_socket", since = "1.10.0")]
90105
pub struct SocketAddr {
@@ -121,6 +136,16 @@ impl SocketAddr {
121136
}
122137

123138
/// Returns true if and only if the address is unnamed.
139+
///
140+
/// # Examples
141+
///
142+
/// ```
143+
/// use std::os::unix::net::UnixListener;
144+
///
145+
/// let socket = match UnixListener::bind("/tmp/sock").unwrap();
146+
/// let addr = socket.local_addr().expect("Couldn't get local address");
147+
/// assert_eq!(addr.is_unnamed(), false);
148+
/// ```
124149
#[stable(feature = "unix_socket", since = "1.10.0")]
125150
pub fn is_unnamed(&self) -> bool {
126151
if let AddressKind::Unnamed = self.address() {
@@ -131,6 +156,17 @@ impl SocketAddr {
131156
}
132157

133158
/// Returns the contents of this address if it is a `pathname` address.
159+
///
160+
/// # Examples
161+
///
162+
/// ```
163+
/// use std::os::unix::net::UnixListener;
164+
/// use std::path::Path;
165+
///
166+
/// let socket = match UnixListener::bind("/tmp/sock").unwrap();
167+
/// let addr = socket.local_addr().expect("Couldn't get local address");
168+
/// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
169+
/// ```
134170
#[stable(feature = "unix_socket", since = "1.10.0")]
135171
pub fn as_pathname(&self) -> Option<&Path> {
136172
if let AddressKind::Pathname(path) = self.address() {

0 commit comments

Comments
 (0)