Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sys::unix::SocketAddr::as_abstract_namespace() (v0.7) #1520

Merged
merged 1 commit into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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