Skip to content

Commit

Permalink
Add sys::unix::SocketAddr::as_abstract_namespace()
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis authored and Thomasdezeeuw committed Oct 16, 2021
1 parent 04e0ca4 commit f4b9252
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/sys/unix/uds/socketaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ cfg_os_poll! {
None
}
}

/// Returns the contents of this address if it is an abstract namespace
/// without the leading null byte.
// Link to std::os::unix::net::SocketAddr pending
// https://github.com/rust-lang/rust/issues/85410.
pub fn as_abstract_namespace(&self) -> Option<&[u8]> {
if let AddressKind::Abstract(path) = self.address() {
Some(path)
} else {
None
}
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/unix_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ fn unix_listener_deregister() {
handle.join().unwrap();
}

#[cfg(target_os = "linux")]
#[test]
fn unix_listener_abstract_namesapce() {
use rand::Rng;
let num: u64 = rand::thread_rng().gen();
let name = format!("\u{0000}-mio-abstract-uds-{}", num);
let listener = UnixListener::bind(&name).unwrap();
assert_eq!(
listener.local_addr().unwrap().as_abstract_namespace(),
Some(&name.as_bytes()[1..]),
);
}

fn smoke_test<F>(new_listener: F, test_name: &'static str)
where
F: FnOnce(&Path) -> io::Result<UnixListener>,
Expand Down

0 comments on commit f4b9252

Please sign in to comment.