Skip to content

Commit 92d6805

Browse files
committed
cross-platform doctests
1 parent d68a8d9 commit 92d6805

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

library/std/src/os/unix/net/addr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl SocketAddr {
214214
/// Ok(())
215215
/// }
216216
/// ```
217+
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
217218
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
218219
#[unstable(feature = "unix_socket_abstract", issue = "85410")]
219220
pub fn as_abstract_namespace(&self) -> Option<&[u8]> {
@@ -271,6 +272,7 @@ impl SocketAddr {
271272
/// Ok(())
272273
/// }
273274
/// ```
275+
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
274276
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
275277
#[unstable(feature = "unix_socket_abstract", issue = "85410")]
276278
pub fn from_abstract_namespace(namespace: &[u8]) -> io::Result<SocketAddr> {

library/std/src/os/unix/net/datagram.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ impl UnixDatagram {
118118
///
119119
/// ```no_run
120120
/// #![feature(unix_socket_abstract)]
121-
/// use std::os::unix::net::{UnixDatagram, SocketAddr};
121+
/// use std::os::unix::net::{UnixDatagram};
122122
///
123123
/// fn main() -> std::io::Result<()> {
124-
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?; // Linux only
125-
/// let sock = match UnixDatagram::bind_addr(&addr) {
124+
/// let sock1 = UnixDatagram::bind("path/to/socket")?;
125+
/// let addr = sock1.local_addr()?;
126+
///
127+
/// let sock2 = match UnixDatagram::bind_addr(&addr) {
126128
/// Ok(sock) => sock,
127129
/// Err(err) => {
128130
/// println!("Couldn't bind: {:?}", err);
@@ -231,10 +233,12 @@ impl UnixDatagram {
231233
///
232234
/// ```no_run
233235
/// #![feature(unix_socket_abstract)]
234-
/// use std::os::unix::net::{UnixDatagram, SocketAddr};
236+
/// use std::os::unix::net::{UnixDatagram};
235237
///
236238
/// fn main() -> std::io::Result<()> {
237-
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?; // Linux only
239+
/// let bound = UnixDatagram::bind("/path/to/socket")?;
240+
/// let addr = bound.local_addr()?;
241+
///
238242
/// let sock = UnixDatagram::unbound()?;
239243
/// match sock.connect_addr(&addr) {
240244
/// Ok(sock) => sock,
@@ -549,10 +553,12 @@ impl UnixDatagram {
549553
///
550554
/// ```no_run
551555
/// #![feature(unix_socket_abstract)]
552-
/// use std::os::unix::net::{UnixDatagram, SocketAddr};
556+
/// use std::os::unix::net::{UnixDatagram};
553557
///
554558
/// fn main() -> std::io::Result<()> {
555-
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?;
559+
/// let bound = UnixDatagram::bind("/path/to/socket")?;
560+
/// let addr = bound.local_addr()?;
561+
///
556562
/// let sock = UnixDatagram::unbound()?;
557563
/// sock.send_to_addr(b"bacon egg and cheese", &addr).expect("send_to_addr function failed");
558564
/// Ok(())

library/std/src/os/unix/net/listener.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ impl UnixListener {
8989
///
9090
/// ```no_run
9191
/// #![feature(unix_socket_abstract)]
92-
/// use std::os::unix::net::{UnixListener, SocketAddr};
92+
/// use std::os::unix::net::{UnixListener};
9393
///
9494
/// fn main() -> std::io::Result<()> {
95-
/// let addr = SocketAddr::from_abstract_namespace(b"namespace")?; // Linux only
96-
/// let listener = match UnixListener::bind_addr(&addr) {
95+
/// let listener1 = UnixListener::bind("path/to/socket")?;
96+
/// let addr = listener1.local_addr()?;
97+
///
98+
/// let listener2 = match UnixListener::bind_addr(&addr) {
9799
/// Ok(sock) => sock,
98100
/// Err(err) => {
99101
/// println!("Couldn't bind: {:?}", err);

library/std/src/os/unix/net/stream.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ impl UnixStream {
114114
///
115115
/// ```no_run
116116
/// #![feature(unix_socket_abstract)]
117-
/// use std::os::unix::net::{UnixStream, SocketAddr};
117+
/// use std::os::unix::net::{UnixListener, UnixStream};
118118
///
119119
/// fn main() -> std::io::Result<()> {
120-
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?; // Linux only
121-
/// match UnixStream::connect_addr(&addr) {
120+
/// let listener = UnixListener::bind("/path/to/the/socket")?;
121+
/// let addr = listener.local_addr()?;
122+
///
123+
/// let sock = match UnixStream::connect_addr(&addr) {
122124
/// Ok(sock) => sock,
123125
/// Err(e) => {
124126
/// println!("Couldn't connect: {:?}", e);

library/std/src/os/unix/net/tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,30 @@ fn test_unnamed_unix_datagram() {
303303
assert_eq!(msg, &buf[..]);
304304
}
305305

306+
#[test]
307+
fn test_unix_datagram_connect_to_recv_addr() {
308+
let dir = tmpdir();
309+
let path1 = dir.path().join("sock1");
310+
let path2 = dir.path().join("sock2");
311+
312+
let sock1 = or_panic!(UnixDatagram::bind(&path1));
313+
let sock2 = or_panic!(UnixDatagram::bind(&path2));
314+
315+
let msg = b"hello world";
316+
let sock1_addr = or_panic!(sock1.local_addr());
317+
or_panic!(sock2.send_to_addr(msg, &sock1_addr));
318+
let mut buf = [0; 11];
319+
let (_, addr) = or_panic!(sock1.recv_from(&mut buf));
320+
321+
let new_msg = b"hello back";
322+
let mut new_buf = [0; 10];
323+
or_panic!(sock2.connect_addr(&addr));
324+
or_panic!(sock2.send(new_msg)); // set by connect_addr
325+
let usize = or_panic!(sock2.recv(&mut new_buf));
326+
assert_eq!(usize, 10);
327+
assert_eq!(new_msg, &new_buf[..]);
328+
}
329+
306330
#[test]
307331
fn test_connect_unix_datagram() {
308332
let dir = tmpdir();

0 commit comments

Comments
 (0)