Skip to content

Commit

Permalink
cross-platform doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaverde committed Oct 10, 2021
1 parent d68a8d9 commit 92d6805
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
2 changes: 2 additions & 0 deletions library/std/src/os/unix/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl SocketAddr {
/// Ok(())
/// }
/// ```
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
#[unstable(feature = "unix_socket_abstract", issue = "85410")]
pub fn as_abstract_namespace(&self) -> Option<&[u8]> {
Expand Down Expand Up @@ -271,6 +272,7 @@ impl SocketAddr {
/// Ok(())
/// }
/// ```
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
#[unstable(feature = "unix_socket_abstract", issue = "85410")]
pub fn from_abstract_namespace(namespace: &[u8]) -> io::Result<SocketAddr> {
Expand Down
20 changes: 13 additions & 7 deletions library/std/src/os/unix/net/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ impl UnixDatagram {
///
/// ```no_run
/// #![feature(unix_socket_abstract)]
/// use std::os::unix::net::{UnixDatagram, SocketAddr};
/// use std::os::unix::net::{UnixDatagram};
///
/// fn main() -> std::io::Result<()> {
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?; // Linux only
/// let sock = match UnixDatagram::bind_addr(&addr) {
/// let sock1 = UnixDatagram::bind("path/to/socket")?;
/// let addr = sock1.local_addr()?;
///
/// let sock2 = match UnixDatagram::bind_addr(&addr) {
/// Ok(sock) => sock,
/// Err(err) => {
/// println!("Couldn't bind: {:?}", err);
Expand Down Expand Up @@ -231,10 +233,12 @@ impl UnixDatagram {
///
/// ```no_run
/// #![feature(unix_socket_abstract)]
/// use std::os::unix::net::{UnixDatagram, SocketAddr};
/// use std::os::unix::net::{UnixDatagram};
///
/// fn main() -> std::io::Result<()> {
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?; // Linux only
/// let bound = UnixDatagram::bind("/path/to/socket")?;
/// let addr = bound.local_addr()?;
///
/// let sock = UnixDatagram::unbound()?;
/// match sock.connect_addr(&addr) {
/// Ok(sock) => sock,
Expand Down Expand Up @@ -549,10 +553,12 @@ impl UnixDatagram {
///
/// ```no_run
/// #![feature(unix_socket_abstract)]
/// use std::os::unix::net::{UnixDatagram, SocketAddr};
/// use std::os::unix::net::{UnixDatagram};
///
/// fn main() -> std::io::Result<()> {
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?;
/// let bound = UnixDatagram::bind("/path/to/socket")?;
/// let addr = bound.local_addr()?;
///
/// let sock = UnixDatagram::unbound()?;
/// sock.send_to_addr(b"bacon egg and cheese", &addr).expect("send_to_addr function failed");
/// Ok(())
Expand Down
8 changes: 5 additions & 3 deletions library/std/src/os/unix/net/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ impl UnixListener {
///
/// ```no_run
/// #![feature(unix_socket_abstract)]
/// use std::os::unix::net::{UnixListener, SocketAddr};
/// use std::os::unix::net::{UnixListener};
///
/// fn main() -> std::io::Result<()> {
/// let addr = SocketAddr::from_abstract_namespace(b"namespace")?; // Linux only
/// let listener = match UnixListener::bind_addr(&addr) {
/// let listener1 = UnixListener::bind("path/to/socket")?;
/// let addr = listener1.local_addr()?;
///
/// let listener2 = match UnixListener::bind_addr(&addr) {
/// Ok(sock) => sock,
/// Err(err) => {
/// println!("Couldn't bind: {:?}", err);
Expand Down
8 changes: 5 additions & 3 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ impl UnixStream {
///
/// ```no_run
/// #![feature(unix_socket_abstract)]
/// use std::os::unix::net::{UnixStream, SocketAddr};
/// use std::os::unix::net::{UnixListener, UnixStream};
///
/// fn main() -> std::io::Result<()> {
/// let addr = SocketAddr::from_abstract_namespace(b"hidden")?; // Linux only
/// match UnixStream::connect_addr(&addr) {
/// let listener = UnixListener::bind("/path/to/the/socket")?;
/// let addr = listener.local_addr()?;
///
/// let sock = match UnixStream::connect_addr(&addr) {
/// Ok(sock) => sock,
/// Err(e) => {
/// println!("Couldn't connect: {:?}", e);
Expand Down
24 changes: 24 additions & 0 deletions library/std/src/os/unix/net/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ fn test_unnamed_unix_datagram() {
assert_eq!(msg, &buf[..]);
}

#[test]
fn test_unix_datagram_connect_to_recv_addr() {
let dir = tmpdir();
let path1 = dir.path().join("sock1");
let path2 = dir.path().join("sock2");

let sock1 = or_panic!(UnixDatagram::bind(&path1));
let sock2 = or_panic!(UnixDatagram::bind(&path2));

let msg = b"hello world";
let sock1_addr = or_panic!(sock1.local_addr());
or_panic!(sock2.send_to_addr(msg, &sock1_addr));
let mut buf = [0; 11];
let (_, addr) = or_panic!(sock1.recv_from(&mut buf));

let new_msg = b"hello back";
let mut new_buf = [0; 10];
or_panic!(sock2.connect_addr(&addr));
or_panic!(sock2.send(new_msg)); // set by connect_addr
let usize = or_panic!(sock2.recv(&mut new_buf));
assert_eq!(usize, 10);
assert_eq!(new_msg, &new_buf[..]);
}

#[test]
fn test_connect_unix_datagram() {
let dir = tmpdir();
Expand Down

0 comments on commit 92d6805

Please sign in to comment.